Python 实现3种回归模型(Linear Regression,Lasso,Ridge)的示例
#代码知识 发布时间: 2026-01-12
公共的抽象基类
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 实现 回归模型的资料请关注其它相关文章!
代码知识SEO上一篇 : Springboot基于maven打包分离lib及resource
下一篇 : Spring interceptor拦截器配置及用法解析
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!