估算器应该是一个实现' fit'方法

时间:2018-06-02 11:32:57

标签: python scikit-learn

当我尝试在python中运行代码时遇到这个问题,我该怎么解决呢?  ---> 95验证(sm_classifier,x_test_normal_s,y_n2_test,y_test,classes_names,' NSLKDD SAE-SAE(test)')          96 #if 名称 ==" main ":main()## with if          97

None

将numpy导入为np 导入数学 随机导入 来自运营商导入项目集

类Softmax:     #from IPython.core.debugger import Tracer; Tracer()()

<ipython-input-23-95022ce9a680> in validation(classifier, data, y_data, y_target, class_names, title)
     52         print ("No accuracy to be computed")
     53     else:
---> 54         accuracy = model_selection.cross_val_score(classifier,x, y_target, scoring='accuracy')
     55         print("Accuracy: "+ str(accuracy))
     56     precision = model_selection.cross_val_score(self.classifier, x, target, scoring='precision')

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in cross_val_score(estimator, X, y, groups, scoring, cv, n_jobs, verbose, fit_params, pre_dispatch)
    130     cv = check_cv(cv, y, classifier=is_classifier(estimator))
    131     cv_iter = list(cv.split(X, y, groups))
--> 132     scorer = check_scoring(estimator, scoring=scoring)
    133     # We clone the estimator to make sure that all the folds are
    134     # independent, and that it is pickle-able.

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\metrics\scorer.py in check_scoring(estimator, scoring, allow_none)
    248     if not hasattr(estimator, 'fit'):
    249         raise TypeError("estimator should be an estimator implementing "
--> 250                         "'fit' method, %r was passed" % estimator)
    251     if isinstance(scoring, six.string_types):
    252         return get_scorer(scoring)

TypeError: estimator should be an estimator implementing 'fit' method, <__main__.Softmax object at 0x00000000048D1F98> was passed

1 个答案:

答案 0 :(得分:0)

如果你改变

它应该工作(或至少,它修复了当前的错误)
def train(self, X, y):

def fit(self, X, y):

有效的sklearn估算工具需要fitpredict方法。

检查一下 How to create a custom Sklearn Estimator class

尝试更换:

class Softmax:

人:

from sklearn.base import BaseEstimator, ClassifierMixin
class Softmax(BaseEstimator, ClassifierMixin):