用python帮我写一个逻辑回归的优化代码,要求分别使用梯度下降法,AdaGrad,牛顿法,随机梯度下降法等,并进行收敛性对比
时间: 2023-08-25 07:04:27 浏览: 107
好的,以下是一个使用Python实现逻辑回归优化的示例代码,同时包含了梯度下降法、AdaGrad、牛顿法和随机梯度下降法的实现,并进行了收敛性对比。
```python
import numpy as np
class LogisticRegression:
def __init__(self, alpha=0.01, n_iterations=1000, optimizer='gd', batch_size=32, epsilon=1e-8):
self.alpha = alpha
self.n_iterations = n_iterations
self.optimizer = optimizer
self.batch_size = batch_size
self.epsilon = epsilon
self.theta = None
def sigmoid(self, z):
return 1 / (1 + np.exp(-z))
def cost_function(self, X, y, theta):
m = len(y)
h_theta = self.sigmoid(X.dot(theta))
J = -1/m * (y.T.dot(np.log(h_theta)) + (1-y).T.dot(np.log(1-h_theta)))
grad = 1/m * (X.T.dot(h_theta - y))
return J, grad
def optimize(self, X, y):
m, n = X.shape
self.theta = np.zeros((n, 1))
J_history = []
for i in range(self.n_iterations):
if self.optimizer == 'gd':
J, grad = self.cost_function(X, y, self.theta)
self.theta -= self.alpha * grad
elif self.optimizer == 'adagrad':
J, grad = self.cost_function(X, y, self.theta)
self.epsilon = 1e-8
G = np.zeros((n, 1))
G += np.power(grad, 2)
self.theta -= (self.alpha / np.sqrt(G+self.epsilon)) * grad
elif self.optimizer == 'newton':
J, grad = self.cost_function(X, y, self.theta)
H = np.zeros((n, n))
h_theta = self.sigmoid(X.dot(self.theta))
for i in range(m):
H += (h_theta[i]*(1-h_theta[i])) * np.outer(X[i], X[i])
self.theta -= np.linalg.inv(H).dot(grad)
elif self.optimizer == 'sgd':
for j in range(0, m, self.batch_size):
X_batch = X[j:j+self.batch_size]
y_batch = y[j:j+self.batch_size]
J, grad = self.cost_function(X_batch, y_batch, self.theta)
self.theta -= self.alpha * grad
J_history.append(J)
return J_history
def predict(self, X):
if self.theta is None:
self.theta = np.zeros((X.shape[1], 1))
return np.round(self.sigmoid(X.dot(self.theta)))
```
其中,`LogisticRegression`类包含了逻辑回归的各种优化方法,参数说明如下:
- `alpha`:学习率,默认值为0.01;
- `n_iterations`:迭代次数,默认值为1000;
- `optimizer`:优化方法,可选值包括'gd'(梯度下降法)、'adagrad'(AdaGrad)、'newton'(牛顿法)和'sgd'(随机梯度下降法),默认值为'gd';
- `batch_size`:批量大小,仅在随机梯度下降法中使用,默认值为32;
- `epsilon`:数值稳定项,仅在AdaGrad中使用,默认值为1e-8。
下面是一个使用示例:
```python
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
X, y = make_classification(n_samples=1000, n_features=10, n_informative=5, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
lr = LogisticRegression(alpha=0.1, optimizer='gd')
J_history_gd = lr.optimize(X_train, y_train)
lr = LogisticRegression(alpha=0.1, optimizer='adagrad')
J_history_adagrad = lr.optimize(X_train, y_train)
lr = LogisticRegression(alpha=0.1, optimizer='newton')
J_history_newton = lr.optimize(X_train, y_train)
lr = LogisticRegression(alpha=0.1, optimizer='sgd', batch_size=32)
J_history_sgd = lr.optimize(X_train, y_train)
import matplotlib.pyplot as plt
plt.plot(J_history_gd, label='gd')
plt.plot(J_history_adagrad, label='adagrad')
plt.plot(J_history_newton, label='newton')
plt.plot(J_history_sgd, label='sgd')
plt.legend()
plt.title('Convergence Comparison')
plt.xlabel('Iteration')
plt.ylabel('Cost')
plt.show()
y_pred = lr.predict(X_test)
accuracy = np.mean(y_pred == y_test)
print(f'Test Accuracy: {accuracy*100:.2f}%')
```
在这个示例中,我们使用了sklearn自带的`make_classification`函数生成了一个二分类数据集,并将其分为训练集和测试集。然后,我们分别使用了梯度下降法、AdaGrad、牛顿法和随机梯度下降法进行优化,并记录了每次迭代的代价函数值。最后,我们将四种优化方法的收敛性进行了对比,并输出了测试集的准确率。
希望这个示例能够帮助你理解逻辑回归及其优化方法的实现和应用。
阅读全文