【问题标题】:When do i have to use the fit method of scikit learn?我什么时候必须使用 scikit learn 的 fit 方法?
【发布时间】:2017-03-22 10:44:58
【问题描述】:

我不明白什么时候必须使用 scikit learn 的 fit 方法。

在此网页中:http://machinelearningmastery.com/automate-machine-learning-workflows-pipelines-python-scikit-learn/ 有一个管道 + StandardScaler 的例子。不使用fit方法。

但在另一个:http://scikit-learn.org/stable/auto_examples/svm/plot_rbf_parameters.html 还有一个StandardScaler,还有一个fit方法。

这是我的代码:Pipeline+Robustscaler:

result_list = []

for name in ["AWA","Rem","S1","S2","SWS","SX", "ALL"]: 
    x=sio.loadmat('/home/{}_E.mat'.format(name))['x'] 
    s_y=sio.loadmat('/home/{}_E.mat'.format(name))['y']
    y=np.ravel(s_y)

    print(name, x.shape, y.shape) 
    print("")

    #Create a pipeline
    clf = make_pipeline(preprocessing.RobustScaler(), SVC(cache_size=1000, kernel='rbf'))


    ###################10x20 SSS##################################
    print("10x20")
    xSSSmean20 = []
    for i in range(10):
        sss= StratifiedShuffleSplit(y, 20, test_size=0.1, random_state=i)
        scoresSSS=cross_validation.cross_val_score(clf, x, y, cv=sss)

        xSSSmean20.append(scoresSSS.mean()) 

     result_list.append(xSSSmean20)

     print("") 

【问题讨论】:

  • 为什么这是一个不好的问题?
  • 因为The fit method is not used. 是错误的说法。用于cross_val_score
  • 好的。谢谢你。我不知道。

标签: python-3.x scikit-learn svm


【解决方案1】:

训练您的分类器,您必须将其放入您的训练数据集中。

第一个链接也这样做,并不是因为它没有明确出现在 sn-p 中,所以它没有这样做:

cross_val_score 方法使用 model,这是将其拟合与数据的估计器。

查看方法“cross_val_score”的实现,并尝试了解它是如何工作的,而不是在不了解它的作用的情况下使用它。

Here是函数的文档,hereGitHub中的实现参考。

忠告:

当您不理解某些内容时,请尝试深入研究代码。你会学到很多东西!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-28
    • 2018-01-24
    • 2019-05-23
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    相关资源
    最近更新 更多