def__init__(self): self.a_ = None self.b_ = None deffit(self, x_train, y_train): assert x_train.ndim==1,\ "Simple Linear Regression can only slove single feature training data" assertlen(x_train) == len(y_train),\ "the size of x_trian must be equle to the size of y_train"
defpredict(self, x_predict): assert x_predict.ndim==1,\ "Simple Linear Regression can only slove single feature training data" assertself.a_ isnotNoneandself.b_ isnotNone ,\ "must fit before predict"
return np.array([self._predict(x) for x in x_predict]) def_predcit(self, x_single): returnself.a_*x_single + self.b_ def__repr__(self): return"SimpleLinearRegression1()"
def__init__(self): self.a_ = None self.b_ = None deffit(self, x_train, y_train): assert x_train.ndim==1,\ "Simple Linear Regression can only slove single feature training data" assertlen(x_train) == len(y_train),\ "the size of x_trian must be equle to the size of y_train"
defpredict(self, x_predict): assert x_predict.ndim==1,\ "Simple Linear Regression can only slove single feature training data" assertself.a_ isnotNoneandself.b_ isnotNone ,\ "must fit before predict"
return np.array([self._predict(x) for x in x_predict]) def_predcit(self, x_single): returnself.a_*x_single + self.b_ def__repr__(self): return"SimpleLinearRegression2()"
defmean_squared_error(y_true, y_predict): assertlen(y_true) == len(y_predict),\ "the size of y_true must be equal to the size of y_predict" return np.sum((y_predict-y_true)**2)/len(y_true)
defmean_absolute_error(y_true, y_predict): assertlen(y_true) == len(y_predict),\ "the size of y_true must be equal to the size of y_predict" return np.sum(np.abs(y_predict-y_true))/len(y_true)