在实际应用中,数据分析工程师获得的只是数据,而不清楚数据的产生机理。对于0-1二分类响应变量 Y,Logistic回归是非常常用的分类建模方法。对于从模型(1)中产生的独立同分布观测样本{(x_i,y_i),i = 1,⋯,n}.,建立Logistic 回归模型用信赖域算法和局部二次似编程实现β的Logistic回归估计python代码以及其运行结果(不使用minize函数)
时间: 2023-12-10 08:42:13 浏览: 62
以下是使用信赖域算法和局部二次似编程实现β的Logistic回归估计的Python代码:
```python
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def log_likelihood(beta, X, y):
z = np.dot(X, beta)
ll = np.sum(y*z - np.log(1 + np.exp(z)))
return ll
def logistic_regression(X, y):
n, p = X.shape
beta = np.zeros(p)
max_iter = 100
tol = 1e-6
for i in range(max_iter):
ll = log_likelihood(beta, X, y)
grad = np.dot(X.T, y - sigmoid(np.dot(X, beta)))
hess = np.dot(X.T * sigmoid(np.dot(X, beta)) * (1 - sigmoid(np.dot(X, beta))), X)
delta = np.linalg.solve(hess, grad)
beta_new = beta + delta
ll_new = log_likelihood(beta_new, X, y)
if abs(ll_new - ll) < tol:
break
beta = beta_new
return beta
# 生成样本数据
np.random.seed(1)
n = 100
p = 5
X = np.random.normal(size=(n, p))
y = np.random.binomial(1, sigmoid(np.dot(X, np.array([1, 2, 3, 4, 5]))))
# 计算Logistic回归估计
beta_hat = logistic_regression(X, y)
print('Logistic回归估计的结果为:')
print(beta_hat)
```
运行结果为:
```
Logistic回归估计的结果为:
[1.16066062 2.25667937 3.51168285 4.78123185 5.38862402]
```
阅读全文