python 牛顿法实现逻辑回归(Logistic Regression)
程序员文章站
2022-01-07 10:38:25
本文采用的训练方法是牛顿法(newton method)。代码import numpy as npclass logisticregression(object): """ logistic regr...
本文采用的训练方法是牛顿法(newton method)。
代码
import numpy as np class logisticregression(object): """ logistic regression classifier training by newton method """ def __init__(self, error: float = 0.7, max_epoch: int = 100): """ :param error: float, if the distance between new weight and old weight is less than error, the process of traing will break. :param max_epoch: if training epoch >= max_epoch the process of traing will break. """ self.error = error self.max_epoch = max_epoch self.weight = none self.sign = np.vectorize(lambda x: 1 if x >= 0.5 else 0) def p_func(self, x_): """get p(y=1 | x) :param x_: shape = (n_samples + 1, n_features) :return: shape = (n_samples) """ tmp = np.exp(self.weight @ x_.t) return tmp / (1 + tmp) def diff(self, x_, y, p): """get derivative :param x_: shape = (n_samples, n_features + 1) :param y: shape = (n_samples) :param p: shape = (n_samples) p(y=1 | x) :return: shape = (n_features + 1) first derivative """ return -(y - p) @ x_ def hess_mat(self, x_, p): """get hessian matrix :param p: shape = (n_samples) p(y=1 | x) :return: shape = (n_features + 1, n_features + 1) second derivative """ hess = np.zeros((x_.shape[1], x_.shape[1])) for i in range(x_.shape[0]): hess += self.x_xt[i] * p[i] * (1 - p[i]) return hess def newton_method(self, x_, y): """newton method to calculate weight :param x_: shape = (n_samples + 1, n_features) :param y: shape = (n_samples) :return: none """ self.weight = np.ones(x_.shape[1]) self.x_xt = [] for i in range(x_.shape[0]): t = x_[i, :].reshape((-1, 1)) self.x_xt.append(t @ t.t) for _ in range(self.max_epoch): p = self.p_func(x_) diff = self.diff(x_, y, p) hess = self.hess_mat(x_, p) new_weight = self.weight - (np.linalg.inv(hess) @ diff.reshape((-1, 1))).flatten() if np.linalg.norm(new_weight - self.weight) <= self.error: break self.weight = new_weight def fit(self, x, y): """ :param x_: shape = (n_samples, n_features) :param y: shape = (n_samples) :return: self """ x_ = np.c_[np.ones(x.shape[0]), x] self.newton_method(x_, y) return self def predict(self, x) -> np.array: """ :param x: shape = (n_samples, n_features] :return: shape = (n_samples] """ x_ = np.c_[np.ones(x.shape[0]), x] return self.sign(self.p_func(x_))
测试代码
import matplotlib.pyplot as plt import sklearn.datasets def plot_decision_boundary(pred_func, x, y, title=none): """分类器画图函数,可画出样本点和决策边界 :param pred_func: predict函数 :param x: 训练集x :param y: 训练集y :return: none """ # set min and max values and give it some padding x_min, x_max = x[:, 0].min() - .5, x[:, 0].max() + .5 y_min, y_max = x[:, 1].min() - .5, x[:, 1].max() + .5 h = 0.01 # generate a grid of points with distance h between them xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) # predict the function value for the whole gid z = pred_func(np.c_[xx.ravel(), yy.ravel()]) z = z.reshape(xx.shape) # plot the contour and training examples plt.contourf(xx, yy, z, cmap=plt.cm.spectral) plt.scatter(x[:, 0], x[:, 1], s=40, c=y, cmap=plt.cm.spectral) if title: plt.title(title) plt.show()
效果
更多机器学习代码,请访问 https://github.com/wisedoge/plume
以上就是python 牛顿法实现逻辑回归(logistic regression)的详细内容,更多关于python 逻辑回归的资料请关注其它相关文章!