简体   繁体   English

从逻辑回归绘制预测

[英]Plotting prediction from logistic regression

I would like to plot y_test and prediction in a scatter plot.我想在散点图中绘制 y_test 和预测。 I am using the logistic regression as model.我使用逻辑回归作为模型。

from sklearn.linear_model import LogisticRegression

vectorizer = CountVectorizer()

X = vectorizer.fit_transform(df['Spam'])
y = df['Label']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=27)

lr = LogisticRegression(solver='liblinear').fit(X_train, y_train)
pred_log = lr.predict(X_test)

I have tried as follows我试过如下

## Plot the model

plt.scatter(y_test, pred_log)
plt.xlabel("True Values")
plt.ylabel("Predictions")

and I got this:我得到了这个:

在此处输入图片说明

that I do not think it is what I should expect.我不认为这是我应该期待的。 y_test is (250,), similarly pred_log is (250,) y_test是 (250,),类似的pred_log是 (250,)

Am I considering the wrong variables to plot, or they are right?我是在考虑绘制错误的变量,还是它们是对的? I have no idea one what the plot with those four values mean.我不知道这四个值的情节是什么意思。 I would have been expected more dots in the plot, but maybe I am wrong.我本来希望情节中有更多的点,但也许我错了。

Please let me know if you need more info.如果您需要更多信息,请告诉我。 Thanks谢谢

I think you know LogisticRegression is a classification algorithm.我想你知道 LogisticRegression 是一种分类算法。 If you do binary classification it will predict whether predicted class is 0 or 1.If you want to get visualization about how model preform, you should consider confusion matrix .You can't use scatterplot for visualize classification results.如果你做二元分类,它会预测预测的类是 0 还是 1。如果你想获得关于模型如何预成型的可视化,你应该考虑混淆矩阵。你不能使用散点图来可视化分类结果。

import seaborn as sns
cm = confusion_matrix(y_true, y_pred)
sns.heatmap(cf_matrix, annot=True)

confusion matrix shows how many labels have correct predictions and how many are wrong.Looking at confusion matrix you can calculate how accurate the model.We can use different metrices like precision,recall and F1 score .混淆矩阵显示有多少标签具有正确的预测以及有多少是错误的。查看混淆矩阵,您可以计算模型的准确度。我们可以使用不同的指标,如精度、召回率和 F1 分数

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

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