我试图训练和测试朴素贝叶斯分类器。
以下是我的代码的一部分:
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
构建的混淆矩阵。虽然我设置aylabels
和axlabels
同样的事情,但行和列在情节中是不同的。
我不知道发生了什么!
你忘了在confusion_matrix中设置labels参数。
cm = confusion_matrix(y_test, y_pred, labels=brands)