繁体   English   中英

TypeError:只有整数标量数组可以转换为python中的标量索引

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

我正在准备进行kfold验证:

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)
<ipython-input-90-752d1f80537e> in <module>()
     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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM