当前位置:网站首页>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
边栏推荐
- 滚动条调整亮度和对比度
- 一个实际使用SwiftUI 4.0中ViewThatFits自适应视图的例子
- 什么是模糊理论,基础,流程
- 安全:如何为行人提供更多保护
- 查数据库实际数据增长情况
- Extension of ES6 function
- Js实现继承的六种方式
- Programming language exercises (I)
- Colleges and universities have introduced 23 Philippine doctors with heavy funds, and the relevant departments have launched an investigation!
- The latest Zhejiang construction safety officer simulation question bank and answers in 2022
猜你喜欢

Rare earth Developer Conference | Apache pulsar committee Liu Dezhi shares the way of cloud native technology transformation

Logisim group experiment 10 single cycle MIPS CPU

ufw 端口转发

AutoCAD - join merge command

The latest Zhejiang construction safety officer simulation question bank and answers in 2022

portfwd 端口转发

Is computer monitoring true? Four experiments to find out

Number theory division block explanation example: 2021 Shaanxi Race C

An example of using viewthatfits adaptive view in swiftui 4.0

一个实际使用SwiftUI 4.0中ViewThatFits自适应视图的例子
随机推荐
[wechat official account H5] authorization
nc 端口转发
ufw 端口转发
内核开发
DHCP relay of HCNP Routing & Switching
Demonstration experiment of scrollbar for adjusting image brightness
快速入门
Internet Download Manager配置
Exception handling - a small case that takes you to solve NullPointerException
查数据库实际数据增长情况
NPM install reported -4058 error
Introduction and use of Pinia
QT graphical interface beginner project - unmanned aerial vehicle group combat simulation
近30所高校,获教育部点名表扬!
HCNP Routing&Switching之DHCP中继
Portmap port forwarding
Code random notes_ Linked list_ 707 design linked list
Transformer structure analysis -- learning notes
Mobile robot (IV) four axis aircraft
Is Shenwan Hongyuan securities' low commission account reliable, reliable and safe