混淆矩阵与轴的不同标签

问题描述 投票:0回答:1

我试图训练和测试朴素贝叶斯分类器。

以下是我的代码的一部分:

from sklearn.feature_extraction.text import CountVectorizer
matrix = CountVectorizer(ngram_range=(1,1))
X = matrix.fit_transform(data).toarray()
y =  [re.sub('[^A-Za-z]', ' ', y).strip(' ') for y in mobiles.iloc[:, 2]]

# split train and test data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y)

# Naive Bayes 
from sklearn.naive_bayes import GaussianNB

classifier = GaussianNB()

classifier.fit(X_train, y_train)

# predict class
y_pred = classifier.predict(X_test)

res = pd.DataFrame({'y_test':y_test, 'y_pred':y_pred})
print(res)
# Confusion matrix
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score, f1_score
cm = confusion_matrix(y_test, y_pred)
cr = classification_report(y_test, y_pred)

brands = list(set(y))

accuracy = accuracy_score(y_test, y_pred)
print("accuracy:", accuracy)
print("Confusion Matrix:")
import seaborn as sn
import pandas  as pd
import matplotlib.pyplot as plt

aylabels = brands #[str(i) for i in aylabels]
axlabels = brands #[str(i) for i in range(50)]

plt.figure(figsize=(10, 10))
sn.set(font_scale=1.4)  # for label size
sn.heatmap(cm, annot=True, annot_kws={"size": 12}, xticklabels=axlabels, yticklabels=aylabels)  # font size
plt.show()

以下是我在上面的代码中使用hitmap构建的混淆矩阵。虽然我设置aylabelsaxlabels同样的事情,但行和列在情节中是不同的。

enter image description here

我不知道发生了什么!

python scikit-learn
1个回答
1
投票

你忘了在confusion_matrix中设置labels参数。

cm = confusion_matrix(y_test, y_pred, labels=brands)
© www.soinside.com 2019 - 2024. All rights reserved.