我试图自行建立线性回归模型。编译器向我显示类型错误:“ linear_regression”不可迭代。 这是我的代码:
from skrypt_3 import multab, sqrtab
import math, statistics
class linear_regresion:
is_fitted = False
def fit(x, y, self):
print('learning has started')
bg = len(list(x)) * sum(multab(x, y)) - sum(x) * sum(y)
bd = len(list(y)) * sum(sqrtab(x)) - math.sqrt(sum(x))
b = bg / bd
a = statistics.mean(y) - b * statistics.mean(x)
is_fitted = True
def predict(a, self):
if is_fitted == True:
return a + b * x
else:
return "model has not been fitted!"
c = [3, 3, 2, 4, 1]
d = [20, 25, 20, 30, 10]
xyz = linear_regresion()
xyz.fit(c, d)
print(xyz.predict(5))
我使用了两个功能:
def multab(x, y):
op = []
for n in range(len(x)):
op.append(x[n] * y[n])
return op
def sqrtab(x):
op = []
for n in range(len(x)):
op.append(x[n] / x[n])
return op