Python SKLearn: TypeError: __init__() 得到了一个意外的关键字参数 n_splits

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45062395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:39:35  来源:igfitidea点击:

SKLearn: TypeError: __init__() got an unexpected keyword argument n_splits

pythonscikit-learn

提问by bclayman

I'm trying to use SKLearn (version 0.18.1) as follows:

我正在尝试使用 SKLearn(版本 0.18.1),如下所示:

from sklearn.model_selection import KFold
kfold = KFold(n_splits=5, random_state=100)

But I get this strange error:

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

---------------------------------------------------------------------------
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'

I've consulted the docs here:

我在这里查阅了文档:

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

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

and n_splitsdoes look like a parameter I should be able to pass...

并且n_splits确实看起来像一个我应该能够传递的参数......

Any idea what's going on here / how to fix?

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

Thanks!

谢谢!

回答by rriccilopes

You have a problem with your Scikit-Learn version, try to check it and look for the right documentation (here):

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

import sklearn
print(sklearn.__version__)

Or download the lastest version with pip install -U scikit-learn.

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

回答by seralouk

Open your terminal (cmd) and try these before you try to import the sklearn.

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

pip install -U scikit-learn

or if you have anaconda installed

或者如果您安装了 anaconda

conda install scikit-learn

or

或者

conda update conda 
conda update scikit-learn

Also make sure your have numpy and scipy:

还要确保你有 numpy 和 scipy:

pip install numpy 
pip install scipy

Restart the python shell after installing scipy !

安装 scipy 后重启 python shell!

回答by Amaan Gigani

Use capital X in the brackets. It worked for me.

在括号中使用大写 X。它对我有用。

from sklearn.model_selection import cross_val_score
accuracies = cross_val_score(estimator = classifier, X = x_train, y = y_train, cv = 10)