我一直在尝试用Python学习logistic回归。 我想通过一个混淆矩阵来评估我的模型。 因为我是python的新手,所以我不知道怎么做。 谷歌搜索显示我如何创建一个,但我得到的结果只是创建矩阵。 我想要更多的统计推断。 在R中,“Caret”包有一个名为confusionMatrix的函数,它提供了许多有用的信息。 例如:代码:
library(caret)
x <- c(1,0,1,1,0,1,1,0,0,1)
y <- c(0,1,1,1,0,1,0,0,0,0)
x <- as.factor(x)
y <- as.factor(y)
confusionMatrix(x,y)
输出:
Confusion Matrix and Statistics
Reference
Prediction 0 1
0 3 1
1 3 3
Accuracy : 0.6
95% CI : (0.2624, 0.8784)
No Information Rate : 0.6
P-Value [Acc > NIR] : 0.6331
Kappa : 0.2308
Mcnemar's Test P-Value : 0.6171
Sensitivity : 0.500
Specificity : 0.750
Pos Pred Value : 0.750
Neg Pred Value : 0.500
Prevalence : 0.600
Detection Rate : 0.300
Detection Prevalence : 0.400
Balanced Accuracy : 0.625
'Positive' Class : 0
有没有一种方法可以在Python中创建类似的输出? 另外,我需要一种方法来绘制ROC曲线。 请帮帮我,我是Python新手。
我使用此代码用scikit-learn
绘制混淆矩阵;
from sklearn.metrics import confusion_matrix
from matplotlib import pyplot as plt
import matplotlib.ticker as ticker
# make predictions replace clf with your trained classifier
y_pred = clf.predict(X_test)
# create the confusion matrix
conf_mat = confusion_matrix(y_true=y_test, y_pred=y_pred)
# create the axis to plot onto
fig = plt.figure()
ax = fig.add_subplot(111)
# plot the matrix
cax = ax.matshow(conf_mat, cmap=plt.cm.Blues)
fig.colorbar(cax)
# labels
plt.xlabel('Predicted')
plt.ylabel('Expected')
plt.show()