帮我写一个利用python实现基于梯度下降的逻辑回归算法的代码
时间: 2023-06-01 08:04:49 浏览: 115
当然,我可以帮你写一个基于梯度下降的逻辑回归算法的 Python 代码。以下是代码示例:
```
import numpy as np
class LogisticRegression:
def __init__(self, learning_rate=0.01, num_iterations=100000):
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 fit(self, X, y):
num_samples, num_features = X.shape
# Initialize weights and bias
self.weights = np.zeros((num_features, 1))
self.bias = 0
# Gradient descent
for i in range(self.num_iterations):
# Forward pass
z = np.dot(X, self.weights) + self.bias
A = self.sigmoid(z)
# Compute cost and gradients
cost = (-1/num_samples) * np.sum(y*np.log(A) + (1-y)*np.log(1-A))
dw = (1/num_samples) * np.dot(X.T, (A-y))
db = (1/num_samples) * np.sum(A-y)
# Update weights and bias
self.weights -= self.learning_rate * dw
self.bias -= self.learning_rate * db
# Print cost every 1000 iterations
if i % 1000 == 0:
print(f"Cost after iteration {i}: {cost}")
def predict(self, X):
z = np.dot(X, self.weights) + self.bias
A = self.sigmoid(z)
return np.round(A)
```
在上面的代码中,我们定义了一个 `LogisticRegression` 类,其中包含了 `__init__()`、`sigmoid()`、`fit()` 和 `predict()` 方法。`__init__()` 方法用来初始化学习率和迭代次数等参数;`sigmoid()` 方法用来计算 Sigmoid 函数;`fit()` 方法用来拟合模型,通过梯度下降算法来寻找最优的权重和偏置;`predict()` 方法用来对测试集进行预测。
使用这个类,我们可以进行如下操作:
```
# Create training data
X_train = np.array([[2.7810836,2.550537003],
[1.465489372,2.362125076],
[3.396561688,4.400293529],
[1.38807019,1.850220317],
[3.06407232,3.005305973],
[7.627531214,2.759262235],
[5.332441248,2.088626775],
[6.922596716,1.77106367],
[8.675418651,-0.242068655],
[7.673756466,3.508563011]])
y_train = np.array([[0],
[0],
[0],
[0],
[0],
[1],
[1],
[1],
[1],
[1]])
# Create instance of LogisticRegression class and fit the model
lr = LogisticRegression()
lr.fit(X_train, y_train)
# Create test data
X_test = np.array([[1.465489372,2.362125076],
[8.675418651,-0.242068655],
[7.673756466,3.508563011],
[3.06407232,3.005305973]])
# Predict test data
predictions = lr.predict(X_test)
print(predictions)
```
上面的代码中,我们使用了一个简单的数据集,创建了实例化 `LogisticRegression` 类,对模型进行训练,并对测试数据进行预测。最后,我们输出了预测结果。
需要注意的是,如果你想使用本代码进行更加复杂的机器学习任务(如图像分类、自然语言处理等),需要对代码进行相应的修改和扩展。
阅读全文