当前位置:网站首页>numpy.reshape, numpy.transpose的理解
numpy.reshape, numpy.transpose的理解
2022-06-28 05:28:00 【微凉的衣柜】
这两个命令在变化矩阵时常用,用久了又会返回来思考二者对矩阵变换结果的影响。此处举例说明:
1.np.reshape
该命令会重排矩阵元素,可看代码:
a = np.random.randint(1, 10, [1, 6, 2] )
print(a)
print(a.reshape(2, 3, 2))
print(a.reshape(3, 2, 2))
print(a.reshape(3, 2, 2).transpose(1, 0, 2))
结果为:
#print(a)
[[[9 1]
[5 8]
[4 2]
[7 1]
[4 9]
[7 9]]]
#print(a.reshape(2, 3, 2))
[[[9 1]
[5 8]
[4 2]]
[[7 1]
[4 9]
[7 9]]]
#print(a.reshape(3, 2, 2))
[[[9 1]
[5 8]]
[[4 2]
[7 1]]
[[4 9]
[7 9]]]
#print(a.reshape(3, 2, 2).transpose(1, 0, 2))
[[[9 1]
[4 2]
[4 9]]
[[5 8]
[7 1]
[7 9]]]
reshape后实际是将原始矩阵顺序重新排列。此处还发现a.reshape(2, 3, 2).reshape(3, 2, 2)与.reshape(3, 2, 2)结果一致。此处的np.transpose放在第2节中叙述。
2.np.transpose
该函数是用于实现矩阵的转置。而对于高维矩阵则该函数也可实现维度交换,但与reshape不一样。此处是基于转置的交换维度,排列顺序不会与原始相同。先看看对于二维矩阵:
mm = np.random.randint(1, 10, [2, 3])
print(mm)#[[1 3 3]
#[9 3 8]]
print(mm.transpose(1, 0))#[[1 9]
#[3 3]
#[3 8]]
此处是基于转置的交换维度。此时可以再来理解1节中的a.reshape(3, 2, 2).transpose(1, 0, 2)。转置只发生在0和1维度中,2维度可以看做固定元素。由结果可见,transpose是将0和1维度转置,并且,a.reshape(3, 2, 2).transpose(1, 0, 2)与a.reshape(2, 3, 2)结果是不一样的,前者是基于转置改变维度,后者还是从前往后组装元素,要特别注意。
边栏推荐
- 电子邮件营销的优势在哪里?为什么shopline独立站卖家如此重视?
- Create NFS based storageclass on kubernetes
- Zzuli:1072 frog climbing well
- Opencv实现目标检测
- [leetcode] 12. Integer to Roman numeral
- Carboxylic acid study: lumiprobe sulfoacyanine 7 dicarboxylic acid
- Leecode question brushing-ii
- Docker installs mysql5.7 and starts binlog
- Function reentry caused by Keil C51's data overlaying mechanism
- 关系数据库与文档数据库对比
猜你喜欢

? How to write the position to output true

Pcr/qpcr research: lumiprobe dsgreen is used for real-time PCR
![[Verilog quick start of Niuke online question brushing series] ~ one out of four multiplexer](/img/1f/becda82f3136678c58dd8ed7bec8fe.png)
[Verilog quick start of Niuke online question brushing series] ~ one out of four multiplexer

2022 new version NFT source code source code of China meta universe digital collection art trading platform

【无标题】drv8825步进电机驱动板子原理图

Create NFS based storageclass on kubernetes

線條動畫

Yin Yang master page

RL 实践(0)—— 及第平台辛丑年冬赛季【Rule-based policy】
![[JVM] - Division de la mémoire en JVM](/img/d8/29a5dc0ff61e35d73f48effb858770.png)
[JVM] - Division de la mémoire en JVM
随机推荐
Opencv实现目标检测
JS中的链表(含leetcode例题)<持续更新~>
Hundreds of lines of code to implement a script interpreter
Office is being updated and the application cannot start normally
msa.h:没有那个文件或目录
双向电平转换电路
JSP
Biovendor sRAGE protein solution
Animation de ligne
Yunda's cloud based business in Taiwan construction 𞓜 practical school
Why does the company choose cloud database? What is its charm!
[JVM] - memory partition in JVM
如何做好水库大坝安全监测工作
Opencv实现颜色检测
How does the power outlet transmit electricity? Simple problems that have plagued my little friend for so many years
Steve Jobs' speech at Stanford University -- follow your heart
一看就会 MotionLayout使用的几种方式
Study on chemical properties and technology of biovendor rage ELISA Kit
FB、WhatsApp群发消息在2022年到底有多热门?
[C language practice - printing hollow square and its deformation]