逻辑回归模型python代码
时间: 2023-11-08 15:56:16 浏览: 96
逻辑回归代码及实验分析
逻辑回归是一种常用的分类算法,在Python中可以使用原生Python来实现逻辑回归模型。以下是一个简单的逻辑回归模型的Python代码示例:
```python
import numpy as np
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def logistic_regression(X, y, num_iterations, learning_rate):
m, n = X.shape
theta = np.zeros((n, 1))
for i in range(num_iterations):
z = np.dot(X, theta)
h = sigmoid(z)
gradient = np.dot(X.T, (h - y)) / m
theta -= learning_rate * gradient
return theta
# 使用示例
X = np.array([[1, 2], [3, 4], [5, 6]])
y = np.array([[0], [1], [0]])
num_iterations = 1000
learning_rate = 0.01
theta = logistic_regression(X, y, num_iterations, learning_rate)
print("逻辑回归模型的参数(theta):")
print(theta)
```
阅读全文