线性回归(python实现)
2021-02-05 09:17
标签:lin image dom play 数据集 min plot python math 其中: 分类面: 最小均方差求\(\omega \, b\) 解得: 线性回归(python实现) 标签:lin image dom play 数据集 min plot python math 原文地址:https://www.cnblogs.com/liudianfengmang/p/12785094.html数据集:
单属性,二分类
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()
效果