逻辑回归梯度下降代码
时间: 2024-04-21 22:21:42 浏览: 94
逻辑回归是一种常用的分类算法,梯度下降是逻辑回归中常用的优化算法。下面是逻辑回归梯度下降的代码示例:
```python
import numpy as np
class LogisticRegression:
def __init__(self, learning_rate=0.01, num_iterations=1000):
self.learning_rate = learning_rate
self.num_iterations = num_iterations
self.weights = None
self.bias = None
def sigmoid(self, z):
return 1 / (1 + np.exp(-z))
def initialize_parameters(self, n_features):
self.weights = np.zeros(n_features)
self.bias = 0
def fit(self, X, y):
n_samples, n_features = X.shape
self.initialize_parameters(n_features)
for _ in range(self.num_iterations):
linear_model = np.dot(X, self.weights) + self.bias
y_predicted = self.sigmoid(linear_model)
dw = (1 / n_samples) * np.dot(X.T, (y_predicted - y))
db = (1 / n_samples) * np.sum(y_predicted - y)
self.weights -= self.learning_rate * dw
self.bias -= self.learning_rate * db
def predict(self, X):
linear_model = np.dot(X, self.weights) + self.bias
y_predicted = self.sigmoid(linear_model)
y_predicted_cls = [1 if i > 0.5 else 0 for i in y_predicted]
return y_predicted_cls
```
上述代码实现了一个简单的逻辑回归类,其中包括了sigmoid函数、参数初始化、模型训练和预测等功能。你可以根据自己的需求进行调整和扩展。
阅读全文