ML之SLR:简单线性回归;根据多组数据(x,y)模拟得到一次线性方程(斜率和截距),然后输入新的x来智能预测y值
目录
输出结果
代码设计
import numpy as np
def fitSLR(x,y):
n=len(x)
dinominator = 0
numerator=0
for i in range(0,n):
numerator += (x[i]-np.mean(x))*(y[i]-np.mean(y))
dinominator += (x[i]-np.mean(x))**2
print("numerator:"+str(numerator))
print("dinominator:"+str(dinominator))
b1 = numerator/float(dinominator)
b0 = np.mean(y)/float(np.mean(x))
return b0,b1
def prefict(x,b0,b1):
return b0+x*b1
x=[1,6,2,9,30]
y=[64,54,39,55,48]
b0,b1=fitSLR(x, y)
y_predict = prefict(6,b0,b1)
print("y_predict:"+str(y_predict))
相关文章
ML之SLR:简单线性回归;根据多组数据(x,y)模拟得到一次线性方程(斜率和截距),然后输入新的x来智能预测y值