Numpy 之shape manipulation
Numpy除了可以定义创建一个多维数组,还可以修改已经创建的多维数组的shape即维度,修改shape的几种方法如下:
Changing the shape of an array
Numpy支持保持原来数组的shape属性,重新创建一个新的array,在新的array中修改shape
看用例,首先创建一个3*4维度的数组:
import numpy as npa = np.floor(10 * np.random.random((3,4)))
print("Array a: ", a)
将a的3*4维度属性,给变成一个一维的数组,相当于把数组给抹平
print("A shape: ", a.shape)
还支持修改reshape,将3*4维度属性的数组,修改为6*2属性的数组
print("A reshape: ",a.reshape(6,2))
还支持将矩阵转置,经过矩阵转置之后,3*4维度属性的数组,变为4*3维度属性的数组
print("A T: ",a.T)
print("A T shape: ",a.T.shape)
经过上述一系列的转换之后,a的属性不变,还是3*4维度属性数组:
print("A shape: ", a.shape)
运行结果:
Array a: [[4. 3. 4. 2.][6. 8. 3. 1.][3. 3. 7. 9.]]
********
A shape: (3, 4)
********
A ravel: [4. 3. 4. 2. 6. 8. 3. 1. 3. 3. 7. 9.]
********
A reshape: [[4. 3.][4. 2.][6. 8.][3. 1.][3. 3.][7. 9.]]
********
A T: [[4. 6. 3.][3. 8. 3.][4. 3. 7.][2. 1. 9.]]
A T shape: (4, 3)
A shape: (3, 4)
上述几个修改shape方式都是不修改旧的数组的属性,numpy resize同样支持直接修改数组的shape而不是新建一个数组,
import numpy as npa = np.floor(10 * np.random.random((3,4)))
print("Array a: ", a)
print("A shape: ", a.shape)
print("*************")
a.resize(2,6)
print("A resize(2 ,6): ", a)
print("A shape: ", a.shape)
运行结果:
Array a: [[0. 1. 9. 8.][2. 6. 7. 8.][7. 0. 3. 1.]]
A shape: (3, 4)
*************
A resize(2 ,6): [[0. 1. 9. 8. 2. 6.][7. 8. 7. 0. 3. 1.]]
A shape: (2, 6)
Stacking together different arrays
numpy支持将多个数组堆叠在一起,用例:
创建两个数组 2*2数组:
import numpy as npa = np.floor(10 * np.random.random((2,2)))
print("Array a:", a)
print("*********")
b = np.floor(10 * np.random.random((2,2)))
print("Array b :", b)
print("*********")
将两个数组垂直叠加
print("Vstack a,b :", np.vstack((a,b)))
运行结果:
Array a: [[5. 1.][7. 5.]]
*********
Array b : [[0. 8.][0. 8.]]
*********
Vstack a,b : [[5. 1.][7. 5.][0. 8.][0. 8.]]
还可以使用hstack()将多个数组按照水平方向叠加:
Array a: [[4. 7.][1. 9.]]
*********
Array b : [[6. 3.][5. 8.]]
*********
hstack a,b : [[4. 7. 6. 3.][1. 9. 5. 8.]]
numpy 还支持column_stack堆叠方式,即按照列堆叠在一起,支持将1维的数组堆叠成2维的数组,
import numpy as npa = np.array([4.,2.])
b = np.array([3.,8.])
c = np.column_stack((a,b))
print("column_stack a,b :", c)
print("hstack a,b :", np.hstack((a,b)))
此时与hstack结果不同,运行结果:
column_stack a,b : [[4. 3.][2. 8.]]
hstack a,b : [4. 2. 3. 8.]
当两个2维数组使用column_stack堆叠时
import numpy as npa = np.floor(10 * np.random.random((2,2)))
print("Array a:", a)
print("*********")
b = np.floor(10 * np.random.random((2,2)))
print("Array b :", b)
print("*********")
print("column_stack a,b :", np.column_stack((a,b)))
print("hstack a,b :", np.hstack((a,b)))
此时与hstack结果相同,运行结果
Array a: [[2. 3.][0. 9.]]
*********
Array b : [[5. 0.][3. 8.]]
*********
column_stack a,b : [[2. 3. 5. 0.][0. 9. 3. 8.]]
hstack a,b : [[2. 3. 5. 0.][0. 9. 3. 8.]]
当把一维数组转换成二维矩阵时:
import numpy as np
from numpy import newaxisa = np.array([4.,2.])
b = np.array([3.,8.])
print("newaxis a:", a[:,newaxis])
print("newaxis b:", b[:,newaxis])
print("column_stack:", np.column_stack((a[:,newaxis],b[:,newaxis])))print("hstack:",np.hstack((a[:,newaxis],b[:,newaxis])))
此时两者结果相同:
newaxis a: [[4.][2.]]
newaxis b: [[3.][8.]]
column_stack: [[4. 3.][2. 8.]]
hstack: [[4. 3.][2. 8.]]
同样ma.row_stack
函数和vstack相同,当一维到多维时,两个不同
Splitting one array into several smaller ones
除了将多个数组叠加在一起外,还可以将一个数组进行分解
使用hsplit函数可以将一个数组按照水平方向进行分解:
import numpy as npa = np.floor(10* np.random.random((2, 12)))
print("Array a:", a)
print("**********")
print("Hsplit (a,3): ", np.hsplit(a,3))
print("**********")
print("Hsplit (a,(3,4)): ", np.hsplit(a,(3,4)))
运行结果:
Array a: [[8. 0. 3. 7. 9. 9. 2. 8. 6. 8. 3. 1.][0. 8. 3. 1. 1. 3. 2. 7. 1. 6. 0. 5.]]
**********
Hsplit (a,3): [array([[8., 0., 3., 7.],[0., 8., 3., 1.]]), array([[9., 9., 2., 8.],[1., 3., 2., 7.]]), array([[6., 8., 3., 1.],[1., 6., 0., 5.]])]
**********
Hsplit (a,(3,4)): [array([[8., 0., 3.],[0., 8., 3.]]), array([[7.],[1.]]), array([[9., 9., 2., 8., 6., 8., 3., 1.],[1., 3., 2., 7., 1., 6., 0., 5.]])]
除了按照水平方向进行分解,还可以使用vsplit()函数,在垂直方向进行分解
Numpy 之shape manipulation相关推荐
- numpy 中shape的用法
numpy 中shape的用法 返回各个维度的维数. >>> import numpy as np >>> y = np.array([[1,2,3],[4,5,6 ...
- 【计算机图形学】Embedded Deformation for shape manipulation在嵌入空间中变形
Embedded Deformation for shape manipulation 发表期刊:siggraph 2007 1. 概述 本论文通过建立"deformation graph& ...
- numpy的shape用法(转http://blog.csdn.net/by_study/article/details/67633593)
https://www.bbsmax.com/A/1O5E78b7z7/ https://www.cnblogs.com/lindaxin/p/8074921.html http://blog.csd ...
- python numpy.ones(shape, dtype=None, order=’C’) empty(shape[, dtype, order]) empty_like(a) 空数组 全1数组
ones(shape[, dtype, order]) 依据给定形状和类型(shape[, dtype, order])返回一个新的元素全部为1的数组. def ones(shape, dtype=N ...
- Python | numpy库 | shape函数与reshape函数
shape:返回数据有多少行多少列. reshape():是与数组array有关的方法,用来重新组织数据 下面代码案例可供参考 一.shape:返回数据有几行几列 1.案例代码 代码如下(示例): i ...
- Python numpy库 shape属性和reshape()方法
shape是数组array的属性:reshape()是数组array的方法 shape属性可以获得当前array的形状: import numpy as npa = np.array([1, 2, 3 ...
- 关于numpy数组shape的理解 比如:(3,) (2,3) (2,3,2) 以及对维度的小认识
shape大概有3种 ------------------------------ (2,) 有一个数字,表示数组只有1个维度 '2'说明有这个维度上有2个元素 array = np.array([9 ...
- 关于Numpy数组中属性shape的理解
关于Numpy数组中属性shape的理解 概述:numpy数组都有着shape这一属性,怎么理解这一属性呢?看以下代码: # 关于numpy中shape参数的深刻理解 p1 = np.array([1 ...
- Numpy中的shape、reshape函数
shape函数可以了解数组的结构:reshape()函数改变数组的结构. 目录 1 shape()函数 2 reshape()函数 1 shape()函数 读取矩阵的长度,比如shape[0]就是读取 ...
最新文章
- 张北将成规模150万台服务器的云计算产业基地
- 身份识别协议枚举工具ident-user-enum
- MD5算法 —— C语言实现(字符串的加密)
- IAR 增加头文件注意事项
- HDU 5389 Zero Escape
- Flink 读取文本文件,聚合每一行的uid
- JVM垃圾收集和优化
- 动态链接MFC引发的血案
- MTK 驱动开发(16)---LCM 移植
- 文件存储,块存储,对象存储的区别
- C#如何卸载已安装的Windows Service服务
- MongoDB复制集全量同步改进
- 识别数学公式_新品独家发布(免费送码)丨极度公式 数学公式 OCR 识别编辑 LaTeX 公式软件...
- Spring 之bean的注入
- 高等代数——大学高等代数课程创新教材(丘维声)——1.3笔记+习题
- C 使用拉依达准则(3σ准则)剔除异常数据( Net剔除一组数据中的奇异值)
- python 华泰股票交易接口_TradeApi 自带资金管理的A股程序化交易接口
- 英特尔cpu与主板芯片组对应关系(包含12代)
- NOI Online Round 3 总结
- 互联网早报 | 8月26日 星期三 | 蚂蚁集团递交招股文件;TikTok首次披露用户数据;滴滴开辟首个欧洲市场...