import numpy as np
from sklearn import datasets
class LogisticRegression:
def __init__(self):
self._theta = None
def sigmoid(self, t):
return 1 / (1 + np.exp(-t))
def fit(self, X_train, y_train, eta=0.01, n_iters=100000):
"""
训练函数 设置学习率和最大迭代次数
:param X_train: 训练集
:param y_train:
:param eta: 学习率
:param n_iters: 最大迭代次数
:return:
"""
def loss(theta, X_b, y):
"""
loss function
交叉熵
"""
y_hat = self.sigmoid(X_b.dot(theta))
return np.sum(y * np.log(y_hat) + (1 - y) * np.log(1 - y_hat))
def dloss(theta, X_b, y):
return X_b.T.dot(self.sigmoid(X_b.dot(theta) - y)) / len(y)
def grandient_descent(X_b, y, init_theta, eta, n_iters, threshold=0.000001):
theta = init_theta
now_iter = 0
while now_iter < n_iters:
gradient = dloss(theta, X_b, y)
last_theta = theta
theta = theta - eta * gradient
if (abs(loss(theta, X_b, y) - loss(last_theta, X_b, y)) < threshold):
break
now_iter += 1
return theta
X_b = np.hstack([np.ones((len(X_train), 1)), X_train])
# X_b = X_train
init_theta = np.zeros(X_b.shape[1])
self._theta = grandient_descent(X_b, y_train, init_theta, eta, n_iters)
return self
def predict_prob(self, X_pre):
X_b = np.hstack([np.ones((len(X_pre), 1)), X_pre])
return self.sigmoid(X_b.dot(self._theta))
def predict(self, X_test):
prob = self.predict_prob(X_test)
return np.array(prob >= 0.5, dtype='int')
def score(self, X_test, y_test):
y_pre = self.predict(X_test)
correct = [1 if (a == b) else 0 for (a, b) in zip(y_pre, y_test)]
rate = (sum(correct) / len(correct))
return int(rate * 100)
if __name__ == '__main__':
iris = datasets.load_iris()
X = iris.data
y = iris.target
X = X[y < 2, :2]
y = y[y < 2]
lr = LogisticRegression()
lr.fit(X, y)
print(X.shape)
手写LogisticRegression
最新推荐文章于 2024-07-06 07:15:46 发布