python - 类型错误:只有整数标量数组可以转换为 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 (最近一次调用最后一次) 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: 只有整数标量数组可以转换为标量索引

回答1

可能您有不是 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))

相似文章

最新文章