色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Python 實現 3 種回歸模型(Linear Regression,Lasso,Ridge)的示例

瀏覽:3日期:2022-07-08 11:05:03

公共的抽象基類

import numpy as npfrom abc import ABCMeta, abstractmethodclass LinearModel(metaclass=ABCMeta): ''' Abstract base class of Linear Model. ''' def __init__(self): # Before fit or predict, please transform samples’ mean to 0, var to 1. self.scaler = StandardScaler() @abstractmethod def fit(self, X, y): '''fit func''' def predict(self, X): # before predict, you must run fit func. if not hasattr(self, ’coef_’): raise Exception(’Please run `fit` before predict’) X = self.scaler.transform(X) X = np.c_[np.ones(X.shape[0]), X] # `x @ y` == `np.dot(x, y)` return X @ self.coef_

Linear Regression

class LinearRegression(LinearModel): ''' Linear Regression. ''' def __init__(self): super().__init__() def fit(self, X, y): ''' :param X_: shape = (n_samples + 1, n_features) :param y: shape = (n_samples]) :return: self ''' self.scaler.fit(X) X = self.scaler.transform(X) X = np.c_[np.ones(X.shape[0]), X] self.coef_ = np.linalg.inv(X.T @ X) @ X.T @ y return self

Lasso

class Lasso(LinearModel): ''' Lasso Regression, training by Coordinate Descent. cost = ||X @ coef_||^2 + alpha * ||coef_||_1 ''' def __init__(self, alpha=1.0, n_iter=1000, e=0.1): self.alpha = alpha self.n_iter = n_iter self.e = e super().__init__() def fit(self, X, y): self.scaler.fit(X) X = self.scaler.transform(X) X = np.c_[np.ones(X.shape[0]), X] self.coef_ = np.zeros(X.shape[1]) for _ in range(self.n_iter): z = np.sum(X * X, axis=0) tmp = np.zeros(X.shape[1]) for k in range(X.shape[1]):wk = self.coef_[k]self.coef_[k] = 0p_k = X[:, k] @ (y - X @ self.coef_)if p_k < -self.alpha / 2: w_k = (p_k + self.alpha / 2) / z[k]elif p_k > self.alpha / 2: w_k = (p_k - self.alpha / 2) / z[k]else: w_k = 0tmp[k] = w_kself.coef_[k] = wk if np.linalg.norm(self.coef_ - tmp) < self.e:break self.coef_ = tmp return self

Ridge

class Ridge(LinearModel): ''' Ridge Regression. ''' def __init__(self, alpha=1.0): self.alpha = alpha super().__init__() def fit(self, X, y): ''' :param X_: shape = (n_samples + 1, n_features) :param y: shape = (n_samples]) :return: self ''' self.scaler.fit(X) X = self.scaler.transform(X) X = np.c_[np.ones(X.shape[0]), X] self.coef_ = np.linalg.inv( X.T @ X + self.alpha * np.eye(X.shape[1])) @ X.T @ y return self

測試代碼

import matplotlib.pyplot as pltimport numpy as npdef gen_reg_data(): X = np.arange(0, 45, 0.1) X = X + np.random.random(size=X.shape[0]) * 20 y = 2 * X + np.random.random(size=X.shape[0]) * 20 + 10 return X, ydef test_linear_regression(): clf = LinearRegression() X, y = gen_reg_data() clf.fit(X, y) plt.plot(X, y, ’.’) X_axis = np.arange(-5, 75, 0.1) plt.plot(X_axis, clf.predict(X_axis)) plt.title('Linear Regression') plt.show()def test_lasso(): clf = Lasso() X, y = gen_reg_data() clf.fit(X, y) plt.plot(X, y, ’.’) X_axis = np.arange(-5, 75, 0.1) plt.plot(X_axis, clf.predict(X_axis)) plt.title('Lasso') plt.show()def test_ridge(): clf = Ridge() X, y = gen_reg_data() clf.fit(X, y) plt.plot(X, y, ’.’) X_axis = np.arange(-5, 75, 0.1) plt.plot(X_axis, clf.predict(X_axis)) plt.title('Ridge') plt.show()

測試效果

Python 實現 3 種回歸模型(Linear Regression,Lasso,Ridge)的示例

Python 實現 3 種回歸模型(Linear Regression,Lasso,Ridge)的示例

Python 實現 3 種回歸模型(Linear Regression,Lasso,Ridge)的示例

更多機器學習代碼,請訪問 https://github.com/WiseDoge/plume

以上就是Python 實現 3 種回歸模型(Linear Regression,Lasso,Ridge)的示例的詳細內容,更多關于Python 實現 回歸模型的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
主站蜘蛛池模板: 中文 日本 免费 高清 | 亚洲欧美卡通成人制服动漫 | 怡红院在线视频全部观看 | 免费亚洲视频 | 国产自在自线午夜精品视频在 | 91亚洲成人| 免费一级欧美毛片 | 99视频有精品视频免费观看 | 宅女深夜福利视频在线 | 中文字幕精品视频在线 | 免费观看成人毛片 | 中文精品爱久久久国产 | 二区久久国产乱子伦免费精品 | 欧美日韩在线视频不卡一区二区三区 | 一区自拍 | 国产三a级日本三级日产三级 | 91精品乱码一区二区三区 | 欧美日韩一区二区三区视频 | 亚洲国产成人麻豆精品 | 91久久在线 | 国内主播福利视频在线观看 | 久久精品成人国产午夜 | 一级片日韩 | 成人免费视频软件网站 | 免费高清特黄a 大片 | 美女被强行扒开双腿激情视频 | 午夜两性视频免费看 | 99精品免费久久久久久久久日本 | 国产精品私人玩物在线观看 | 2021国产精品系列一区二区 | 手机在线看a | 日韩精品在线看 | 国产女女视屏免费 | 国产香蕉98碰碰久久人人 | 国产日韩在线观看视频 | 久久精品视频5 | 国产成年视频 | 午夜宅男在线永远免费观看网 | 亚洲情a成黄在线观看 | 国产免费一区不卡在线 | 最新69成人精品毛片 |