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

Viewed 581

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

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