我遵循了sklearn k-fold验证的文档,并编写了以下代码:
import numpy as np
from sklearn.model_selection import KFold
X = ["w", "x", "y", "a"]
print(X[0])
kf = KFold(n_splits=4)
for train, test in kf.split(X):
print(X[(test)])它在最后一行输出一个错误:
TypeError: only integer scalar arrays can be converted to a scalar index为什么会出现这个错误?对不起,显然我是个初学者。
发布于 2019-06-03 09:27:33
正如错误所说的那样。您的错误来自于print语句。这是因为KFold.split生成的索引与python列表不兼容。尝尝这个,
import numpy as np
from sklearn.model_selection import KFold
X = np.array(["w", "x", "y", "a"])
kf = KFold(n_splits=4)
for train, test in kf.split(X):
print(train, test)
print(X[test])输出:
[1 2 3] [0]
['w']
[0 2 3] [1]
['x']
[0 1 3] [2]
['y']
[0 1 2] [3]
['a']https://stackoverflow.com/questions/56420126
复制相似问题