import numpy as np
import math
# 逻辑回归算法
class LogisticRegression:
def __init__(self):
"""初始化loigistic regression模型"""
self.coef_ = None # 维度
self.intercept_ = None # 截距
self._theta = None
# sigmoid函数
def _sigmoid(self, x):
y = 1 / (1 + np.exp(-x))
return y
def fit(self, x_train, y_train, eta=0.01, n_iters=1 * math.e * 4):
assert x_train.shape[0] == y_train.shape[0] # 训练长度与标签长度一致
# 计算损失函数
def J(theta, x_b, y):
p_predict = self._sigmoid(x_b.dot(theta))
try:
return -np.sum(y*np.log(p_predict) + (1 - y)* np.log(1 - p_predict)) / len(y)
except:
return float('inf')
# 求sigmoid梯度的导数
def dJ(theta, x_b, y):
x = self._sigmoid(x_b.dot(theta))
return x_b.T.dot(x-y) / len(x_b)
def gradient_descent(x_b, y, initial_theta, eta, n_iters= 1*math.e*4, epsilion=1 * math.e - 8):
theta = initial_theta
i_iter = 0
while i_iter < n_iters:
gradient = dJ(theta, x_b, y)
last_theta = theta
theta = theta - eta * gradient
i_iter += 1
if abs(J(theta, x_b, y)) - J(last_theta, x_b, y) < epsilion:
break
return theta
x_b = np.hstack([np.ones((len(x_train), 1)), x_train])
initial_theta = np.zeros((x_b.shape[1]))
self._theta = gradient_descent(x_b, y_train, initial_theta, eta, n_iters)
self.intercept_ = self._theta[0]
self.coef_ = self._theta[1:]
return self
def predict_proba(self, x_predict):
x_b = np.hstack([np.ones((len(x_predict),1)),x_predict])
return self._sigmoid(x_b.dot(self._theta))
def predict(self, x_predict):
proba = self.predict_proba(x_predict)
return np.array(proba > 0.5, dtype= 'int')
逻辑回归算法
最新推荐文章于 2022-03-08 10:36:51 发布