标签:img 矩阵 线性回归 random tab 一个 code 向量 --
import matplotlib.pyplot as plt
import numpy as np
x=[0.75,0.85,0.95,1.08,1.12,1.16,1.35,1.51,1.55,1.6,1.63,1.67,1.71,1.78,1.85]
y=[10,12,15,17,20,22,35,41,48,50,51,54,59,66,75]
x=np.array(x)
y=np.array(y)
plt.scatter(x,y)
plt.axis([0,2,0,100]) #axis传入坐标轴x,y的起点和终点
plt.ylabel("身高和体重的散点图") #标签
plt.show()
x_mean = np.mean(x)
y_mean = np.mean(y)
num = 0.0
d = 0.0
for x_i,y_i in zip(x,y):
#a的分子
num +=(x_i - x_mean) *(y_i-y_mean)
#a的分母
d +=(x_i - x_mean)**2
a = num / d
b = y_mean - a * x_mean
t = np.random.uniform(0,2)
g = a * t + b
print("输入的x是:",t)
print("预测的体重y是:",g)
print(a,b)
标签:img 矩阵 线性回归 random tab 一个 code 向量 --
原文地址:https://www.cnblogs.com/nanfengnan/p/14013232.html