Permalink
Browse files

Merge pull request #712 from agramfort/fix_y_center

Fix y centering with multiple y see #708
  • Loading branch information...
2 parents 80438a2 + 03443da commit d101d74239059ef6b2e505eccb738e37de66c475 @mblondel mblondel committed Mar 19, 2012
Showing with 47 additions and 4 deletions.
  1. +2 −2 sklearn/linear_model/base.py
  2. +3 −2 sklearn/linear_model/omp.py
  3. +42 −0 sklearn/linear_model/tests/test_ridge.py
@@ -55,12 +55,12 @@ def center_data(X, y, fit_intercept, normalize=False, copy=True):
X /= X_std
else:
X_std = np.ones(X.shape[1])
- y_mean = y.mean()
+ y_mean = y.mean(axis=0)
y = y - y_mean
else:
X_mean = np.zeros(X.shape[1])
X_std = np.ones(X.shape[1])
- y_mean = 0.
+ y_mean = 0. if y.ndim == 1 else np.zeros(y.shape[1], dtype=X.dtype)
return X, y, X_mean, y_mean, X_std
@@ -506,15 +506,16 @@ def fit(self, X, y, Gram=None, Xy=None):
"""
X = array2d(X)
y = np.asarray(y)
- if y.ndim == 1:
- y = y[:, np.newaxis]
n_features = X.shape[1]
X, y, X_mean, y_mean, X_std = self._center_data(X, y,
self.fit_intercept,
self.normalize,
self.copy_X)
+ if y.ndim == 1:
+ y = y[:, np.newaxis]
+
if self.n_nonzero_coefs == None and self.tol is None:
self.n_nonzero_coefs = int(0.1 * n_features)
@@ -68,6 +68,48 @@ def test_ridge():
assert_true(ridge.score(X, y) > 0.9)
+def test_ridge_shapes():
+ """Test shape of coef_ and intercept_
+ """
+ n_samples, n_features = 5, 10
+ X = np.random.randn(n_samples, n_features)
+ y = np.random.randn(n_samples)
+ Y1 = y[:, np.newaxis]
+ Y = np.c_[y, 1 + y]
+
+ ridge = Ridge()
+
+ ridge.fit(X, y)
+ assert_equal(ridge.coef_.shape, (n_features,))
+ assert_equal(ridge.intercept_.shape, ())
+
+ ridge.fit(X, Y1)
+ assert_equal(ridge.coef_.shape, (1, n_features))
+ assert_equal(ridge.intercept_.shape, (1, ))
+
+ ridge.fit(X, Y)
+ assert_equal(ridge.coef_.shape, (2, n_features))
+ assert_equal(ridge.intercept_.shape, (2, ))
+
+
+def test_ridge_intercept():
+ """Test intercept with multiple targets GH issue #708
+ """
+ n_samples, n_features = 5, 10
+ X = np.random.randn(n_samples, n_features)
+ y = np.random.randn(n_samples)
+ Y = np.c_[y, 1. + y]
+
+ ridge = Ridge()
+
+ ridge.fit(X, y)
+ intercept = ridge.intercept_
+
+ ridge.fit(X, Y)
+ assert_almost_equal(ridge.intercept_[0], intercept)
+ assert_almost_equal(ridge.intercept_[1], intercept + 1.)
+
+
def test_toy_ridge_object():
"""Test BayesianRegression ridge classifier

0 comments on commit d101d74

Please sign in to comment.