一、简单线性回归模型举例
汽车卖家做电视广告数量与卖出去的汽车数量:
1.1 列出适合简单线性回归模型的最佳回归线?
使sum of squares最小
1.2 计算
1.3 预测
假设有一周的广告数为6.预测的汽车销售量为多少?
代码:
# -*- coding:utf-8 -*- #简单线性回归:只有一个自变量 y=k*x+b 预测使得(y-y*)^2最小 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)-(b1*(np.mean(x))) return b0,b1 # y = b0 + x*b1 def prefict(x, b0, b1): return b0 + x*b1 x=[1,3,2,1,3] y=[14,24,18,17,27] b0,b1=fitSLR(x,y) print("b0:",b0,"b1:",b1) y_predict = prefict(6, b0, b1) print("y_predict:" + str(y_predict))
结果:
numerator:20.0 dinominator:4.0 b0: 10.0 b1: 5.0 y_predict:40.0
原文地址:https://www.cnblogs.com/lyywj170403/p/10452488.html
时间: 2024-10-09 17:31:15