公共的抽象基类
import numpy as np from abc import ABCMeta, abstractmethod class 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] = 0 p_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 = 0 tmp[k] = w_k self.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 plt import numpy as np def 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, y def 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()
测试效果
更多机器学习代码,请访问 https://github.com/WiseDoge/plume
以上就是Python 实现 3 种回归模型(Linear Regression,Lasso,Ridge)的示例的详细内容,更多关于Python 实现 回归模型的资料请关注其它相关文章!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“Python 实现3种回归模型(Linear Regression,Lasso,Ridge)的示例”评论...
更新日志
2024年11月08日
2024年11月08日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]