Tôi đang cố gắng để chuỗi Tìm kiếm lưới và loại bỏ tính năng đệ quy trong một đường ống bằng cách sử dụng scikit-tìm hiểu.Tìm kiếm lưới với tính năng đệ quy Loại bỏ trong đường ống scikit-tìm hiểu trả về một lỗi
GridSearchCV và RFE với phân loại "trần" hoạt động tốt:
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)
Đưa phân loại trong một đường ống trả về một lỗi: RuntimeError: Bộ phân loại không phơi bày "coef_" hoặc "feature_importances_" thuộc tính
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)
EDIT:
tôi đã nhận ra rằng tôi đã không rõ ràng mô tả vấn đề. Đây là đoạn trích rõ ràng hơn:
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)
Như bạn thấy, sự khác biệt duy nhất là đặt trình ước lượng vào đường ống. Tuy nhiên, đường ống ẩn các thuộc tính "coef_" hoặc "feature_importances_". Các câu hỏi là:
- Có cách nào tốt để giải quyết vấn đề này trong học tập không?
- Nếu không, hành vi này có mong muốn vì bất kỳ lý do nào không?
EDIT2:
cập nhật, đoạn làm việc dựa trên các câu trả lời được cung cấp bởi @ 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_)
Tôi sẽ xem xét mã nguồn để kiểm tra xem chuỗi sự kiện nào dẫn đến RuntimeError. Có thể bạn có thể ghi đè lên các thuộc tính của đối tượng trả về có liên quan và chỉ cần thêm lại vào các biến - ví dụ, nếu chúng giống nhau khi được trả về từ SVR(). Trong mọi trường hợp, make_pipeline() có thể không trả lại cùng một loại đối tượng như SVR(). – noumenal