岭回归是一种常用的线性回归的正则化方法,可以用来处理数据集中特征之间存在多重共线性的情况。
import numpy as np
class RidgeRegression:
def __init__(self, alpha=1.0):
self.alpha = alpha
self.coef_ = None
def fit(self, X, y):
n_samples, n_features = X.shape
# Add ridge penalty term to X^TX
XtX = np.dot(X.T, X) + self.alpha * np.eye(n_features)
# Compute X^Ty
Xty = np.dot(X.T, y)
# Solve for beta
self.coef_ = np.linalg.solve(XtX, Xty)
def predict(self, X):
if self.coef_ is None:
raise ValueError("Ridge regression model has not been trained yet.")
return np.dot(X, self.coef_)
代码解释: