'python KeyError: "['2', '3'] not found in axis"

I'm doing SVM code, but I got some trouble.

The steps are:

Step1. 80% of the information for the data for the training SVM model, and the remaining 20% is the testing data.

Step2. First, the training data is constructed to construct the SVM classification model, and then the test data input model is used to facilitate the predictive classification accuracy.

Step3. Each rotation removal of a finger indicator, reinsert the establishment of a class model and record the classification quasi-degree 𝐴𝑖, 𝑖 = 1,2,..., 25.

Step4. Calculate the impact of the accuracy of each indicator on the accuracy of the classification 𝜇𝑖, that is, 𝜇𝑖 = 𝐴𝑎𝑙𝑙 − 𝐴𝑖 , 𝑖 = 1,2,..., 25.

Step5. If 𝜇𝑖 ≺ 0, it means that the accuracy of this indicator will increase to the overall classification accuracy. However, at this stage, the guidelines for obtaining the minimum value of 𝜇𝑖 will be selected to remove it, and the criteria that will not eliminate all the degree of impact at once.

Step6. Repeat the remaining index step 1 to step 5 until all the impact of all the criteria is ≥ 0, then the screening is ended.

My code is as below:

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn import svm
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler

data = pd.read_csv('iris2.csv')
# print(data.head())

df_col = data.iloc[:,0:4]
target = data.iloc[:,4]

for i in range(4):
    each_c = [] #the accuracy of each criteria
    
    for j in df_col:
        # print('j = ',j)
        X_train,X_test,y_train,y_test = train_test_split(df_col,target,test_size=0.8)
        #the model of all critrias
        clf = svm.SVC() 
        clf.fit(X_train,y_train)
        Aall = clf.score(X_test, y_test)
        # print('Aall = ',Aall) #the accuracy of all critrias miodel
        #the model of a critria be deleted
        df_col_remove = df_col.drop(j, axis=1)
        X_train,X_test,y_train,y_test = train_test_split(df_col_remove,target,test_size=0.8)
        clf = svm.SVC() 
        clf.fit(X_train,y_train)
        ai = clf.score(X_test, y_test) 
        # print('ai = ',ai) #the accuracy of a critria be deleted
        ui = np.round(Aall - ai,3) #ui
        # print('ui = ',ui) #the imapct of sort
        each_c.append(ui)
    print('each_c = ',each_c) #[]
    s = pd.Series(each_c).rename(dict(enumerate(df_col.columns)))
    df_col = df_col.append(s, ignore_index=True)
    print(df_col)
    df_col = df_col.loc[:,s.ne(s.min())]
    print (df_col)
    df_col = df_col.drop(df_col.iloc[150:151]) #drop each_c and re-establish the model

In the last line, I get a KeyError: "['2', '3'] not found in axis"

Here is the csv, iris2.csv

Can someone help me to solve this? thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source