最小二乘法实现:
向量法代码实现:
简单的来说就是将最小二乘法计算分子分母的for改成使用向量法运算,使得性能大幅度的提升
与上一篇博客代码的区别在于第19、20行。
1 class SimpleLinearRegression2: 2 def __init__(self): 3 """初始化Simple Linear Regression 模型""" 4 self.a_ = None 5 self.b_ = None 6 #a和b不是用户送来的参数,是得出的结果 7 #x_train和y_train只用来提供训练,训练得出所需参数之后,数据就没用了 8 9 def fit(self, x_train, y_train): 10 """根据训练数据集x_train,y_train训练Simple Linear Regression 模型""" 11 assert x_train.ndim == 1, \ 12 "Simple Linear Regressor can only solve simple feature training data" 13 assert len(x_train) == len(y_train), \ 14 "the size of x_train must be equal to the size of y_train" 15 #算法实现代码 16 x_mean = np.mean(x_train) 17 y_mean = np.mean(y_train) 18 19 num = (x_train - x_mean).dot(y_train - y_mean) 20 d = (x_train - x_mean).dot(x_train - x_mean) 21 22 self.a_ = num / d 23 self.b_ = y_mean - self.a_ * x_mean 24 25 return self 26 27 def predict(self, x_predict): 28 """给定待预测数据集x_predict, 返回表示x_predict的结果向量""" 29 assert x_predict.ndim == 1, \ 30 "Simple Linear Regressor can only solve single feature training data" 31 assert self.a_ is not None and self.b_ is not None, \ 32 "must fit before predict" 33 34 return np.array([self._predict(x) for x in x_predict]) 35 36 def _predict(self, x_single): 37 """给定单个待预测数据x_single, 返回x_single的预测结果值""" 38 return self.a_ * x_single + self.b_ 39 40 def __repr__(self): 41 return "SimpleLinearRegression1()"
在jupyter notebook中验证一下性能:
reg1表示的是使用for循环计算线性回归的算法
reg2表示的是使用向量法计算线性回归的算法
从运行结果我们可以看出向量法对性能有大幅度的提升。