I was going to try the code in this link:
我想试试这个链接的代码:
I am getting error from the line which refers to StratifiedKFold(n_splits=60)
. Can anybody tell me how i can solve this error?
我从这条线中得到了一个错误,它指的是层状褶皱(n_= 60)。有人能告诉我怎么解决这个错误吗?
Here is the Code:
这是代码:
import numpy as np
from scipy import interp
import matplotlib.pyplot as plt
from itertools import cycle
from sklearn import svm, datasets
from sklearn.metrics import roc_curve, auc
from sklearn.cross_validation import StratifiedKFold
iris = datasets.load_iris()
X = iris.data
y = iris.target
X, y = X[y != 2], y
X, y
cv = StratifiedKFold(n_splits=6)
classifier = svm.SVC(kernel='linear', probability=True,
random_state=random_state)
mean_tpr = 0.0
mean_fpr = np.linspace(0, 1, 100)
Here is the error:
这是错误:
TypeError Traceback (most recent call last)
<ipython-input-227-2af2773f4987> in <module>()
----> 1 sklearn.cross_validation.StratifiedKFold(n_splits=6)
2 #cv = StratifiedKFold(n_splits=6, shuffle=True, random_state=1)
3 classifier = svm.SVC(kernel='linear', probability=True,
4 random_state=random_state)
5
TypeError: __init__() got an unexpected keyword argument 'n_splits'
1 个解决方案
#1
2
You are not getting any warnings while importing the sklearn.cross-validation
module. This means that your installed version is less than 0.18.
在导入sklearn时,您没有得到任何警告。交叉验证模块。这意味着您安装的版本小于0.18。
If your scikit-learn version is < 0.18
, then change the below lines: (Taken from StratifiedKFold documentation for version 0.17)
如果您的scikitt -学习版本是< 0.18,那么更改以下代码:(从0.17版本的分层文档中提取)
#Notice the extra parameter y and change of name for n_splits to n_folds
cv = StratifiedKFold(y, n_folds=6)
#Also note that the cv is called directly in for loop
for train_index, test_index in cv:
print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
If your scikit-learn version is >=0.18
, then only you can use the n_splits
parameter for the cv
: (Taken from StratifiedKFold current documentation, which is what I think you are referring to)
如果您的scikitt -学习版本是>=0.18,那么只有您可以为cv使用n_参数(从分层的当前文档中提取,这是我认为您所指的)
#Notice the extra parameter y is removed here
cv = StratifiedKFold(n_splits=6)
#Also note that the cv.split() is called here (opposed to cv in ver 0.17 above)
for train_index, test_index in cv.split(X, y):
print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
Recommendation:
推荐:
Update your scikit-learn to the latest version 0.18. Because most documentation you will find by directly searching will be of this version and it will get you confused.
更新您的scikit-学习到最新版本0.18。因为你会直接搜索到的大多数文档都是这个版本的,这会让你感到困惑。
Edit:
编辑:
I have already answered your similar question here: - Issue with Cross Validation
我已经在这里回答了你的类似的问题:交叉验证的问题。
So next time, please mention the version of libraries you use in the question itself, and remember to access their relevant documentation, not the other ones.
所以下次,请提到你在问题本身中使用的库的版本,并且记住要访问他们的相关文档,而不是其他的。
#1
2
You are not getting any warnings while importing the sklearn.cross-validation
module. This means that your installed version is less than 0.18.
在导入sklearn时,您没有得到任何警告。交叉验证模块。这意味着您安装的版本小于0.18。
If your scikit-learn version is < 0.18
, then change the below lines: (Taken from StratifiedKFold documentation for version 0.17)
如果您的scikitt -学习版本是< 0.18,那么更改以下代码:(从0.17版本的分层文档中提取)
#Notice the extra parameter y and change of name for n_splits to n_folds
cv = StratifiedKFold(y, n_folds=6)
#Also note that the cv is called directly in for loop
for train_index, test_index in cv:
print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
If your scikit-learn version is >=0.18
, then only you can use the n_splits
parameter for the cv
: (Taken from StratifiedKFold current documentation, which is what I think you are referring to)
如果您的scikitt -学习版本是>=0.18,那么只有您可以为cv使用n_参数(从分层的当前文档中提取,这是我认为您所指的)
#Notice the extra parameter y is removed here
cv = StratifiedKFold(n_splits=6)
#Also note that the cv.split() is called here (opposed to cv in ver 0.17 above)
for train_index, test_index in cv.split(X, y):
print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
Recommendation:
推荐:
Update your scikit-learn to the latest version 0.18. Because most documentation you will find by directly searching will be of this version and it will get you confused.
更新您的scikit-学习到最新版本0.18。因为你会直接搜索到的大多数文档都是这个版本的,这会让你感到困惑。
Edit:
编辑:
I have already answered your similar question here: - Issue with Cross Validation
我已经在这里回答了你的类似的问题:交叉验证的问题。
So next time, please mention the version of libraries you use in the question itself, and remember to access their relevant documentation, not the other ones.
所以下次,请提到你在问题本身中使用的库的版本,并且记住要访问他们的相关文档,而不是其他的。