简体   繁体   English

朴素贝叶斯和 SVM 分类 - 如何在 xy 轴上提高 plot 精度?

[英]Naive Bayes and SVM classification - how to plot accuracy on x y axis?

I'm trying to generate some line graph with an x and y axis demonstrating accuracy of 2 different algorithms running a classification - Naive Bayes and SVM.我正在尝试生成一些带有 x 和 y 轴的线图,以展示运行分类的 2 种不同算法的准确性 - 朴素贝叶斯和 SVM。

I train/test the data like this:我训练/测试这样的数据:

# split the dataset into training and validation datasets
train_x, valid_x, train_y, valid_y = model_selection.train_test_split(result['post'], result['type'], test_size=0.30, random_state=1)

# label encode the target variable
encoder = preprocessing.LabelEncoder()
train_y = encoder.fit_transform(train_y)
valid_y = encoder.fit_transform(valid_y)

def tokenizersplit(str):
    return str.split()
tfidf_vect = TfidfVectorizer(tokenizer=tokenizersplit, encoding='utf-8', min_df=2, ngram_range=(1, 2), max_features=25000)

tfidf_vect.fit(result['post'])
tfidf_vect.transform(result['post'])

xtrain_tfidf = tfidf_vect.transform(train_x)
xvalid_tfidf = tfidf_vect.transform(valid_x)

def train_model(classifier, trains, t_labels, valids, v_labels):
    # fit the training dataset on the classifier
    classifier.fit(trains, t_labels)

    # predict the labels on validation dataset
    predictions = classifier.predict(valids)

    return metrics.accuracy_score(predictions, v_labels)

# Naive Bayes
accuracy = train_model(naive_bayes.MultinomialNB(), xtrain_tfidf, train_y, xvalid_tfidf, valid_y)
print ("NB accuracy: ", accuracy)

However for an assignment I need something plotted on the x/y axis using matplotlib.但是对于分配,我需要使用 matplotlib 在 x/y 轴上绘制一些东西。 I tried this:我试过这个:

m=linear_model.LogisticRegression()
m.fit(xtrain_tfidf, train_y)
y_pred = m.predict(xvalid_tfidf)
print(metrics.classification_report(valid_y, y_pred))
plt.plot(valid_y, y_pred)
plt.show()

But this gives me:但这给了我:

在此处输入图像描述

I need something that can more easily compare the accuracy of Naive Bayes vs SVM vs another algorithm.我需要一些可以更轻松地比较朴素贝叶斯与 SVM 与另一种算法的准确性的东西。 How can I do this?我怎样才能做到这一点? Plotting classification report:绘制分类报告:

plt.plot(metrics.classification_report(valid_y, y_pred))
plt.show()

在此处输入图像描述

My classification output:我的分类output:

  precision    recall  f1-score   support

           0       1.00      0.18      0.31        11
           1       0.00      0.00      0.00        14
           2       0.00      0.00      0.00        19
           3       0.50      0.77      0.61        66
           4       0.39      0.64      0.49        47
           5       0.00      0.00      0.00        23

    accuracy                           0.46       180
   macro avg       0.32      0.27      0.23       180
weighted avg       0.35      0.46      0.37       180

Error w edit:错误 w 编辑:

df = pd.DataFrame(metrics.classification_report(valid_y, y_pred)).transpose()

gives error给出错误

ValueError: DataFrame constructor not properly called! ValueError: DataFrame 构造函数未正确调用!

metrics.classification_report summarizes the prediction result. metrics.classification_report总结了预测结果。 So this is not meant for plotting and just for printing a "report".所以这不是为了绘图而只是为了打印“报告”。 If you want the table in a visual format you can follow https://stackoverflow.com/a/34304414/4005668 .如果您希望表格采用可视格式,您可以按照https://stackoverflow.com/a/34304414/4005668 操作

Otherwise you can get the dataframe by capturing it in a dataframe否则,您可以通过在 dataframe 中捕获来获得 dataframe

import pandas as pd
# put it in a dataframe
df = pd.DataFrame(metrics.classification_report(..)).transpose()
# plot the dataframe
df.plot()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM