当前位置:网站首页>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
边栏推荐
- PAT甲级——签到与签出
- 一个实际使用SwiftUI 4.0中ViewThatFits自适应视图的例子
- Logisim group experiment 10 single cycle MIPS CPU
- Scept: consistent and strategy based trajectory prediction for planned scenarios
- Using unity to do simulation, I don't allow this chart plug-in, you don't know
- Canvas from getting started to persuading friends to give up (graphic version)
- One article of quantitative framework backtrader: understand indicator indicators
- 2022 牛客暑期多校 K - Link with Bracket Sequence I(线性dp)
- Today, I met a 38K from Tencent, which let me see the ceiling of the foundation
- Use yarn
猜你喜欢

Still using xshell? You are out, recommend a more modern terminal connection tool!

一个实际使用SwiftUI 4.0中ViewThatFits自适应视图的例子

socat 端口转发

nc 端口转发

键盘输入操作
![[wechat official account H5] authorization](/img/d1/2712f87e134c0b8b8fdeaab9e30492.png)
[wechat official account H5] authorization

Exception handling - a small case that takes you to solve NullPointerException

Three.js (7): local texture refresh

Still developing games with unity? Then you're out. Try unity to build an answer system

Image information is displayed by browser: data:image/png; Base64, + image content
随机推荐
[array]nc143 matrix multiplication - simple
[wechat official account H5] authorization
JSP custom tag library --foreach
Cann training camp learns the animation stylization and AOE ATC tuning of the second season of 2022 model series
CDN (content delivery network) content distribution network from entry to practice
One article of quantitative framework backtrader: understand indicator indicators
地表最强程序员装备“三件套”,你知道是什么吗?
An example of using viewthatfits adaptive view in swiftui 4.0
Is it safe for qiniu to open an account?
Still developing games with unity? Then you're out. Try unity to build an answer system
DHCP relay of HCNP Routing & Switching
Stop littering configuration files everywhere! Try our 7-year-old solution, which is stable
Wallys/3 × 3/2 × 2 MIMO 802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz/QCN9074
Analyze the capabilities and scenarios of Apache pulsar, a cloud native message flow system
Reptiles and counter crawls: an endless battle
Digital transformation must have digital thinking
Ipaylinks, a cross-border payment integration service, won the 3A Asia Award of treasury
Natbypass port forwarding
Canvas from getting started to persuading friends to give up (graphic version)
Transformer structure analysis -- learning notes