(1)自定义一个用于实现多元线性回归的类:
import numpy as np
from sklearn.metrics import r2_score
class LinearRegression:
# 初始化LinearRegression模型
def __init__(self):
self.coef_ = None
self.interception_ = None
# theta为私有变量
self._theta = None
# 使用正规化方程0
def fit_normal(self, X_train, y_train):
assert X_train.shape[0] == y_train.shape[0], \
"X_train和y_train大小必须一致"
X_b = np.hstack([np.ones((len(X_train), 1)), X_train])
self._theta = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y_train)
self.interception_ = self._theta[0]
self.coef_ = self._theta[1:]
return self
def predict(self, X_predict):
assert self.interception_ is not None and self.coef_ is not None, \
"必须先拟合才能进行预测!"
assert X_predict.shape[1] == len(self.coef_), \