当前位置:网站首页>Use Matplotlib to simulate linear regression
Use Matplotlib to simulate linear regression
2022-07-24 17:29:00 【Program black】
First, we need two modules :
1.numpy
2.matplotlib.pylab
Installation command
pip install numpy
pip install matplotlib

The main function of linear regression is to use a linear function or expression to fit random points in discrete space , It is a predictive modeling technology .
Import module after installation :
import numpy as np
import matplotlib.pylab as plt
1. First, create data in space :
In linear space -1 To 1 In the middle of 100 A digital , And pass reshape Function to 100 That's ok 1 Columns of data , And break it up .
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 The function of is to -1 and 1 In the middle of 100 A digital , And this 100 The difference between the two numbers is the same .
STEP 2
np.hstack Stack the element array of the parameter tuple in the horizontal direction
ones_like Method returns an array of the same type as the given array , This is defined as 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() No parameters are accepted . It's just used to turn on interactive mode
plt.figure(1,figsize=(8,6)) # Create a shape
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) # Draw on the graph x,y The coordinates are discrete points
plt.plot(x,pred_y,'r-',lw=5) # Red line , Rough as 5 Fit
plt.text(0.5,0,'Loss=%.4f' %cost) # Display the loss value
plt.subplot(2,1,2) # Add a new sub module for displaying graphics
plt.cla()
plt.plot(J)
plt.pause(0.2)
plt.ioff()
plt.show()
Combine all the code , You can get the running diagram of linear regression .
The figure is dynamically displayed ,
When the number of repetitions is large enough , The loss function becomes less , The resulting straight line ( curve ) It will better fit the discrete points on the interval .
Theoretical knowledge reference :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
边栏推荐
- Internet Download Manager配置
- Pat a - correct spelling
- Memory allocation and recycling strategy
- 实习报告1——人脸三维重建方法
- [matlab]: basic knowledge learning
- JSP custom tag library -- select tag
- Getaverse,走向Web3的远方桥梁
- What is fuzzy theory, foundation and process
- 2022 牛客暑期多校 K - Link with Bracket Sequence I(线性dp)
- Buffer overflow vulnerability lab experiment record
猜你喜欢

Buffer overflow vulnerability lab experiment record

Open source Invoicing system, 10 minutes to complete, it is recommended to collect!

快速入门

地表最强程序员装备“三件套”,你知道是什么吗?
![Why can't sizeof (ARR) / size (arr[0]) be used to calculate the array length inside the called function?](/img/e6/1d2213f3eb9bd0ff537a66d2b01f98.png)
Why can't sizeof (ARR) / size (arr[0]) be used to calculate the array length inside the called function?

通道的分离与合并

Check the actual data growth of the database

Image information is displayed by browser: data:image/png; Base64, + image content

Apachecon Asia 2022 opens registration: pulsar technology issues make a big debut

The most powerful programmer on the earth is equipped with a "three piece set". Do you know what it is?
随机推荐
TCP协议调试工具TcpEngine V1.3.0使用教程
Safety: how to provide more protection for pedestrians
[matlab]: basic knowledge learning
Hcip day 3
Analog electricity - what is the resistance?
通道的分离与合并
PAT甲级——A + B 格式
An example of using viewthatfits adaptive view in swiftui 4.0
电脑监控是真的吗?4个实验一探究竟
At & T pseudo instruction and interpretation of CFI CFA
AI opportunities for operators: expand new tracks with large models
portfwd 端口转发
Internet Download Manager配置
CANN训练营学习2022第二季 模型系列 动漫风格化和AOE ATC调优
Stop littering configuration files everywhere! Try our 7-year-old solution, which is stable
Memory allocation and recycling strategy
详解 Apache Hudi Schema Evolution(模式演进)
Still shocked by the explosion in the movie? Then you must not miss this explosive plug-in of unity
The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced
Separation and merging of channels