Permalink
Browse files

Merge branch 'ridge'

  • Loading branch information...
2 parents 5e891f4 + b09ffd7 commit 215ed615456afe5654de9fddb2a037e0bc95a9f8 @mblondel mblondel committed Jan 28, 2011
@@ -41,6 +41,7 @@
from scikits.learn.datasets import load_files
from scikits.learn.feature_extraction.text.sparse import Vectorizer
+from scikits.learn.linear_model import RidgeClassifier
from scikits.learn.svm.sparse import LinearSVC
from scikits.learn.linear_model.sparse import SGDClassifier
from scikits.learn import metrics
@@ -154,6 +155,11 @@ def benchmark(clf):
print
return score, train_time, test_time
+for clf, name in ((RidgeClassifier(), "Ridge Classifier"),):
+ print 80*'='
+ print name
+ results = benchmark(clf)
+
for penalty in ["l2", "l1"]:
print 80*'='
print "%s penalty" % penalty.upper()
View
@@ -253,6 +253,37 @@ def score(self, X, y):
################################################################################
+class TransformerMixin(object):
+ """ Mixin class for all transformers in the scikit learn
+ """
+
+ def fit_transform(self, X, y=None, **fit_params):
+ """Fit model to data and subsequently transform the data
+
+ Sometimes, fit and transform can be implemented more efficiently jointly
+ than separately. In those cases, the estimator will typically override
+ the method.
+
+ Parameters
+ ----------
+ X : numpy array of shape [n_samples, n_features]
+ Training set.
+
+ y : numpy array of shape [n_samples]
+ Target values.
+
+ Returns
+ -------
+ self : returns an instance of self.
+ """
+ if y is None:
+ # fit / transform methods of arity 1
+ return self.fit(X, **fit_params).transform(X)
+ else:
+ # fit / transform methods of arity 2
+ return self.fit(X, y, **fit_params).transform(X, y)
+
+################################################################################
# XXX: Temporary solution to figure out if an estimator is a classifier
def _get_sub_estimator(estimator):
@@ -22,7 +22,7 @@
from .coordinate_descent import Lasso, ElasticNet, LassoCV, ElasticNetCV, \
lasso_path, enet_path
from .stochastic_gradient import SGDClassifier, SGDRegressor
-from .ridge import Ridge
+from .ridge import Ridge, RidgeCV, RidgeClassifier, RidgeClassifierCV
from .logistic import LogisticRegression
from . import sparse
@@ -11,9 +11,13 @@
# License: BSD Style.
import numpy as np
+import scipy.sparse as sp
from ..base import BaseEstimator, RegressorMixin, ClassifierMixin
from .sgd_fast import Hinge, Log, ModifiedHuber, SquaredLoss, Huber
+from ..utils.extmath import safe_sparse_dot
+from ..utils import safe_asanyarray
+
###
### TODO: intercept for all models
@@ -39,8 +43,8 @@ def predict(self, X):
C : array, shape = [n_samples]
Returns predicted values.
"""
- X = np.asanyarray(X)
- return np.dot(X, self.coef_) + self.intercept_
+ X = safe_asanyarray(X)
+ return safe_sparse_dot(X, self.coef_) + self.intercept_
@staticmethod
def _center_data(X, y, fit_intercept):
@@ -50,9 +54,12 @@ def _center_data(X, y, fit_intercept):
centered.
"""
if fit_intercept:
- Xmean = X.mean(axis=0)
+ if sp.issparse(X):
+ Xmean = np.zeros(X.shape[1])
+ else:
+ Xmean = X.mean(axis=0)
+ X = X - Xmean
ymean = y.mean()
- X = X - Xmean
y = y - ymean
else:
Xmean = np.zeros(X.shape[1])
Oops, something went wrong.

3 comments on commit 215ed61

Owner

ogrisel replied Jan 28, 2011

Very interesting work. However I think such large merges should better be handled by the pull-request / code review process with notification on the mailing list.

Owner

mblondel replied Jan 28, 2011

I did do a pull request although I did not mention it on the ML .

Owner

ogrisel replied Jan 28, 2011

Sorry, I missed it, my bad :)

Please sign in to comment.