TypeError: only integer scalar arrays can be converted to a scalar index in python

TypeError: only integer scalar arrays can be converted to a scalar index in python

我正在尝试进行 k 折验证

X = df[['Smedications', 'Infections', 'lib' , 'north']].values

Y= df['Comorbidities'].values

kf = KFold(n_splits=10, shuffle=True)
list(kf.split(X))
splits = list(kf.split(X))
train_indices, test_indices = splits[0]
X_train = X[train_indices]
X_test = X[test_indices]
y_train = y[train_indices]
y_test = y[test_indices]

model = LogisticRegression()
model.fit(X_train, y_train)
print(model.score(X_test, y_test))

但是我有错误信息

TypeError Traceback (most recent call last) in () 12 X_train = X[train_indices] 13 X_test = X[test_indices] ---> 14 y_train = y[train_indices] 15 y_test = y[test_indices] 16

TypeError: only integer scalar arrays can be converted to a scalar index

可能您拥有不是 numpy 的数组或不是 int 类型的索引。如果不起作用,则显示一些包含数据 X、Y 的行。

X = df[['Smedications', 'Infections', 'lib' , 'north']].values

Y= df['Comorbidities'].values

kf = KFold(n_splits=10, shuffle=True)
list(kf.split(X))
splits = list(kf.split(X))
train_indices, test_indices = splits[0]
X_train = np.array(X)[train_indices.astype(int)]
X_test = np.array(X)[test_indices.astype(int)]
y_train = np.array(y)[train_indices.astype(int)]
y_test = np.array(y)[test_indices.astype(int)]

model = LogisticRegression()
model.fit(X_train, y_train)
print(model.score(X_test, y_test))