neghez
neghez

Reputation: 725

BaseEstimator in sklearn.base (Python)

I've been learning and practicing sklearn library on my own. When I participated Kaggle competitions, I noticed the provided sample code used BaseEstimator from sklearn.base. I don't quite understand how/why is BaseEstimator used.

from sklearn.base import BaseEstimator
class FeatureMapper:
    def __init__(self, features):
        self.features = features        #features contains feature_name, column_name, and extractor( which is CountVectorizer) 

    def fit(self, X, y=None):
        for feature_name, column_name, extractor in self.features:
            extractor.fit(X[column_name], y) #my question is: is X features? if yes, where is it assigned? or else how can X call column_name by X[column_name]. 

...

This is what I usually see on sklearn's tutorial page:

from sklearn import SomeClassifier
X = [[0, 0], [1, 1],[2, 2],[3, 3]]
Y = [0, 1, 2, 3]
clf = SomeClassifier()
clf = clf.fit(X, Y)

I couldn't find a good example or any documentations on sklearn's official page. Although I found the sklearn.base code on github, but I'd like some examples and explanation of how is it used.

UPDATE

Here is the link for the sample code: https://github.com/benhamner/JobSalaryPrediction/blob/master/features.py Correction: I just realized BaseEstimator is used for the class SimpleTransform. I guess my first question is why is it needed? (because it's not used anywhere in the computation), the other question is when define fit, what is X, and how is assigned? Because usually I see:

def mymethod(self, X, y=None):
     X=self.features
     # then do something to X[Column_name]

Upvotes: 29

Views: 23905

Answers (1)

ogrisel
ogrisel

Reputation: 40159

BaseEstimator provides among other things a default implementation for the get_params and set_params methods, see [the source code]. This is useful to make the model grid search-able with GridSearchCV for automated parameters tuning and behave well with others when combined in a Pipeline.

Upvotes: 33

Related Questions