列表对象在 SVM 中不可调用
Posted
技术标签:
【中文标题】列表对象在 SVM 中不可调用【英文标题】:List object not callable in SVM 【发布时间】:2021-10-26 04:27:33 【问题描述】:我正在尝试在 Python 中使用分层 K 折叠运行此 SVM,但是我不断收到如下错误
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.utils import shuffle
from sklearn import preprocessing
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import accuracy_score, zero_one_loss, confusion_matrix
import pandas as pd
import numpy as np
z = 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 scaling
scaler = preprocessing.MinMaxScaler()
X_scaled = scaler.fit_transform(X)
# Defining the SVM with 'rbf' kernel
svc = 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 model
print('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
是一个列表。你不能叫它。你可能想打电话给svc.score
。
@user3046211,你的问题解决了吗?
@user1740577 :是的,它解决了问题 - DYZ 的建议帮助我解决了问题
【参考方案1】:
accuracy_score
是我的代码中的一个列表,我也调用了同一个列表作为函数,它覆盖了函数accuarcy_score
的现有功能。将列表名称更改为acc_score
,即可解决问题。
【讨论】:
以上是关于列表对象在 SVM 中不可调用的主要内容,如果未能解决你的问题,请参考以下文章
Python/Pandas TypeError:“列表”对象不可调用