当前位置:网站首页>在毕设中学习02——numpy多维数组的切片,形态变化,维度交换
在毕设中学习02——numpy多维数组的切片,形态变化,维度交换
2022-06-11 11:09:00 【Fortunate.F】
2022.5.22
文章目录
构建三维数组,并按照指定维度输出
import numpy as np
#
a=np.arange(0,60,1,dtype=np.floating).reshape(3,4,5)
print(a)
print(a[0])
print(a[0][0])
第一个输出
[[[ 0. 1. 2. 3. 4.]
[ 5. 6. 7. 8. 9.]
[10. 11. 12. 13. 14.]
[15. 16. 17. 18. 19.]]
[[20. 21. 22. 23. 24.]
[25. 26. 27. 28. 29.]
[30. 31. 32. 33. 34.]
[35. 36. 37. 38. 39.]]
[[40. 41. 42. 43. 44.]
[45. 46. 47. 48. 49.]
[50. 51. 52. 53. 54.]
[55. 56. 57. 58. 59.]]]
第二个输出
[[ 0. 1. 2. 3. 4.]
[ 5. 6. 7. 8. 9.]
[10. 11. 12. 13. 14.]
[15. 16. 17. 18. 19.]]
第三个输出
[0. 1. 2. 3. 4.]
生成一组随机数,摆放为指定矩阵形式
a=np.random.randint(15,40,size=(10,10,3))
print(a)
print(a.size)
Python中range(start,stop,步长)
为什么Python中range(10)输出的是range(0, 10)?
因为range()函数返回的是生成器对象。
生成器对象直接打印出不来内容,只会返回对象信息。
想要看生成器具体会产生什么,可以使用list()、或者tuple()函数转换。
以 list()为例:list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list(range(10)) #从0开始到10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list(range(1,11)) #从1开始到11
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list(range(0,30,5)) #步长为 5
[0, 5, 10, 15, 20, 25]
list(range(0,30,3)) #步长为 3
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
list(range(0,-10,-1)) #步长为负数时候为从第一个数往随后一个数(输出过程和正常的相比,在坐标轴上是反方向输出的)
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
生成指定范围,指定步长的一组数
a=np.arange(1,20,2)
import numpy as np
a=np.arange(1,20,2)
print(type(a))
print(a)
print(list(a))
#输出
<class 'numpy.ndarray'>
[ 1 3 5 7 9 11 13 15 17 19]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
注意这个np.arange()方法的返回值是<class ‘numpy.ndarray’>类型的数据,不是列表
数据之间没有逗号,可以理解为是一个矩阵
所以针对这个返回可以输出其shape
可以重构其shape
print(a.shape)
print(a.reshape(2,5))
#输出
(10,)
[[ 1 3 5 7 9]
[11 13 15 17 19]]
多维数组切片——过滤信息
import numpy as np
#按照表达式j*10+i,生成6*6矩阵
a=np.array( [[j*10+i for i in range(6)]for j in range(6)] )
print(a)
#多维数组的切片操作
print(a[2,3:5])
print(a[2:5,2:5])
#输出
[[ 0 1 2 3 4 5]
[10 11 12 13 14 15]
[20 21 22 23 24 25]
[30 31 32 33 34 35]
[40 41 42 43 44 45]
[50 51 52 53 54 55]]
[23 24]
[[22 23 24]
[32 33 34]
[42 43 44]]
进程已结束,退出代码0
多维矩阵的维度顺序变换
假设 a 数组是shape为(7352, 9, 128, 1)的numpy数组
方法一:
如果想要数组变换形态,比如使它变成(9, 7352, 128, 1)可以使用transpose方法
b=a.transpose((1,0,2,3))
#解释:1-0-2-3指的就是变换数组的维度,明显正常的维度是0-1-2-3-4....
#此处:0-1交换了位置,也就是变换了第一维度和第二维度的顺序
#可用于改变数组形态方便神经网络输入
方法二:
a.swapaxes(ax1,ax2) 或者np.swapaxes(a,1,2)
多维矩阵的切片
可以获取任意维度的任意片段数据
比如这个a的第二维度的9表示数据有9个通道(就像RGB图像有3个通道)
我只要第前三个通道的数据,可以这么写
c=a[,[0:3],] c的形状就变成了(7352, 3, 128, 1)有时候需要跨通道获取数据比如我只要1,3,5,6四个通道的数据可以这么写
d=a[,[0,2,4,5],]
d的形状就变成了(7352, 4, 128, 1)
多维矩阵的形态变化
a.reshape(新的形状),返回新数组
a.resize(新的形状),改变原数组
边栏推荐
- Summary of information of main account of Chia Tai futures on Wednesday in advance
- MySQL optimized learning diary 10 - locking mechanism
- 小白在同花顺上直接开户是安全的吗?
- Processing of uci-har datasets
- 为WordPress相关日志插件增加自动缩略图功能
- 使用pydub修改wav文件的比特率,报错:C:\ProgramData\Anaconda3\lib\site-packages\pydub\utils.py:170: RuntimeWarning:
- How programmers do sidelines
- 【碎碎念】关于波长|波速|周期的想法
- The application of the spingboot+quartrz production environment supports distributed, custom corn, reflective execution of multiple tasks
- Adapter mode -- can you talk well?
猜你喜欢

施一公:我直到博士毕业,对研究也没兴趣!对未来很迷茫,也不知道将来要干什么......

After 95, programmers in big factories were sentenced for deleting databases! Dissatisfied with the leaders because the project was taken over

Command mode - attack, secret weapon

Xiao P weekly Vol.08

Adapter mode -- can you talk well?

使用Yolov5训练自己制作的数据集,快速上手

全国多年太阳辐射空间分布数据1981-2022年、气温分布数据、蒸散量数据、蒸发量数据、降雨量分布数据、日照数据、风速数据

Method of converting VOC format data set to Yolo format data set

985 University doctors became popular because of their thanks in classical Chinese! The tutor commented that he not only wrote well in sci

(key points of software engineering review) Chapter IV overall design exercises
随机推荐
Jerry's ble spp open pin_ Code function [chapter]
不做伪工作者
2022年最好的年金险产品是什么?
Droid-slam: depth vision slam for monocular and binocular rgbd cameras
Enterprise wechat applet pit avoidance guide, welcome to add...
Use pydub to modify the bit rate of the wav file, and an error is reported: c:\programdata\anaconda3\lib\site packages\pydub\utils py:170: RuntimeWarning:
使用Labelimg制作VOC数据集或yolo数据集的入门方法
Exness: the progress of Russia Ukraine negotiations is limited, and the RBA's decision remains unchanged
Using domestic MCU (national technology n32g031f8s7) to realize pwm+dma control ws2812
WordPress登录页面定制插件推荐
Want to be iron man? It is said that many big men use it to get started
How to form a good habit? By perseverance? By determination? None of them!
Content-Type: multipart/form-data; boundary=${bound}
WordPress登录页面美化插件:Login Designer推荐
Is it safe for Xiaobai to open an account directly on the flush?
Lifeifei: I am more like a scientist in physics than an engineer
Planck plan 2022 Huawei software elite challenge is coming!
小白在同花顺上直接开户是安全的吗?
没有财富就不能自由吗?
Store management skills: how to manage chain stores efficiently?