gpt4 book ai didi

python - Sklearn SVM - 如何获取错误预测列表?

转载 作者:行者123 更新时间:2023-11-30 09:17:18 26 4
gpt4 key购买 nike

我不是专家用户。我知道我可以获得混淆矩阵,但我想获得以错误方式分类的行的列表,以便在分类后研究它们。

在 stackoverflow 上我发现了这个 Can I get a list of wrong predictions in SVM score function in scikit-learn但我不确定是否已理解所有内容。

这是一个示例代码。

# importing necessary libraries
from sklearn import datasets
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split

# loading the iris dataset
iris = datasets.load_iris()

# X -> features, y -> label
X = iris.data
y = iris.target

# dividing X, y into train and test data
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state = 0)

# training a linear SVM classifier
from sklearn.svm import SVC
svm_model_linear = SVC(kernel = 'linear', C = 1).fit(X_train, y_train)
svm_predictions = svm_model_linear.predict(X_test)

# model accuracy for X_test
accuracy = svm_model_linear.score(X_test, y_test)

# creating a confusion matrix
cm = confusion_matrix(y_test, svm_predictions)

为了迭代行并找到错误的行,建议的解决方案是:

predictions = clf.predict(inputs)
for input, prediction, label in zip(inputs, predictions, labels):
if prediction != label:
print(input, 'has been classified as ', prediction, 'and should be ', label)

我不明白什么是“输入”/“输入”。如果我将此代码改编为我的代码,如下所示:

for input, prediction, label in zip (X_test, svm_predictions, y_test):
if prediction != label:
print(input, 'has been classified as ', prediction, 'and should be ', label)

我得到:

[6.  2.7 5.1 1.6] has been classified as  2 and should be  1

第 6 行是错误的行吗? 6后面的数字是什么?我问这个问题是因为我在比这个更大的数据集上使用相同的代码,所以我想确保我正在做正确的事情。我没有发布其他数据集,因为不幸的是我不能,但问题是我得到了这样的东西:

  (0, 253)  0.5339655767137572
(0, 601) 0.27665553856928027
(0, 1107) 0.7989633757962163 has been classified as 7 and should be 3
(0, 885) 0.3034934766501018
(0, 1295) 0.6432561790864061
(0, 1871) 0.7029318585026516 has been classified as 7 and should be 6
(0, 1020) 1.0 has been classified as 3 and should be 8

当我计算最后一个输出的每一行时,我获得了测试集的双倍行...所以我不确定我是否正在分析错误的预测结果列表...

最佳答案

如果您只想获取错误分类实例的列表,可以执行以下操作:

# with the following sentence you can get a mask of the items bad classified
mask = np.logical_not(np.equal(y_test, predictions))
# Now you can use the mask to see the elements bad classified:
print(f"Elements wrong classified: {X_test[mask]}")
print(f"Prediction by the model for each of those elements: {predictions[mask]}")
print(f"Actual value for each of those elements: {np.asarray(y_test)[mask]}")

关于python - Sklearn SVM - 如何获取错误预测列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52209312/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com