用python代码,分别利用梯度下降算法和牛顿法求解Logistic回归模型,在手写体数据集MINST上,对数字6识别,给出准确率,F1得分,并画出ROC曲线图
时间: 2023-10-08 11:04:32 浏览: 93
首先需要准备MINST数据集,可以使用sklearn库中的load_digits函数加载数据集:
```python
from sklearn.datasets import load_digits
digits = load_digits()
X = digits.data
y = digits.target
# 将数字6设为正样本,其余数字设为负样本
y[y != 6] = 0
y[y == 6] = 1
```
接下来,我们可以使用梯度下降算法和牛顿法求解Logistic回归模型,可以使用scikit-learn库中的LogisticRegression函数实现:
```python
from sklearn.linear_model import LogisticRegression
# 梯度下降算法
lr_gd = LogisticRegression(solver='sag', max_iter=10000)
lr_gd.fit(X, y)
# 牛顿法
lr_newton = LogisticRegression(solver='newton-cg', max_iter=10000)
lr_newton.fit(X, y)
```
得到模型后,我们可以使用sklearn.metrics库中的相关函数来评估模型性能:
```python
from sklearn.metrics import accuracy_score, f1_score, roc_curve, auc
import matplotlib.pyplot as plt
# 计算准确率和F1得分
y_pred_gd = lr_gd.predict(X)
y_pred_newton = lr_newton.predict(X)
acc_gd = accuracy_score(y, y_pred_gd)
acc_newton = accuracy_score(y, y_pred_newton)
f1_gd = f1_score(y, y_pred_gd)
f1_newton = f1_score(y, y_pred_newton)
print('Accuracy (gradient descent):', acc_gd)
print('Accuracy (Newton method):', acc_newton)
print('F1 score (gradient descent):', f1_gd)
print('F1 score (Newton method):', f1_newton)
# 绘制ROC曲线
fpr_gd, tpr_gd, _ = roc_curve(y, lr_gd.predict_proba(X)[:, 1])
fpr_newton, tpr_newton, _ = roc_curve(y, lr_newton.predict_proba(X)[:, 1])
roc_auc_gd = auc(fpr_gd, tpr_gd)
roc_auc_newton = auc(fpr_newton, tpr_newton)
plt.figure()
lw = 2
plt.plot(fpr_gd, tpr_gd, color='darkorange',
lw=lw, label='ROC curve (gradient descent) (area = %0.2f)' % roc_auc_gd)
plt.plot(fpr_newton, tpr_newton, color='blue',
lw=lw, label='ROC curve (Newton method) (area = %0.2f)' % roc_auc_newton)
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic')
plt.legend(loc="lower right")
plt.show()
```
在本地运行代码后,可以得到准确率、F1得分和ROC曲线图。
阅读全文