简单线性回归(Simple Linear Regression)下

1、简单线性回归模型举例:

汽车卖家做电视广告数量与卖出的汽车数量:


如何训练适合简单线性回归模型的最佳回归线?


使sum of squares最小


计算



分子 = (1-2)(14-20)+(3-2)(24-20)+(2-2)(18-20)+(1-2)(17-20)+(3-2)(27-20)=6+4+0+3+7=20

分母 = (1-2)^2+(3-2)^2+(2-2)^2+(1-2)^2+(3-2)^2 = 1+1+0+1+1=4

b1 = 20/4 = 5

b0 = 20 - 5*2 = 20 - 10 =10


预测:假设有一周广告数量为6,预测的汽车销售量是多少?


x_given = 6

Y_hat = 5*6+10=40

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: ",numerator
    print "dinominaytor: ",dinominator
    b1 = numerator/float(dinominator)
    b0 = np.mean(y)/float(np.mean(x))
    return b0,b1
    
def predict(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 "intercept:",b0,"slope:",b1

x_test = 6

y_test = predict(6,b0,b1)

print "y_test:",y_test



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值