import numpy as np
# 线性回归模型
class LinearRegression:
def fit(self,X,y):
# 计算最优解w=((X.T*X)^-1)*X.T*y
self.w=np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)
def predict(self,X):
return X.dot(self.w)
# 均方误差函数
def mean_squared_error(y_true,y_pred):
return np.average((y_true-y_pred)**2,axis=0)
# 决定系数的函数
def r2_score(y_true,y_pred):
numerator=(y_true-y_pred)**2
denominator=(y_true-np.average(y_true,axis=0))**2
return 1-numerator.sum(axis=0)/denominator.sum(axis=0)
08-07
8666
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)