在demo1 的基础上实现 线性回归的图形化,
还是解决 y=ax+b的问题。

这个demo需要引入matplotlib 这个图形库的一些知识。理解为就是作图的工具。

直接上代码:

# -- coding: utf-8 --
#  用matplotlib画图像试一下,先安装: pip install matplotlib
import matplotlib.pyplot as plt

# 构建一个图表
from sklearn.linear_model import LinearRegression

# X,Y学习数据
x = [[1],[2],[3],[4],[5],[6]]
y = [[1],[2.1],[2.9],[4.2],[5.1],[9]]

#模型学习
model = LinearRegression()
model.fit(x,y)
# 给一组X2,通过学习后的模型算出Y2.
x2 = [[0], [2.5], [5.3], [9.1]]
y2 = model.predict(x2)

# 定义图表的一些属性
plt.figure()
plt.title("LineRegrssion Chart")
plt.xlabel("x label")
plt.ylabel("y labe")
# plt.axis([0,10,0,10])   显示图表的x,y的范围,默认自动适应
plt.grid(True)
# 显示
plt.plot(x,y,'k.')
# plt.plot(x,y,'g-')
plt.plot(x2,y2,'g-')  #把学习后的数据放进去
plt.show()
Demo2- 线性回归图形化
demo2.png

相关文章:

  • 2021-06-28
  • 2022-01-15
  • 2022-01-03
  • 2022-12-23
  • 2022-12-23
  • 2022-02-01
  • 2021-06-06
  • 2021-12-05
猜你喜欢
  • 2022-01-25
  • 2021-05-20
  • 2021-06-08
  • 2021-07-22
  • 2021-10-19
  • 2021-11-06
  • 2021-09-06
相关资源
相似解决方案