当前位置:网站首页>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)结果是不一样的,前者是基于转置改变维度,后者还是从前往后组装元素,要特别注意。
边栏推荐
猜你喜欢

Why is point shield cloud forced to quit playing?

Share a powerful tool for factor Mining: genetic programming

WordPress zibll sub theme 6.4.1 happy version is free of authorization

线条动画

MySQL 45 talk | 05 explain the index in simple terms (Part 2)
![[JVM] - memory partition in JVM](/img/d8/29a5dc0ff61e35d73f48effb858770.png)
[JVM] - memory partition in JVM

When excel copies the contents of a row, the columns are separated by the tab "\t"

RL 实践(0)—— 及第平台辛丑年冬赛季【Rule-based policy】

【JVM】——JVM中內存劃分

一看就会 MotionLayout使用的几种方式
随机推荐
Line animation
What are functions in C language? What is the difference between functions in programming and functions in mathematics? Understanding functions in programming languages
Programmer - Shepherd
Shutter nestedscrollview sliding folding head pull-down refresh effect
FB、WhatsApp群发消息在2022年到底有多热门?
TypeScript基础类型
How to design an awesome high concurrency architecture from scratch (recommended Collection)
数据中台:一篇带你深入浅出了解数据中台
Operation of 2022 power cable judgment question simulation examination platform
How to do a good job of dam safety monitoring
Wireless sensor network learning notes (I)
Oracle基础知识总结
分享|智慧环保-生态文明信息化解决方案(附PDF)
Determine whether an attribute exists in an object
mysql导出数据库字典成excel文件
How to do a good job of gateway high availability protection in the big promotion scenario
拉萨手风琴
Study on modified triphosphate: lumiprobe amino-11-ddutp
DPDK 源码测试时性能下降问题
How to develop the language pack in the one-to-one video chat source code