简体   繁体   中英

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

I'm traying to do kfold validation:

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))

but I get this error message:

-----------------------------------------------------------------------
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

Probably you either have arrays that are not numpy or indexes that are not of the int type. If it doesn't work, then show some rows with data 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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