我试图在Python中使用分层K折交叉验证来运行这个SVM,但是我不断得到如下错误
from sklearn.model_selection import train_test_splitfrom sklearn.svm import SVCfrom sklearn.utils import shufflefrom sklearn import preprocessingfrom sklearn.preprocessing import MinMaxScalerfrom sklearn.model_selection import StratifiedKFoldfrom sklearn.metrics import accuracy_score, zero_one_loss, confusion_matriximport pandas as pdimport numpy as npz = pd.read_csv('/home/User/datasets/gtzan.csv', header=0)X = z.iloc[:, :-1]y = z.iloc[:, -1:]X = np.array(X)y = np.array(y)# Performing standard scalingscaler = preprocessing.MinMaxScaler()X_scaled = scaler.fit_transform(X)# Defining the SVM with 'rbf' kernelsvc = SVC(kernel='rbf', C=100, random_state=50)#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.10, shuffle=True)skf = StratifiedKFold(n_splits=10, shuffle=True)accuracy_score = []#skf.get_n_splits(X, y)for train_index, test_index in skf.split(X, y): X_train, X_test = X_scaled[train_index], X_scaled[test_index] y_train, y_test = y[train_index], y[test_index] # Training the model svc.fit(X_train, np.ravel(y_train)) # Prediction on test dataste y_pred = svc.predict(X_test) # Obtaining the accuracy scores of the model score = accuracy_score(y_test, y_pred) accuracy_score.append(score)# Print the accuarcy of the svm modelprint('accuracy score: %0.3f' % np.mean(accuracy_score))
然而,它给我带来了如下错误
Traceback (most recent call last): File "/home/User/Test_SVM.py", line 55, in <module> score = accuracy_score(y_test, y_pred)TypeError: 'list' object is not callable
是什么让这个分数列表不可调用,我该如何修复这个错误?
回答:
accuracy_score
在我代码中是一个列表,我也在调用同一个列表作为函数,这覆盖了函数accuarcy_score
的现有功能。将列表名称更改为acc_score
解决了这个问题。