报错日志:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-934a5ad0c6c7> in <module>
----> 1 sim.predict(np.array([x_predict]))
TypeError: predict() takes 1 positional argument but 2 were given
错误代码:
def predict(x_predict):
"""给定带预测数据集x_predict,返回其中每一个值对应的y_hat"""
assert x_predict.ndim == 1,"SimpleLinearRegression can only deal with single feature training data"
assert self.a_ is not None and self.b_ is not None,"must fit before predict"
return [self._predict(i) for i in x_predict]
原因:
没有传入self。类对象调用类内部函数时,会自动传入self参数,而代码中没有给self留位置,所以就会报错了。
改正:
def predict(self, x