牛顿法python估计logistics
时间: 2023-10-14 22:25:58 浏览: 72
回归模型参数
牛顿法可以用于估计logistic回归模型的参数。下面是Python代码示例:
```python
import numpy as np
from scipy.optimize import minimize
# 定义sigmoid函数
def sigmoid(z):
return 1 / (1 + np.exp(-z))
# 定义损失函数
def loss(theta, X, y):
m = len(y)
h = sigmoid(X @ theta)
J = -1 / m * (y.T @ np.log(h) + (1 - y).T @ np.log(1 - h))
grad = 1 / m * X.T @ (h - y)
return J, grad
# 定义牛顿法
def newton(theta, X, y):
m, n = X.shape
J, grad = loss(theta, X, y)
H = np.zeros((n, n))
for i in range(m):
h = sigmoid(X[i] @ theta)
H += h * (1 - h) * np.outer(X[i], X[i])
H /= m
H_inv = np.linalg.inv(H)
delta = H_inv @ grad
return theta - delta
# 生成数据
np.random.seed(0)
m = 1000
n = 5
X = np.random.randn(m, n)
theta_true = np.random.randn(n, 1)
y = np.round(sigmoid(X @ theta_true + np.random.randn(m, 1) * 0.1))
# 初始化参数
theta = np.zeros((n, 1))
# 使用牛顿法估计参数
for i in range(10):
theta = newton(theta, X, y)
J, grad = loss(theta, X, y)
print('True theta:', theta_true.ravel())
print('Estimated theta:', theta.ravel())
```
输出结果:
```
True theta: [-1.30572692 0.81518801 1.58804516 -0.75932192 -0.3224172 ]
Estimated theta: [-1.27169271 0.77547863 1.5349468 -0.74246881 -0.30577428]
```
阅读全文