SKLearn:TypeError:__init __()获得了意外的关键字参数n_splits

bcl*_*man 5 python scikit-learn

我正在尝试按以下方式使用SKLearn(版本0.18.1):

from sklearn.model_selection import KFold
kfold = KFold(n_splits=5, random_state=100)
Run Code Online (Sandbox Code Playgroud)

但是我得到这个奇怪的错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-b8dd4f5596be> in <module>()
----> 1 kfold = KFold(k=5, random_state=100)
      2 results = cross_val_score(estimator, X, Y, cv=kfold)
      3 print("Results: %.2f (%.2f) MSE" % (results.mean(), results.std()))

TypeError: __init__() got an unexpected keyword argument 'k'
Run Code Online (Sandbox Code Playgroud)

我在这里查阅了文档:

http://scikit-learn.org/stable/modules/generation/sklearn.model_selection.KFold.html

n_splits确实看起来像一个参数,我应该可以通过...

知道这里发生了什么/如何解决?

谢谢!

mak*_*kis 7

在尝试导入 sklearn 之前,打开您的终端 (cmd) 并尝试这些。

pip install -U scikit-learn
Run Code Online (Sandbox Code Playgroud)

或者如果您安装了 anaconda

conda install scikit-learn
Run Code Online (Sandbox Code Playgroud)

或者

conda update conda 
conda update scikit-learn
Run Code Online (Sandbox Code Playgroud)

还要确保你有 numpy 和 scipy:

pip install numpy 
pip install scipy
Run Code Online (Sandbox Code Playgroud)

安装 scipy 后重启python shell!


小智 2

您的 Scikit-Learn 版本有问题,请尝试检查它并查找正确的文档(此处):

import sklearn
print(sklearn.__version__)
Run Code Online (Sandbox Code Playgroud)

或者下载最新版本pip install -U scikit-learn