我在sklearn中运行了一些模型。这是相同的代码。
# Function for Stochastic Gradient Descent Logistic Regression with Elastic Net
def SGDlogistic(k_fold,train_X,train_Y):
"""Method to implement Multi-class SVM using
Stochastic Gradient Descent
"""
from sklearn.linear_model import SGDClassifier
scores_sgd_lr = []
for train_indices, test_indices in k_fold:
train_X_cv = train_X[train_indices]
train_Y_cv= train_Y[train_indices]
test_X_cv = train_X[test_indices]
test_Y_cv= train_Y[test_indices]
sgd_lr=SGDClassifier(loss='log',penalty='elasticnet')
scores_sgd_lr.append(sgd_lr.fit(train_X_cv,train_Y_cv).score(test_X_cv,test_Y_cv))
print("The mean accuracy of Stochastic Gradient Descent Logistic on CV data is:", np.mean(scores_sgd_lr))
return sgd_lr
def test_performance(test_X,test_Y,classifier,name):
"""This method checks the performance of each algorithm on test data."""
from sklearn import metrics
# For SGD
print ("The accuracy of "+ name + " on test data is:",classifier.score(test_X,test_Y))
print 'Classification Metrics for'
print metrics.classification_report(test_Y, classifier.predict(test_X))
print "Confusion matrix"
print metrics.confusion_matrix(test_Y, classifier.predict(test_X))
def plot_ROC(test_X,test_Y,classifier):
""" This functions plots the ROC curve of the classifier"""
from sklearn.metrics import roc_curve, auc
false_positive_rate, true_positive_rate, thresholds =roc_curve(test_Y, classifier.predict(test_X))
roc_auc= auc(false_positive_rate, true_positive_rate)
plt.title('Receiver Operating Characteristic')
plt.plot(false_positive_rate, true_positive_rate, 'b',label='AUC = %0.2f'% roc_auc)
plt.legend(loc='lower right')
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
第一个函数使用弹性净惩罚进行逻辑回归。 第二个功能是在测试数据上测试算法的性能。这给出了混淆矩阵和准确性。
plot_ROC绘制测试数据的ROC曲线。
这是我看到的。
('The accuracy of Logistic with Elastic Net on test data is:', 0.90566607467092586)
Classification Metrics for
precision recall f1-score support
0 0.91 1.00 0.95 227948
1 0.50 0.00 0.00 23743
avg / total 0.87 0.91 0.86 251691
Confusion matrix
[[227944 4]
[ 23739 4]]
(array([ 0. , 0.00001755, 1. ]),
array([ 0. , 0.00016847, 1. ]),
array([2, 1, 0]))
如果你看到,测试数据的准确度为90%甚至是混淆矩阵都表现出良好的精确度和召回率。所以这不仅仅是准确性可能会产生误导。但它给出的ROC和AUC是0.50?。那太奇怪了。它表现为按照ROC的随机猜测,而精度和混淆矩阵显示不同的图像。
帮助
编辑2:
确定。所以我添加了使用概率的代码而不是AUC中的实际分类。
这就是我现在所得到的。
如您所见AUC 0.71 。我没有为班级不平衡做任何事情。一个问题。如何将预测分数转换为SVM等的概率。目前,它仅针对日志丢失或胡贝尔丢失函数具有predict_proba。这意味着我不能超越Logistic回归来获得AUC?
答案 0 :(得分:5)
您的结果似乎表明在几乎所有情况下分类器都是预测0。
下面是一个示例,其中数据在类0中为90%,分类器始终预测为0.它看起来与您的结果非常相似。
from sklearn.metrics import confusion_matrix, classification_report
y_true = [0] * 90 + [1] * 10 # 90% Class 0, 10% class 1
y_pred = [0] * 90 + [0] * 10 # All predictions are class 0
print classification_report(y_true, y_pred)
# precision recall f1-score support
#
# 0 0.90 1.00 0.95 90
# 1 0.00 0.00 0.00 10
#
# avg / total 0.81 0.90 0.85 100
print confusion_matrix(y_true, y_pred)
#[[90 0]
# [10 0]]
print roc_auc_score(y_true, y_pred)
# 0.5
此外,对于测量AUC,您应该使用predict_proba
而不是预测标签来预测概率。
probs = classifier.predict_proba(test_X).T[1]
false_positive_rate, true_positive_rate, thresholds = \
roc_curve(test_Y, probs)