LeaveOneOut scikit中的交叉验证了解

时间:2017-04-07 02:42:03

标签: scikit-learn cross-validation

有人知道如何删除下面的错误吗?

NameError                                 Traceback (most recent call last)
<ipython-input-31-d3625a93ead4> in <module>()
     11 loo = LeaveOneOut(num_of_examples)
     12 for train_index, test_index in loo:
---> 13     print("%s %s" % (train, test))
     14 

NameError: name 'train' is not defined

导致错误的代码如下所示:

from sklearn import datasets
import numpy as np
from sklearn.preprocessing import StandardScaler

iris = datasets.load_iris()
X = iris.data[:, [2, 3]]
y = iris.target
X, y
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)

sc = StandardScaler()
sc.fit(X_train)
X_train_std = sc.transform(X_train)
X_test_std = sc.transform(X_test)
num_of_examples = len(X_train_std)

这是我收到错误的地方:

from sklearn.cross_validation import LeaveOneOut
loo = LeaveOneOut(num_of_examples)
for train_index, test_index in loo:
    print("%s %s" % (train, test))

1 个答案:

答案 0 :(得分:0)

您没有在代码中定义traintest。如果您想要打印train_indextest_index,请执行以下操作:

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

此外,this documentation可能相关。