我想使用Leave one out cross validation。但我得到以下错误:
AttributeError Traceback (most recent call last)
<ipython-input-19-f15f1e522706> in <module>()
3 loo = LeaveOneOut(num_of_examples)
4 #loo.get_n_splits(X_train_std)
----> 5 for train, test in loo.split(X_train_std):
6 print("%s %s" % (train, test))
AttributeError:&#39; LeaveOneOut&#39;对象没有属性&#39; split&#39;
详细代码如下:
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test =
train_test_split(X, y, test_size=0.3, random_state=0)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
sc.fit(X_train)
X_train_std = sc.transform(X_train)
X_test_std = sc.transform(X_test)
from sklearn.cross_validation import LeaveOneOut
num_of_examples = len(X_train_std)
loo = LeaveOneOut(num_of_examples)
for train, test in loo.split(X_train_std):
print("%s %s" % (train, test))
答案 0 :(得分:0)
我认为您使用的scikit-learn版本低于0.18,可能会参考0.18版本的一些教程。
在0.18之前的版本中,LeaveOneOut()
构造函数具有必需参数n
,在您发布的上述代码中未提供该参数。因此错误。您可以参考其中提及的documentation of LeaveOneOut for version 0.17 here:
参数: n :int数据集中的元素总数。
解决方案:
按如下方式初始化 LeaveOneOut :
loo = LeaveOneOut(size of X_train_std)
修改强>:
如果您使用的是scikit版本&gt; = 0.18:
from sklearn.model_selection import LeaveOneOut
for train_index, test_index in loo.split(X):
print("%s %s" % (train_index, test_index))
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
否则,对于版本&lt; 0.18使用这样的迭代(请注意,此处loo.split()
未使用,loo
直接使用):
from sklearn.cross_validation import LeaveOneOut
loo = LeaveOneOut(num_of_examples)
for train_index, test_index in loo:
print("%s %s" % (train_index, test_index))
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
答案 1 :(得分:0)
使用sklearn.model_selection导入train_test_split 而不是cross_validation,因为将cross_validation更改为model_selction