线性回归(python实现)

2021-02-05 09:17

阅读:708

标签:lin   image   dom   play   数据集   min   plot   python   math   

数据集:

\[D=\lbrace (x_1,y_1),(x_2,y_2),...,(x_m,y_m)\rbrace \]

其中:

\[x_i = (x_{i1};x_{i2};...;x_{id}) \]

单属性,二分类

分类面:

\[f(x)= \omega x + b \]

最小均方差求\(\omega \, b\)

\[min \sum_{i=1}^m (f(x_i)-y_i)^2 \]

解得:

\[\omega = \frac{\sum_{i=1}^m y_i(x_i- \hat x)}{\sum_{i=1}^m x^2 - \frac 1m \sum_{i=1}^m(x_i)^2} \]

\[b= \frac 1m \sum_{i=1}^m(y_i - \omega x_i) \]

python程序

import numpy as np 
import matplotlib.pyplot as plt 
N = 50
X = np.random.rand(N)
Y = np.random.rand(N)
#print(X)
plt.scatter(X, Y, alpha=0.6)#散点图绘制

#求omiga
xg = 0.0 #x均值
xf = 0.0 #x平方
for i in X:
    xg += i
    xf += i**2
xg /= N
omiga_m = 0.0
for i in range(N):
    omiga_m += Y[i]*(X[i] - xg)
omiga_d = xf - (N*xg)**2/N
omiga = omiga_m/omiga_d

#求偏移b
b_temp = 0.0
for i in range(N):
    b_temp += Y[i] - omiga*X[i]
b = b_temp/N

x_linear = np.linspace(0, 1, num=50)
y_linear = [omiga*i + b for i in x_linear]
plt.plot(x_linear, y_linear, color = ‘red‘)

plt.show()

效果

技术图片

线性回归(python实现)

标签:lin   image   dom   play   数据集   min   plot   python   math   

原文地址:https://www.cnblogs.com/liudianfengmang/p/12785094.html


评论


亲,登录后才可以留言!