我有下面的代碼工作正常,因爲它是:如何通過類之間的屬性(克隆問題)
class Classifiers(object):
"""Multiple classifiers"""
class SVM():
"""
SVM Classifier Object.
This is binary classifier
"""
@staticmethod
def classifier(X, y):
from sklearn import svm
classifier = svm.SVC(kernel='linear', probability=True)
return(X,y,classifier)
class FeatureSelection(object):
def select_RFECV(self, X, y, clf):
"""
Feature ranking with recursive feature elimination
and cross-validated
"""
from sklearn.feature_selection import RFECV
from sklearn.svm import SVC
estimator = SVC(kernel="linear", probability=True)
# Below retrieving Clf failed
#estimator = clf
# Code below is ok
selector = RFECV(estimator, step=1, cv=5)
selector = selector.fit(X, y)
print selector.support_
return
def main():
# call estimator
svm = Classifiers.SVM()
# Create dataset
from sklearn import datasets
X, y = datasets.make_classification(n_samples = 100, n_features =20, n_classes=2)
# Feature selection
FS = FeatureSelection()
sel = FS.select_RFECV(X,y,svm)
if __name__ == '__main__':
main()
它產生的輸出是這樣的:
[False True False False False True False False False False True True
False False True False False False False True]
但我的問題是這個。類別FeatureSelection()
, 中的屬性select_RFECV
將其輸入中的一個輸入爲估計器clf
。現在,這個estimator
實際上與svm = Classifiers.SVM()
相同。
當我註釋掉estimator = SVC(kernel="linear", probability=True)
並取消註釋estimator = clf
。我得到了這個錯誤:
TypeError: Cannot clone object '<__main__.SVM instance at 0x1112f0fc8>' (type <type 'instance'>): it does not seem to be a scikit-learn estimator a it does not implement a 'get_params' methods.
如何正確地傳遞類之間的屬性?