当前位置:网站首页>使用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
边栏推荐
- 2022 ranking list of database audit products - must see!
- Is it safe for qiniu to open an account?
- TCP protocol debugging tool tcpengine v1.3.0 tutorial
- Still using xshell? You are out, recommend a more modern terminal connection tool!
- mysql 查询某字段中以逗号分隔的字符串的方法
- Axi protocol (3): handshake mechanism and implementation details of Axi architecture
- hcip第四天笔记
- In the morning, Tencent took out 38K, which let me see the ceiling of the foundation
- Getaverse,走向Web3的远方桥梁
- 电脑监控是真的吗?4个实验一探究竟
猜你喜欢

Axi protocol (3): handshake mechanism and implementation details of Axi architecture

opencv自带颜色操作

nc 端口转发

Heuristic merging (including examples of general formula and tree heuristic merging)
![[how to optimize her] teach you how to locate unreasonable SQL? And optimize her~~~](/img/10/996d594a53d9a34a36079fed829f27.png)
[how to optimize her] teach you how to locate unreasonable SQL? And optimize her~~~

2022 牛客暑期多校 K - Link with Bracket Sequence I(线性dp)

Coldplay weekly issue 10

Getaverse,走向Web3的远方桥梁

近30所高校,获教育部点名表扬!
![[GNN report] Tencent AI Lab Xu TingYang: graph generation model and its application in molecular generation](/img/5f/c790baf8f8e62fca36fdb4492c38b2.png)
[GNN report] Tencent AI Lab Xu TingYang: graph generation model and its application in molecular generation
随机推荐
Portmap port forwarding
[matlab]: basic knowledge learning
Opencv has its own color operation
2022年最新浙江建筑安全员模拟题库及答案
Socat port forwarding
QT graphical interface beginner project - unmanned aerial vehicle group combat simulation
Wrote a few small pieces of code, broke the system, and was blasted by the boss
Rare earth Developer Conference | Apache pulsar committee Liu Dezhi shares the way of cloud native technology transformation
Apachecon Asia 2022 opens registration: pulsar technology issues make a big debut
pinia 入门及使用
Are the top ten securities companies safe and risky to open accounts?
Live review | wonderful playback of Apache pulsar meetup (including PPT download)
Open source Invoicing system, 10 minutes to complete, it is recommended to collect!
数论整除分块讲解 例题:2021陕西省赛C
Stop littering configuration files everywhere! Try our 7-year-old solution, which is stable
Pat class A - A + B format
地表最强程序员装备“三件套”,你知道是什么吗?
[数组]NC143 矩阵乘法-简单
Canvas 从入门到劝朋友放弃(图解版)
Axi protocol (2): five channels and two transactions of Axi architecture