मैं विज्ञान-सीखने का उपयोग करके पाइपलाइन में ग्रिड सर्च और रिकर्सिव फीचर एलिमिनेशन श्रृंखला की कोशिश कर रहा हूं।रिकर्सिव फीचर के साथ ग्रिड सर्च विज्ञान-सीखने वाली पाइपलाइन में उन्मूलन एक त्रुटि देता है
GridSearchCV और "नंगे" वर्गीकारक साथ RFE ठीक काम करता है:
from sklearn.datasets import make_friedman1
from sklearn import feature_selection
from sklearn.grid_search import GridSearchCV
from sklearn.svm import SVR
X, y = make_friedman1(n_samples=50, n_features=10, random_state=0)
est = SVR(kernel="linear")
selector = feature_selection.RFE(est)
param_grid = dict(estimator__C=[0.1, 1, 10])
clf = GridSearchCV(selector, param_grid=param_grid, cv=10)
clf.fit(X, y)
एक पाइप लाइन में वर्गीकारक लाना एक त्रुटि देता है: RuntimeError: वर्गीकारक "coef_" या "feature_importances_" का खुलासा नहीं करता विशेषताओं
from sklearn.datasets import make_friedman1
from sklearn import feature_selection
from sklearn import preprocessing
from sklearn import pipeline
from sklearn.grid_search import GridSearchCV
from sklearn.svm import SVR
X, y = make_friedman1(n_samples=50, n_features=10, random_state=0)
est = SVR(kernel="linear")
std_scaler = preprocessing.StandardScaler()
pipe_params = [('std_scaler', std_scaler), ('clf', est)]
pipe = pipeline.Pipeline(pipe_params)
selector = feature_selection.RFE(pipe)
param_grid = dict(estimator__clf__C=[0.1, 1, 10])
clf = GridSearchCV(selector, param_grid=param_grid, cv=10)
clf.fit(X, y)
संपादित करें:
मैंने महसूस किया है कि मैं समस्या का वर्णन स्पष्ट नहीं था। यह स्पष्ट स्निपेट है:
from sklearn.datasets import make_friedman1
from sklearn import feature_selection
from sklearn import pipeline
from sklearn.grid_search import GridSearchCV
from sklearn.svm import SVR
X, y = make_friedman1(n_samples=50, n_features=10, random_state=0)
# This will work
est = SVR(kernel="linear")
selector = feature_selection.RFE(est)
clf = GridSearchCV(selector, param_grid={'estimator__C': [1, 10]})
clf.fit(X, y)
# This will not work
est = pipeline.make_pipeline(SVR(kernel="linear"))
selector = feature_selection.RFE(est)
clf = GridSearchCV(selector, param_grid={'estimator__svr__C': [1, 10]})
clf.fit(X, y)
जैसा कि आप देख सकते हैं, केवल अंतर अनुमानक को पाइपलाइन में डाल रहा है। हालांकि, पाइपलाइन "coef_" या "feature_importances_" विशेषताओं को छुपाती है। प्रश्न हैं:
- क्या विज्ञान-सीखने में इसका निपटारा करने का कोई अच्छा तरीका है?
- यदि नहीं, तो यह व्यवहार किसी भी कारण से वांछित है?
EDIT2:
अपडेट किया गया, काम टुकड़ा @Chris
from sklearn.datasets import make_friedman1
from sklearn import feature_selection
from sklearn import pipeline
from sklearn.grid_search import GridSearchCV
from sklearn.svm import SVR
class MyPipe(pipeline.Pipeline):
def fit(self, X, y=None, **fit_params):
"""Calls last elements .coef_ method.
Based on the sourcecode for decision_function(X).
Link: https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/pipeline.py
----------
"""
super(MyPipe, self).fit(X, y, **fit_params)
self.coef_ = self.steps[-1][-1].coef_
return self
X, y = make_friedman1(n_samples=50, n_features=10, random_state=0)
# Without Pipeline
est = SVR(kernel="linear")
selector = feature_selection.RFE(est)
clf = GridSearchCV(selector, param_grid={'estimator__C': [1, 10, 100]})
clf.fit(X, y)
print(clf.grid_scores_)
# With Pipeline
est = MyPipe([('svr', SVR(kernel="linear"))])
selector = feature_selection.RFE(est)
clf = GridSearchCV(selector, param_grid={'estimator__svr__C': [1, 10, 100]})
clf.fit(X, y)
print(clf.grid_scores_)
मैं स्रोत कोड का निरीक्षण करता हूं ताकि यह जांच सके कि घटनाओं की कौन सी श्रृंखला RuntimeError की ओर ले जाती है। यह काफी संभव है कि आप प्रासंगिक रिटर्न ऑब्जेक्ट के गुणों को ओवरराइड कर सकें और केवल चर में वापस जोड़ सकें - उदाहरण के लिए, यदि वे एसवीआर() से लौटे जाने पर समान हैं। किसी भी मामले में, make_pipeline() उसी प्रकार की ऑब्जेक्ट को वापस नहीं कर सकता क्योंकि एसवीआर() करता है। – noumenal