机器学习

最简单的线性回归

2020-01-01  本文已影响0人  徐_c90e
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
%matplotlib inline

#产生一套模拟输入数据
x=np.linspace(1,10,50) #输入
noise=np.random.uniform(-2,2,size=50) #干扰量
y = x * 5 + 6 + noise #输出

#训练
liner=LinearRegression()
liner.fit(x.reshape(-1,1),y.reshape(-1,1))  # 训练

k=liner.coef_ #斜率k
b=liner.intercept_ #截距b
print (k)
print (b)
plt.plot(x,y,'o')

#测试一个输入,看看输出
y_test=liner.predict([[6]])
print('y_test=',y_test)

k= [[4.95313161]]
b= [6.02016759]


untitled.png

y_test= [[35.73895727]]

上一篇 下一篇

猜你喜欢

热点阅读