av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:2日期: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 實現(xiàn) 3 種回歸模型(Linear Regression,Lasso,Ridge)的示例

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

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

更多機器學(xué)習(xí)代碼,請訪問 https://github.com/WiseDoge/plume

以上就是Python 實現(xiàn) 3 種回歸模型(Linear Regression,Lasso,Ridge)的示例的詳細(xì)內(nèi)容,更多關(guān)于Python 實現(xiàn) 回歸模型的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
主站蜘蛛池模板: 伊人春色av | www性| 九九热在线播放 | 中文字幕av在线 | 久久久久久久久久久久久久久久久 | 亚洲永久免费视频 | 一区视频| 中国一级黄色 | 日本免费在线观看视频 | 国产精品欧美一区二区 | 亚洲欧美在线一区 | 亚洲青涩 | 操bbbbb| 精品国产999久久久免费 | 四虎成人网 | 日韩精品视频在线播放 | 免费av一区 | 激情视频网| 狼人久久 | 国产一区二区久久 | 日本不卡二区 | 日本在线观看一区 | 欧美激情综合五月色丁香 | 男女啪啪免费视频 | 国产欧美精品一区二区三区 | 亚洲丝袜一区 | 欧美a一级 | 中文字幕精品视频 | 久久人体| 日韩网站免费观看 | 亚洲国产免费 | 欧美久久一区二区 | 婷婷色网 | 男女啪啪免费 | 久久精品久久久精品美女 | av免费观看网址 | 97国产在线视频 | 国产精品视频一区二区三区 | 欧美日日日| 天天综合影院 | 日本免费在线观看视频 |