当前位置:网站首页>使用matplotlib模拟线性回归
使用matplotlib模拟线性回归
2022-07-24 17:26:00 【程序小黑】
首先需要两个模块:
1.numpy
2.matplotlib.pylab
安装命令
pip install numpy
pip install matplotlib

线性回归的主要作用就是用一条线性的函数或者表达式来拟合在离散空间的随机点,是一种预测性的建模技术。
安装后导入模块:
import numpy as np
import matplotlib.pylab as plt
1.首先在空间中创建数据:
在线性空间中-1到1中生成100个数字,并通过reshape函数转换成100行1列的数据,并将其打乱。
x = np.linspace(-1,1,100).reshape((100,1))
m = len(x)
y = 0.8*x + 0.1* np.random.randn(m).reshape((m,1))
linespace 的作用就是在-1和1中生成100个数字,并且这100个数字之间的差是相同的。
STEP 2
np.hstack将参数元组的元素数组按水平方向进行叠加
ones_like方法返回一个和给定数组相同类型的数组,这里定义为X
w = np.zeros(2).reshape((2,1))
X = np.hstack((x,np.ones_like(x)))
STEP 3
n = 200
lr = 0.05
J = list()
plt.ion() #ion() 不接受任何参数。只是用于开启交互模式
plt.figure(1,figsize=(8,6)) #创建一个图形
for t in range(n):
pred_y = np.dot(X,w)
cost = 1/(2*m) * np.sum((np.dot(X,w)-y)**2)
J.append(cost)
dw = 1/m * np.dot(X.T,np.dot(X,w)-y)
w = w - lr * dw;
if t % 5 == 0:
plt.subplot(2,1,1)
plt.cla()
plt.scatter(x,y) #在图上绘制x,y坐标为离散点
plt.plot(x,pred_y,'r-',lw=5) #红色线条,粗为5进行拟合
plt.text(0.5,0,'Loss=%.4f' %cost) #显示损失数值
plt.subplot(2,1,2) #新增一个子模块用于显示图形
plt.cla()
plt.plot(J)
plt.pause(0.2)
plt.ioff()
plt.show()
将所有代码结合,就能够得到线性回归的运行示意图。
该图是动态展示的,
当重复次数足够大时,损失函数会变少,得到的直线(曲线)就会更加拟合区间上的离散点。
理论知识参考:https://blog.csdn.net/qq_45771939/article/details/119800382?ops_request_misc=&request_id=&biz_id=102&utm_term=%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-3-119800382.142v32pc_rank_34,185v2control&spm=1018.2226.3001.4187
边栏推荐
- opencv自带颜色操作
- 近30所高校,获教育部点名表扬!
- Exception handling - a small case that takes you to solve NullPointerException
- HCNP Routing&Switching之DHCP中继
- portfwd 端口转发
- Safety: how to provide more protection for pedestrians
- 键盘输入操作
- 量化框架backtrader之一文读懂Indicator指标
- Getaverse,走向Web3的远方桥梁
- 2022 Asia International Internet of things exhibition
猜你喜欢

Reptiles and counter crawls: an endless battle

2022 Asia International Internet of things exhibition

Logical operation of image pixels

HCNP Routing&Switching之DHCP中继

Analog electricity - what is the resistance?

Number theory division block explanation example: 2021 Shaanxi Race C

I'll teach you how to use NPs to build intranet penetration services. When you go out, you can easily connect your lightweight notebook to your home game console to play remotely

Cann training camp learns the animation stylization and AOE ATC tuning of the second season of 2022 model series

2022年最新浙江建筑安全员模拟题库及答案

NC port forwarding
随机推荐
UFW port forwarding
Open source Invoicing system, 10 minutes to complete, it is recommended to collect!
opencv自带颜色操作
Topic 6 - message queue for client communication
Coldplay weekly issue 10
The industrial information security center takes the lead in building a data circulation platform, and ant group and other manufacturers provide technical support
Atcoder beginer 202 e - count descendants (heuristic merge on heavy chain split tree for offline query)
Bring 120W goods in 15 seconds. You can also shoot such a popular video
Safety: how to provide more protection for pedestrians
Canvas from getting started to persuading friends to give up (graphic version)
Natbypass port forwarding
DHCP relay of HCNP Routing & Switching
微信朋友圈的高性能复杂度分析
socat 端口转发
Getaverse,走向Web3的远方桥梁
Can Lu Zhengyao hide from the live broadcast room dominated by Luo min?
安全:如何为行人提供更多保护
In the morning, Tencent took out 38K, which let me see the ceiling of the foundation
Logisim group experiment 10 single cycle MIPS CPU
Check the actual data growth of the database