为什么sk中的k折学习不打印数组值

时间:2019-06-03 01:11:24

标签: python scikit-learn cross-validation

我正在跟踪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

为什么会出现此错误?抱歉,显然我是一个初学者。

1 个答案:

答案 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])