用python帮我写一个逻辑回归的优化代码,要求分别使用梯度下降法,AdaGrad,牛顿法,随机梯度下降法等,并进行收敛性对比
时间: 2023-12-31 21:04:39 浏览: 76
好的,根据您的要求,下面是使用Python实现逻辑回归优化的代码,包括梯度下降法、Adagrad、牛顿法、随机梯度下降法等,并进行收敛性对比。
首先,我们需要导入相关的库和数据集,这里我们使用sklearn中的鸢尾花数据集作为例子:
```python
from sklearn.datasets import load_iris
import numpy as np
iris = load_iris()
X = iris.data[:, :2]
y = (iris.target != 0) * 1
```
接下来,我们定义逻辑回归模型及其相关函数:
```python
class LogisticRegression:
def __init__(self, lr=0.01, num_iter=100000, fit_intercept=True, verbose=False, optimizer='gd'):
self.lr = lr
self.num_iter = num_iter
self.fit_intercept = fit_intercept
self.verbose = verbose
self.optimizer = optimizer
def __add_intercept(self, X):
intercept = np.ones((X.shape[0], 1))
return np.concatenate((intercept, X), axis=1)
def __sigmoid(self, z):
return 1 / (1 + np.exp(-z))
def __loss(self, h, y):
return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()
def __gradient(self, X, h, y):
return (np.dot(X.T, (h - y))) / y.size
def __hessian(self, X, h):
return (X.T @ np.diag(h) @ np.diag(1 - h) @ X) / X.shape[0]
def fit(self, X, y):
if self.fit_intercept:
X = self.__add_intercept(X)
self.theta = np.zeros(X.shape[1])
for i in range(self.num_iter):
z = np.dot(X, self.theta)
h = self.__sigmoid(z)
if self.optimizer == 'gd':
gradient = self.__gradient(X, h, y)
self.theta -= self.lr * gradient
elif self.optimizer == 'sgd':
idx = np.random.randint(y.size)
gradient = self.__gradient(X[idx, :], h[idx], y[idx])
self.theta -= self.lr * gradient
elif self.optimizer == 'adagrad':
gradient = self.__gradient(X, h, y)
self.cache += gradient ** 2
self.theta -= self.lr * gradient / (np.sqrt(self.cache) + 1e-8)
elif self.optimizer == 'newton':
gradient = self.__gradient(X, h, y)
hessian = self.__hessian(X, h)
self.theta -= np.linalg.inv(hessian) @ gradient
z = np.dot(X, self.theta)
h = self.__sigmoid(z)
loss = self.__loss(h, y)
if self.verbose and i % 10000 == 0:
print(f'Loss at iteration {i}: {loss}')
def predict_prob(self, X):
if self.fit_intercept:
X = self.__add_intercept(X)
return self.__sigmoid(np.dot(X, self.theta))
def predict(self, X, threshold=0.5):
return self.predict_prob(X) >= threshold
```
上述代码中,我们定义了一个LogisticRegression类,它包含了几个私有函数,包括添加截距项、sigmoid函数、损失函数、梯度函数、Hessian矩阵函数,以及一个公有函数fit用于训练模型,一个公有函数predict用于预测结果。
接下来,我们开始进行模型训练和预测。首先,我们使用梯度下降法训练模型:
```python
model = LogisticRegression(lr=0.1, num_iter=300000, optimizer='gd')
model.fit(X, y)
```
接下来,我们使用Adagrad训练模型:
```python
model = LogisticRegression(lr=0.1, num_iter=300000, optimizer='adagrad')
model.cache = np.zeros(X.shape[1])
model.fit(X, y)
```
接下来,我们使用牛顿法训练模型:
```python
model = LogisticRegression(lr=0.1, num_iter=300000, optimizer='newton')
model.fit(X, y)
```
最后,我们使用随机梯度下降法训练模型:
```python
model = LogisticRegression(lr=0.1, num_iter=300000, optimizer='sgd')
model.fit(X, y)
```
对于以上四种优化方法,我们可以比较它们的收敛速度和准确度:
```python
import matplotlib.pyplot as plt
plt.figure(figsize=(12, 8))
plt.plot(range(model.num_iter), model.loss, label='GD')
plt.plot(range(model.num_iter), model.loss, label='Adagrad')
plt.plot(range(model.num_iter), model.loss, label='Newton')
plt.plot(range(model.num_iter), model.loss, label='SGD')
plt.legend(loc='upper right')
plt.xlabel('Iterations')
plt.ylabel('Loss')
plt.show()
```
以上代码会生成一个收敛曲线,我们可以根据曲线来观察四种优化方法的收敛速度和准确度。
以上就是使用Python实现逻辑回归优化的全部内容,希望对您有所帮助!
阅读全文