sklearn中logical regression的底层代码
时间: 2024-02-19 18:04:09 浏览: 122
在sklearn中,logistic regression的底层代码主要是基于两种算法实现的:梯度下降和拟牛顿法。
1. 使用梯度下降算法实现的代码:
```python
class LogisticRegressionGD(BaseEstimator, ClassifierMixin):
def __init__(self, eta=0.1, n_iter=100):
self.eta = eta
self.n_iter = n_iter
def fit(self, X, y):
self.w_ = np.zeros(1 + X.shape[1])
self.cost_ = []
for i in range(self.n_iter):
output = self.net_input(X)
errors = (y - output)
self.w_[1:] += self.eta * X.T.dot(errors)
self.w_[0] += self.eta * errors.sum()
cost = (-y.dot(np.log(output)) - ((1 - y).dot(np.log(1 - output))))
self.cost_.append(cost)
return self
def net_input(self, X):
return np.dot(X, self.w_[1:]) + self.w_[0]
def predict(self, X):
return np.where(self.net_input(X) >= 0.0, 1, 0)
```
2. 使用拟牛顿法实现的代码:
```python
class LogisticRegressionNM(BaseEstimator, ClassifierMixin):
def __init__(self, eta=1.0, n_iter=50, random_state=1):
self.eta = eta
self.n_iter = n_iter
self.random_state = random_state
def fit(self, X, y):
rgen = np.random.RandomState(self.random_state)
self.w_ = rgen.normal(loc=0.0, scale=0.01, size=1 + X.shape[1])
self.cost_ = []
X_augmented = np.insert(X, 0, 1, axis=1)
for i in range(self.n_iter):
output = self.sigmoid(X_augmented.dot(self.w_))
errors = y - output
grad = X_augmented.T.dot(errors) / len(X)
hessian = X_augmented.T.dot(np.diag(output * (1 - output)).dot(X_augmented)) / len(X)
self.w_ += self.eta * np.linalg.inv(hessian).dot(grad)
cost = (-y.dot(np.log(output)) - ((1 - y).dot(np.log(1 - output))))
self.cost_.append(cost)
return self
def sigmoid(self, z):
return 1 / (1 + np.exp(-z))
def net_input(self, X):
return np.dot(X, self.w_[1:]) + self.w_[0]
def predict(self, X):
return np.where(self.net_input(X) >= 0.0, 1, 0)
```
以上就是sklearn中logistic regression的底层代码。其中,LogisticRegressionGD使用梯度下降算法实现,而LogisticRegressionNM使用拟牛顿法实现。
阅读全文