1

I am working with a csv file: 39 participants (rows) each having values for 30 features (columns). I am trying to implement LeaveOneOut() using the below code. I am getting a key error... Any help would be appreciated!

# code
X = df.drop(labels=['Diagnosis'], axis=1) # dropped diagnosis 

Y = df['Diagnosis'].values
Y = Y.astype('int') 

loo = LeaveOneOut()
for train, test in loo.split(X, Y):
    X_train, X_test = X[train], X[test]
    Y_train, Y_test = Y[train], Y[test]

svm = SVC(kernel='linear')
svm.fit(X_train,Y_train)
pred_svm = svm.predict(X_test)
print(classification_report(Y_test, pred_svm))
print(confusion_matrix(Y_test, pred_svm))
K.level4
  • 11
  • 3
  • What is the error and also can you share the your implementation of LeaveOneOut if the problem is caused by that? – alparslan mimaroğlu Aug 11 '21 at 07:14
  • The error reads: raise KeyError(f"None of [{key}] are in the [{axis_name}]") KeyError: "None of [Int64Index([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,\n... When I put a print(X_train, X_test, Y_train, Y_test) function below my 'for' loop, it gives this error again and no splits are printed. Thanks again – K.level4 Aug 11 '21 at 13:50

1 Answers1

0

If you can share what is the result of the loo I can answer more accurately but as far as I understand it is a list of indexes instead of a boolean mask therefore you can change your code like this.

for train, test in loo.split(X, Y):
    X_train, X_test = X.loc[train].copy(), X.loc[test].copy()
    Y_train, Y_test = Y[train], Y[test]
alparslan mimaroğlu
  • 1,450
  • 1
  • 10
  • 20
  • Thank you, although this does now split the training and testing data appropriately, the new warning message is: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. The confusion matrix produced is all 1's. Thank you again for the help, I am new to this – K.level4 Aug 11 '21 at 14:19
  • you just have to copy the dataframes. I am editing the answer – alparslan mimaroğlu Aug 11 '21 at 14:25
  • Sorry, this did not work. I have thought of some other solutions I can try instead! Thank you again for the head start! – K.level4 Aug 11 '21 at 18:37