Permalink
Browse files

Merge branch 'master' of http://github.com/fabianp/scikit-learn

  • Loading branch information...
2 parents 2138c51 + cbdbda6 commit 4b7167568261317ee708564173bd04f909754f99 @GaelVaroquaux GaelVaroquaux committed Sep 16, 2010
Showing with 3,379 additions and 622 deletions.
  1. +1 −1 scikits/learn/glm/__init__.py
  2. +0 −140 scikits/learn/glm/lars.py
  3. +1 −154 scikits/learn/glm/tests/test_lars.py
  4. +148 −0 scikits/learn/src/cblas/ATL_drefcopy.c
  5. +170 −0 scikits/learn/src/cblas/ATL_drefrot.c
  6. +146 −0 scikits/learn/src/cblas/ATL_drefrotg.c
  7. +0 −99 scikits/learn/src/cblas/ATL_dreftpsvLTN.c
  8. +0 −99 scikits/learn/src/cblas/ATL_dreftpsvLTU.c
  9. +32 −30 scikits/learn/src/cblas/{ATL_dtpsv.c → ATL_dreftrsv.c}
  10. +11 −15 scikits/learn/src/cblas/{ATL_dreftpsvLNU.c → ATL_dreftrsvLNN.c}
  11. +13 −17 scikits/learn/src/cblas/{ATL_dreftpsvLNN.c → ATL_dreftrsvLNU.c}
  12. +96 −0 scikits/learn/src/cblas/ATL_dreftrsvLTN.c
  13. +96 −0 scikits/learn/src/cblas/ATL_dreftrsvLTU.c
  14. +11 −15 scikits/learn/src/cblas/{ATL_dreftpsvUNN.c → ATL_dreftrsvUNN.c}
  15. +11 −15 scikits/learn/src/cblas/{ATL_dreftpsvUNU.c → ATL_dreftrsvUNU.c}
  16. +11 −14 scikits/learn/src/cblas/{ATL_dreftpsvUTU.c → ATL_dreftrsvUTN.c}
  17. +11 −14 scikits/learn/src/cblas/{ATL_dreftpsvUTN.c → ATL_dreftrsvUTU.c}
  18. +942 −0 scikits/learn/src/cblas/atlas_aux.h
  19. 0 scikits/learn/src/cblas/atlas_dsysinfo.h
  20. +129 −0 scikits/learn/src/cblas/atlas_level1.h
  21. +338 −0 scikits/learn/src/cblas/atlas_level2.h
  22. +477 −0 scikits/learn/src/cblas/atlas_misc.h
  23. +59 −0 scikits/learn/src/cblas/atlas_refalias1.h
  24. +85 −0 scikits/learn/src/cblas/atlas_refalias2.h
  25. +423 −0 scikits/learn/src/cblas/atlas_reflevel1.h
  26. +1 −0 scikits/learn/src/cblas/atlas_reflevel2.h
  27. 0 scikits/learn/src/cblas/atlas_type.h
  28. +52 −0 scikits/learn/src/cblas/cblas_dcopy.c
  29. +60 −0 scikits/learn/src/cblas/cblas_drot.c
  30. +42 −0 scikits/learn/src/cblas/cblas_drotg.c
  31. +13 −9 scikits/learn/src/cblas/{cblas_dtpsv.c → cblas_dtrsv.c}
@@ -12,7 +12,7 @@
from .bayes import BayesianRidge, ARDRegression
from .base import LinearRegression
from .bayes import BayesianRidge, ARDRegression
-from .lars import LARS, LassoLARS, lars_path, LeastAngleRegression
+from .lars import LARS, LassoLARS, lars_path
from .coordinate_descent import Lasso, ElasticNet, LassoCV, ElasticNetCV, \
lasso_path, enet_path
from .ridge import Ridge
View
@@ -13,13 +13,9 @@
import scipy.sparse as sp # needed by LeastAngleRegression
from .base import LinearModel
-from .._minilearn import lars_fit_wrap
from ..utils.fixes import copysign
from ..utils import arrayfuncs
-# all linalg.solve solve a triangular system, so this could be heavily
-# optimized by binding (in scipy ?) trsv or trsm
-
def lars_path(X, y, Gram=None, max_iter=None, alpha_min=0,
method="lar", precompute=True):
""" Compute Least Angle Regression and LASSO path
@@ -65,8 +61,6 @@ def lars_path(X, y, Gram=None, max_iter=None, alpha_min=0,
XXX : add reference papers
"""
- # TODO: precompute : empty for now
- #
# TODO: detect stationary points.
# Lasso variant
# store full path
@@ -395,137 +389,3 @@ def fit (self, X, y, Gram=None, **params):
return self
-
-#### OLD C-based LARS : will probably be removed
-
-
-class LeastAngleRegression(LinearModel):
- """
- Least Angle Regression using the LARS algorithm.
-
- Attributes
- ----------
- `coef_` : array, shape = [n_features]
- parameter vector (w in the fomulation formula)
-
- `intercept_` : float
- independent term in decision function.
-
- `coef_path_` : array, shape = [max_features + 1, n_features]
- Full coeffients path.
-
- Notes
- -----
- predict does only work correctly in the case of normalized
- predictors.
-
- See also
- --------
- scikits.learn.glm.Lasso
-
- """
-
- def __init__(self):
- self.alphas_ = np.empty(0, dtype=np.float64)
- self._chol = np.empty(0, dtype=np.float64)
- self.beta_ = np.empty(0, dtype=np.float64)
-
- def fit (self, X, Y, fit_intercept=True, max_features=None, normalize=True):
- """
- Fit the model according to data X, Y.
-
- Parameters
- ----------
- X : numpy array of shape [n_samples,n_features]
- Training data
-
- Y : numpy array of shape [n_samples]
- Target values
-
- fit_intercept : boolean, optional
- wether to calculate the intercept for this model. If set
- to false, no intercept will be used in calculations
- (e.g. data is expected to be already centered).
-
- max_features : int, optional
- number of features to get into the model. The iterative
- will stop just before the `max_features` variable enters
- in the active set. If not specified, min(N, p) - 1
- will be used.
-
- normalize : boolean
- whether to normalize (make all non-zero columns have mean
- 0 and norm 1).
- """
- ## TODO: resize (not create) arrays, check shape,
- ## add a real intercept
-
- X = np.asanyarray(X, dtype=np.float64, order='C')
- _Y = np.asanyarray(Y, dtype=np.float64, order='C')
-
- if Y is _Y: Y = _Y.copy()
- else: Y = _Y
-
- if max_features is None:
- max_features = min(*X.shape)-1
-
- sum_k = max_features * (max_features + 1) /2
- self.alphas_.resize(max_features + 1)
- self._chol.resize(sum_k)
- self.beta_.resize(sum_k)
- coef_row = np.zeros(sum_k, dtype=np.int32)
- coef_col = np.zeros(sum_k, dtype=np.int32)
-
-
- if normalize:
- # will only normalize non-zero columns
- self._xmean = X.mean(0)
- self._ymean = Y.mean(0)
- X = X - self._xmean
- Y = Y - self._ymean
- self._norms = np.apply_along_axis (np.linalg.norm, 0, X)
- nonzeros = np.flatnonzero(self._norms)
- X[:, nonzeros] /= self._norms[nonzeros]
- else:
- self._xmean = 0.
- self._ymean = 0.
-
- lars_fit_wrap(0, X, Y, self.beta_, self.alphas_, coef_row,
- coef_col, self._chol, max_features)
-
- self.coef_path_ = sp.coo_matrix((self.beta_,
- (coef_row, coef_col)),
- shape=(X.shape[1], max_features+1)).todense()
-
- self.coef_ = np.ravel(self.coef_path_[:, max_features])
-
- # XXX : should use self._set_intercept
- if fit_intercept:
- self.intercept_ = self._ymean
- else:
- self.intercept_ = 0.
-
- return self
-
-
- def predict(self, X, normalize=True):
- """
- Predict using the linear model.
-
- Parameters
- ----------
- X : numpy array of shape [n_samples,n_features]
-
- Returns
- -------
- C : array, shape = [n_samples]
- Returns predicted values.
- """
- X = np.asanyarray(X, dtype=np.float64, order='C')
- if normalize:
- X -= self._xmean
- X /= self._norms
- return np.dot(X, self.coef_) + self.intercept_
-
-
-
@@ -4,7 +4,7 @@
from nose.tools import assert_equal, assert_true
-from ..lars import lars_path, LeastAngleRegression, LassoLARS, LARS
+from ..lars import lars_path, LassoLARS, LARS
from ..coordinate_descent import Lasso
from scikits.learn import datasets
@@ -96,156 +96,3 @@ def test_lasso_lars_vs_lasso_cd(verbose=False):
assert error < 1e-5
-
-################################################################################
-# Test LeastAngleRegression
-################################################################################
-
-def test_toy():
- """Very simple test on a small dataset"""
- X = [[1, 0, -1.],
- [0, 0, 0],
- [0, 1, .9]]
- Y = [1, 0, -1]
-
- clf = LeastAngleRegression().fit(X, Y, max_features=1)
- assert_array_almost_equal(clf.coef_, [0, 0, -1.2624], decimal=4)
- assert_array_almost_equal(clf.alphas_, [1.4135, 0.1510], decimal=4)
- assert_array_almost_equal(clf.alphas_.shape, clf.coef_path_.shape[1])
- assert np.linalg.norm(clf.predict(X) - Y) < 0.3
-
-
- clf = LeastAngleRegression().fit(X, Y) # implicitly max_features=2
- assert_array_almost_equal(clf.coef_, [0, -.0816, -1.34412], decimal=4)
- assert_array_almost_equal(clf.alphas_, \
- [ 1.41356, .1510, 3.919e-16], decimal=4)
- assert_array_almost_equal(clf.alphas_.shape, clf.coef_path_.shape[1])
- assert_array_almost_equal(clf.predict(X), np.array(Y))
-
- # TODO: check that Lasso with coordinate descent finds the same
- # coefficients
-
-
-def test_toy2():
- """
- Check that LAR responds well to features (columns) that are zero
- arrays.
- """
- X = [[0, 0],
- [0, 1]]
- Y = [0, 1]
- clf = LeastAngleRegression().fit(X, Y)
- assert_array_almost_equal(clf.coef_, [0., .70], decimal=2)
-
-
-def test_feature_selection():
- n_samples, n_features = 442, 100
-
- # deterministic test
- np.random.seed(0)
-
- # generate random input set
- X = np.random.randn(n_samples, n_features)
-
- # generate a ground truth model with only the first 10 features being non
- # zeros (the other features are not correlated to Y and should be ignored by
- # the L1 regularizer)
- coef_ = np.random.randn(n_features)
- coef_[10:] = 0.0
-
- # generate the grand truth Y from the model and Y
- Y = np.dot(X, coef_)
- Y += np.random.normal(Y.shape) # make some (label) noise!
-
- # fit the model assuming that will allready know that only 10 out of
- # n_features are contributing to Y
- clf = LeastAngleRegression().fit(X, Y, max_features=10)
-
- # ensure that only the first 10 coefs are non zeros and the remaining set to
- # null, as in the ground thrutg model
- assert_equal((clf.coef_[:10] == 0.0).sum(), 0)
- assert_equal((clf.coef_[10:] != 0.0).sum(), 0)
-
- # train again, but this time without knowing in advance how many features
- # are useful:
- clf = LeastAngleRegression().fit(X, Y, max_features=None)
- assert_equal((clf.coef_[:10] == 0.0).sum(), 0)
- assert_equal((clf.coef_[10:] != 0.0).sum(), 89)
-
- # explicitly set to zero parameters really close to zero (manual
- # thresholding)
- sparse_coef = np.where(abs(clf.coef_) < 1e-13,
- np.zeros(clf.coef_.shape),
- clf.coef_)
-
- # we find again that only the first 10 features are useful
- assert_equal((sparse_coef[:10] == 0.0).sum(), 0)
- assert_equal((sparse_coef[10:] != 0.0).sum(), 0)
-
-
-def test_predict(verbose=False):
- """
- Just see if predicted values are close from known response.
- """
- n, m = 10, 20
- np.random.seed(0)
- X = np.random.randn(n, m)
- Y = np.random.randn(n)
- Y = Y - Y.mean() # center response
-
- Y_ = LeastAngleRegression().fit(X, Y, fit_intercept=False).predict(X)
- if verbose:
- print np.linalg.norm(Y - Y_)
- assert np.linalg.norm(Y-Y_) < 1e-10
-
-
- # the same but with an intercept
- Y = Y + 10.
- Y_ = LeastAngleRegression().fit(X, Y).predict(X)
- assert np.linalg.norm(Y-Y_) < 1e-10
-
-
-def test_sparse_coding():
- """Use LARS as a sparse encoder w.r.t to a given fixed dictionary"""
- n_samples, n_features = 42, 100
- n_dictionary = 30
- max_features = 5
-
- # generate random input set
- X = np.random.randn(n_samples, n_features)
-
- # take the first vectors of the dataset as a dictionnary
- D = X[:n_dictionary].T
-
- assert_equal(D.shape, (n_features, n_dictionary))
-
- def sparse_encode(vector):
- return LeastAngleRegression().fit(
- D, vector, max_features=max_features, normalize=False,
- fit_intercept=False
- ).coef_
-
- def sparse_decode(vector):
- return np.dot(D, vector)
-
- # sparse encode each vector and check the quality of the encoded
- # representation
- for i, x_i in enumerate(X):
- # compute a sparse representation of x_i using the dictionary D
- c_i = sparse_encode(x_i)
-
- assert_equal(c_i.shape, (n_dictionary,))
-
- if i < n_dictionary:
- # x_i is one of the vector of D hence there is a trivial sparse code
- expected_code = np.zeros(n_dictionary)
- expected_code[i] = 1.0
-
- assert_almost_equal(c_i, expected_code, decimal=2)
- else:
- # x_i does not exactly belong to the dictionary, hence up to 5
- # components of the dictionary are used to represent it
- assert_true((c_i != 0.0).sum() <= max_features)
-
-
-
Oops, something went wrong.

0 comments on commit 4b71675

Please sign in to comment.