我正在跟踪sklearn k折文档,并编写了以下代码:
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
为什么会出现此错误?抱歉,显然我是一个初学者。
答案 0 :(得分:0)
正如错误所言。您的错误来自打印声明。这是因为i2c
生成的索引与python列表不兼容。试试这个,
KFold.split
输出:
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])