Numpy系列(六)- 形状操作
2024-10-10 10:28:54
Numpy 有一个强大之处在于可以很方便的修改生成的N维数组的形状。
更改数组形状
数组具有由沿着每个轴的元素数量给出的形状:
a = np.floor(10*np.random.random((3,4)))
a
Out[181]:
array([[6., 0., 2., 1.],
[5., 2., 8., 2.],
[8., 4., 8., 4.]])
a.shape
Out[182]: (3, 4)
上面生成了一个 3x4 的数组,现在对它进行形状的改变。
a.ravel()
Out[184]: array([6., 0., 2., 1., 5., 2., 8., 2., 8., 4., 8., 4.])
a.reshape(2,6)
Out[185]:
array([[6., 0., 2., 1., 5., 2.],
[8., 2., 8., 4., 8., 4.]])
a.T
Out[186]:
array([[6., 5., 8.],
[0., 2., 4.],
[2., 8., 8.],
[1., 2., 4.]])
a.shape
Out[187]: (3, 4)
无论是ravel
、reshape
、T
,它们都不会更改原有的数组形状,都是返回一个新的数组。
使用 resize
方法可以直接修改数组本身:
a
Out[188]:
array([[6., 0., 2., 1.],
[5., 2., 8., 2.],
[8., 4., 8., 4.]])
a.resize(2,6)
a
Out[190]:
array([[6., 0., 2., 1., 5., 2.],
[8., 2., 8., 4., 8., 4.]])
技巧:在使用 reshape
时,可以将其中的一个维度指定为 -1,Numpy 会自动计算出它的真实值
a.reshape(3, -1)
Out[191]:
array([[6., 0., 2., 1.],
[5., 2., 8., 2.],
[8., 4., 8., 4.]])
将不同数组堆叠在一起
除了可以对单个数组的形状进行转换外,还可以把多个数据进行堆叠。
a = np.floor(10*np.random.random((2,2)))
a
Out[192]:
array([[0., 3.],
[1., 9.]])
b = np.floor(10*np.random.random((2,2)))
b
Out[193]:
array([[2., 8.],
[9., 7.]])
np.hstack((a,b))
Out[194]:
array([[0., 3., 2., 8.],
[1., 9., 9., 7.]])
对于2D数组来说,使用hstack
和column_stack
效果一样,对于1D数组来说,column_stack
会将1D数组作为列堆叠到2D数组中:
from numpy import newaxis
np.column_stack((a,b))
Out[195]:
array([[0., 3., 2., 8.],
[1., 9., 9., 7.]])
a = np.array([4.,2.])
b = np.array([3.,8.])
np.column_stack((a,b))
Out[196]:
array([[4., 3.],
[2., 8.]])
np.hstack((a,b)) # 一维数组的情况下,column_stack和hstack结果不一样
Out[197]: array([4., 2., 3., 8.])
a[:,newaxis]
Out[198]:
array([[4.],
[2.]])
np.column_stack((a[:,newaxis],b[:,newaxis]))
Out[199]:
array([[4., 3.],
[2., 8.]])
np.hstack((a[:,newaxis],b[:,newaxis])) # 二维数组的情况下,column_stack和hstack结果一样
Out[200]:
array([[4., 3.],
[2., 8.]])
另一方面,对于任何输入数组,函数row_stack
等效于vstack
。一般来说,对于具有两个以上维度的数组,hstack
沿第二轴堆叠,vstack
沿第一轴堆叠,concatenate
允许一个可选参数,给出串接应该发生的轴。
将一个数组分成几个较小的数组
既然可以将多个数组进行对堆叠,自然也可以将一个数组拆分成多个小数组。
使用hsplit
,可以沿其水平轴拆分数组,通过指定要返回的均匀划分的数组数量,或通过指定要在其后进行划分的列:
from pprint import pprint
a = np.floor(10*np.random.random((2,12)))
a
Out[201]:
array([[8., 5., 5., 7., 3., 5., 8., 6., 2., 8., 9., 0.],
[5., 8., 0., 0., 9., 0., 7., 5., 3., 9., 4., 8.]])
pprint(np.hsplit(a,3)) #水平切成三等分
[array([[8., 5., 5., 7.],
[5., 8., 0., 0.]]),
array([[3., 5., 8., 6.],
[9., 0., 7., 5.]]),
array([[2., 8., 9., 0.],
[3., 9., 4., 8.]])]
vsplit
沿垂直轴分割,array_split
允许指定沿哪个轴分割。
最新文章
- C#-WinForm-无边框窗体的移动和阴影-API
- Inno Setup 卸载前关闭进程或服务 x86 x64
- FZU 2147 A-B Game
- Struts2 Annotation 注解配置
- LR(1)表驱动语法分析程序
- SQL用replace替换文本部分内容
- Zeppelin添加mysql解释器
- Delphi用ADOquery主从表例子(转总结)
- iconfont 字库入门到精通
- iOS钉钉远程打卡助手(支持越狱和非越狱)
- Redis实现单点登录
- [Swift]LeetCode676. 实现一个魔法字典 | Implement Magic Dictionary
- Mina的IoBuffer改造成Netty的ByteBuff
- Python之find命令中的位置的算法
- 15. Life Cycle of the Products 产品的生命周期
- SQL Server的等待事件
- cocos2dx 3.x(for 循环让精灵从中间往上下两边排列)
- Django中ORM简介与单表数据操作
- Echarts饼图显示模板
- MVC 模式——第3章
热门文章
- Python二次编码、小数据池之心照神交
- c/c++ 多线程 等待一次性事件 异常处理
- Chrome浏览器清除缓存
- java基础:子类-父类构造器关系
- Bootstrap -- 缩略图、进度条、列表组、面板
- CSS---内外边距
- 解决Error response from daemon: Get https://registry-1.docker.io/v2/library/hello-world/manifests/
- SP11470 TTM - To the moon
- C语言作业评价标准
- 07-JavaScript之常用内置对象