分别利用梯度下降算法和牛顿法求解Logistic回归模型,在手写体数据集MINST上,对数字6识别,给出准确率,F1得分,并画出ROC曲线图,用python代码实现
时间: 2023-10-08 11:03:47 浏览: 90
首先,我们需要导入必要的库和手写体数据集MINST:
```python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, f1_score, roc_curve, auc
```
```python
digits = load_digits()
X = digits.data
y = digits.target
X_6 = X[y==6]
y_6 = y[y==6]
y_6[np.where(y_6 == 6)] = 1
y_6[np.where(y_6 != 1)] = 0
X_train, X_test, y_train, y_test = train_test_split(X_6, y_6, test_size=0.2, random_state=42)
```
进行特征归一化:
```python
X_train = (X_train - np.mean(X_train, axis=0)) / np.std(X_train, axis=0)
X_test = (X_test - np.mean(X_test, axis=0)) / np.std(X_test, axis=0)
```
定义sigmoid函数和代价函数:
```python
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def cost_function(X, y, theta):
m = y.size
h = sigmoid(X.dot(theta))
J = -1/m * (y.T.dot(np.log(h)) + (1-y).T.dot(np.log(1-h)))
grad = 1/m * (X.T.dot(h-y))
return J, grad
```
使用梯度下降法训练模型:
```python
def gradient_descent(X, y, theta, alpha, num_iters):
m = y.size
J_history = np.zeros(num_iters)
for i in range(num_iters):
J_history[i], grad = cost_function(X, y, theta)
theta -= alpha * grad
return theta, J_history
```
```python
alpha = 0.1
num_iters = 1000
X_train = np.concatenate((np.ones((X_train.shape[0], 1)), X_train), axis=1)
initial_theta = np.zeros(X_train.shape[1])
theta, J_history = gradient_descent(X_train, y_train, initial_theta, alpha, num_iters)
plt.plot(J_history)
plt.xlabel('Iterations')
plt.ylabel('Cost')
plt.show()
```
使用牛顿法训练模型:
```python
def hessian(X, theta):
m = X.shape[0]
h = sigmoid(X.dot(theta))
H = np.zeros((X.shape[1], X.shape[1]))
for i in range(m):
H += h[i]*(1-h[i])*np.outer(X[i], X[i])
return 1/m * H
def newton_method(X, y, theta, num_iters):
m = y.size
J_history = np.zeros(num_iters)
for i in range(num_iters):
J_history[i], grad = cost_function(X, y, theta)
H = hessian(X, theta)
theta -= np.linalg.inv(H).dot(grad)
return theta, J_history
```
```python
num_iters = 10
X_train = np.concatenate((np.ones((X_train.shape[0], 1)), X_train), axis=1)
initial_theta = np.zeros(X_train.shape[1])
theta, J_history = newton_method(X_train, y_train, initial_theta, num_iters)
plt.plot(J_history)
plt.xlabel('Iterations')
plt.ylabel('Cost')
plt.show()
```
使用训练好的模型在测试集上进行预测:
```python
X_test = np.concatenate((np.ones((X_test.shape[0], 1)), X_test), axis=1)
y_pred_gd = np.round(sigmoid(X_test.dot(theta)))
y_pred_nm = np.round(sigmoid(X_test.dot(theta)))
print('Gradient Descent Accuracy:', accuracy_score(y_test, y_pred_gd))
print('Newton Method Accuracy:', accuracy_score(y_test, y_pred_nm))
print('Gradient Descent F1 Score:', f1_score(y_test, y_pred_gd))
print('Newton Method F1 Score:', f1_score(y_test, y_pred_nm))
```
绘制ROC曲线图:
```python
y_scores_gd = sigmoid(X_test.dot(theta))
fpr_gd, tpr_gd, _ = roc_curve(y_test, y_scores_gd)
roc_auc_gd = auc(fpr_gd, tpr_gd)
y_scores_nm = sigmoid(X_test.dot(theta))
fpr_nm, tpr_nm, _ = roc_curve(y_test, y_scores_nm)
roc_auc_nm = auc(fpr_nm, tpr_nm)
plt.plot(fpr_gd, tpr_gd, color='darkorange', lw=2, label='Gradient Descent (area = %0.2f)' % roc_auc_gd)
plt.plot(fpr_nm, tpr_nm, color='blue', lw=2, label='Newton Method (area = %0.2f)' % roc_auc_nm)
plt.plot([0, 1], [0, 1], color='navy', lw=2, 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()
```
阅读全文