简单线性回归(最小二乘法)python实现

 

 

 

简单线性回归(最小二乘法)

 

0.引入依赖

In [7]:
import numpy as np
import matplotlib.pyplot as plt
 

1.导入数据

In [15]:
points = np.genfromtxt("data.csv",delimiter=",")
#points
#提取points中的两列数据,分别作为x,y
x=points[:,0];
y=points[:,1];

#用plt画出散点图
plt.scatter(x,y)
plt.show()
 
 

2.定义损失函数

In [17]:
# 损失函数是系数的函数,另外还要传入数据的x,y
def compute_cost(w,b,points):
    total_cost=0
    M =len(points)
    for i in range(M):
        x=points[i,0]
        y=points[i,1]
        total_cost += (y-w*x-b)**2
    return total_cost/M #一除都是浮点 两个除号是地板除,整型。 如 3 // 4
 

3.定义核心算法拟合函数

In [24]:
# 先定义一个求均值的函数 问题 求均值是不是可以直接用np.mean(data)来实现?
# def average(data):
#     sum=0
#     num=len(data)
#     for i in range(num):
#         sum += data[i]
#     return sum/num
# print(average(x))
# print(np.mean(x))
#打印出来结果一样,可以通用。

#定义核心拟合函数
def fit(points):
    M = len(points)
    x_bar=np.mean(points[:,0])
    sum_yx= 0
    sum_x2=0
    sum_delta =0
    for i in range(M):
        x=points[i,0]
        y=points[i,1]
        sum_yx += y*(x-x_bar)
        sum_x2 += x**2
    #根据公式计算w
    w = sum_yx/(sum_x2-M*(x_bar**2))
    
    for i in range(M):
        x=points[i,0]
        y=points[i,1] 
        sum_delta += (y-w*x)
    b = sum_delta / M
    return w,b
 

测试

In [25]:
w,b =fit(points)
In [29]:
w,b
print ("w is :",w)
print ("b is :",b)
cost = compute_cost(w,b,points)
print("cost is :" ,cost)
 
w is : 1.9842918093406656
b is : 1.299369117112415
cost is : 16659.08147458056
 

5.画出拟合曲线

In [31]:
plt.scatter(x,y)

pred_y= w*x+b

plt.plot(x,pred_y,c='r')
Out[31]:
[<matplotlib.lines.Line2D at 0x11f0446a0>]
 
In [ ]:
 
posted @ 2019-08-29 10:26  Arli  阅读(11483)  评论(0编辑  收藏  举报