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]