2019-08-13
python学习
00
请注意,本文编写于 1805 天前,最后修改于 1805 天前,其中某些信息可能已经过时。

目录

1. 对象的创建
2. 查询
3. 多维数组的形态变换
1. 构建多维数组
2. 转换为一维数组,拉平
3. 转换为二维数组(12x10)
4. 转置
4. 多维数组的堆叠
1. 横向叠放
2. 竖向叠放
3. 深度叠加
5. 多维数组的拆分
1. 横向拆分
2. 纵向拆分
3. 深向拆分
6. 其他属性

1. 对象的创建

import numpy as np # 创建一维数组 a= np.arange(5) print(a) print(a.dtype) print(len(a)) print(a.shape)
[0 1 2 3 4] int64 5 (5,)
# 创建二维数组 b = np.array([np.arange(3),np.arange(3)]) print(b) # 按照列表的长度计算 print(len(b)) #打印对应的行和列 print(b.shape)
[[0 1 2] [0 1 2]] 2 (2, 3)

创建数组时候指定类型

print(np.arange(7,dtype=float)) print(np.arange(7,dtype=int))
[0. 1. 2. 3. 4. 5. 6.] [0 1 2 3 4 5 6]

2. 查询

一维数组的切片和索引(可以参考标准列表)

d = np.arange(10) print(d[4:]) print(d[:-1]) # 逆序打印 print(d[::-1]) print(d[3])
[4 5 6 7 8 9] [0 1 2 3 4 5 6 7 8] [9 8 7 6 5 4 3 2 1 0] 3

多维数据的切片和索引

c = np.array([np.arange(3),np.arange(3,6)]) print(c)
[[0 1 2] [3 4 5]]

获取元素

# 第一个元素为行 第二个元素为列 c[0][1] # c[3][4]
1
c[0]
array([0, 1, 2])

3. 多维数组的形态变换

1. 构建多维数组
b = np.arange(24).reshape(3,8) # 3列 8排 print('In:b \n',b) # 拉平 print(b.ravel()) print(b.flatten())
In:b [[ 0 1 2 3 4 5 6 7] [ 8 9 10 11 12 13 14 15] [16 17 18 19 20 21 22 23]] [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23] [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
b = np.arange(120).reshape(3,2,5,4) print(b)
[[[[ 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]] [[ 60 61 62 63] [ 64 65 66 67] [ 68 69 70 71] [ 72 73 74 75] [ 76 77 78 79]]] [[[ 80 81 82 83] [ 84 85 86 87] [ 88 89 90 91] [ 92 93 94 95] [ 96 97 98 99]] [[100 101 102 103] [104 105 106 107] [108 109 110 111] [112 113 114 115] [116 117 118 119]]]]
2. 转换为一维数组,拉平
b.ravel()
array([ 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, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119])
b.flatten()
array([ 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, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119])
3. 转换为二维数组(12x10)
b.shape = (12,10) b
array([[ 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], [ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [ 90, 91, 92, 93, 94, 95, 96, 97, 98, 99], [100, 101, 102, 103, 104, 105, 106, 107, 108, 109], [110, 111, 112, 113, 114, 115, 116, 117, 118, 119]])
4. 转置
print(b.transpose())
[[ 0 10 20 30 40 50 60 70 80 90 100 110] [ 1 11 21 31 41 51 61 71 81 91 101 111] [ 2 12 22 32 42 52 62 72 82 92 102 112] [ 3 13 23 33 43 53 63 73 83 93 103 113] [ 4 14 24 34 44 54 64 74 84 94 104 114] [ 5 15 25 35 45 55 65 75 85 95 105 115] [ 6 16 26 36 46 56 66 76 86 96 106 116] [ 7 17 27 37 47 57 67 77 87 97 107 117] [ 8 18 28 38 48 58 68 78 88 98 108 118] [ 9 19 29 39 49 59 69 79 89 99 109 119]]
print(b)
[[ 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] [ 60 61 62 63 64 65 66 67 68 69 70 71] [ 72 73 74 75 76 77 78 79 80 81 82 83] [ 84 85 86 87 88 89 90 91 92 93 94 95] [ 96 97 98 99 100 101 102 103 104 105 106 107] [108 109 110 111 112 113 114 115 116 117 118 119]]
# 数组变形,修改自身吗,同reshape b.resize(12,10) print(b)
[[ 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] [ 60 61 62 63 64 65 66 67 68 69] [ 70 71 72 73 74 75 76 77 78 79] [ 80 81 82 83 84 85 86 87 88 89] [ 90 91 92 93 94 95 96 97 98 99] [100 101 102 103 104 105 106 107 108 109] [110 111 112 113 114 115 116 117 118 119]]

4. 多维数组的堆叠

1. 横向叠放
a = np.arange(20).reshape(4,5) print(a) b = np.arange(20,40).reshape(4,5) print(b)
[[ 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]]
np.hstack((a,b)) #print((a,b))
array([[ 0, 1, 2, 3, 4, 20, 21, 22, 23, 24], [ 5, 6, 7, 8, 9, 25, 26, 27, 28, 29], [10, 11, 12, 13, 14, 30, 31, 32, 33, 34], [15, 16, 17, 18, 19, 35, 36, 37, 38, 39]])
np.concatenate((a,b),axis = 1)
array([[ 0, 1, 2, 3, 4, 20, 21, 22, 23, 24], [ 5, 6, 7, 8, 9, 25, 26, 27, 28, 29], [10, 11, 12, 13, 14, 30, 31, 32, 33, 34], [15, 16, 17, 18, 19, 35, 36, 37, 38, 39]])
2. 竖向叠放
np.vstack((a,b))
array([[ 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]])
np.concatenate((a,b),axis = 0)
array([[ 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]])
3. 深度叠加

深度叠加:除此之外,还有一种深度叠加方法,这要用到dstack()函数和一个元组。这种方法是沿着第三个坐标轴(纵向)的方向来叠加一摞数组。举例来说,可以在一个图像数据的二维数组上叠加另一幅图像的数据。

print("a:",a) print("b:",b) print("v:",np.dstack((a,b)))
a: [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14] [15 16 17 18 19]] b: [[20 21 22 23 24] [25 26 27 28 29] [30 31 32 33 34] [35 36 37 38 39]] v: [[[ 0 20] [ 1 21] [ 2 22] [ 3 23] [ 4 24]] [[ 5 25] [ 6 26] [ 7 27] [ 8 28] [ 9 29]] [[10 30] [11 31] [12 32] [13 33] [14 34]] [[15 35] [16 36] [17 37] [18 38] [19 39]]]

5. 多维数组的拆分

a = np.arange(20).reshape(4,5) a
array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]])
1. 横向拆分
np.hsplit(a,5)
[array([[ 0], [ 5], [10], [15]]), array([[ 1], [ 6], [11], [16]]), array([[ 2], [ 7], [12], [17]]), array([[ 3], [ 8], [13], [18]]), array([[ 4], [ 9], [14], [19]])]

横向拆分

np.split(a,5,axis = 1)
[array([[ 0], [ 5], [10], [15]]), array([[ 1], [ 6], [11], [16]]), array([[ 2], [ 7], [12], [17]]), array([[ 3], [ 8], [13], [18]]), array([[ 4], [ 9], [14], [19]])]
2. 纵向拆分
a = np.arange(20).reshape(4,5)
np.vsplit(a,4)
[array([[0, 1, 2, 3, 4]]), array([[5, 6, 7, 8, 9]]), array([[10, 11, 12, 13, 14]]), array([[15, 16, 17, 18, 19]])]
np.split(a,4,axis = 0)
[array([[0, 1, 2, 3, 4]]), array([[5, 6, 7, 8, 9]]), array([[10, 11, 12, 13, 14]]), array([[15, 16, 17, 18, 19]])]

纵向拆分

3. 深向拆分
c = np.arange(27).reshape(3,3,3) c
array([[[ 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]]])
np.dsplit(c,3)
[array([[[ 0], [ 3], [ 6]], [[ 9], [12], [15]], [[18], [21], [24]]]), array([[[ 1], [ 4], [ 7]], [[10], [13], [16]], [[19], [22], [25]]]), array([[[ 2], [ 5], [ 8]], [[11], [14], [17]], [[20], [23], [26]]])]

6. 其他属性

b = np.arange(24).reshape(2,12) b
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]])

秩,即轴的数量或维度的数量

print(b.ndim)
2

返回元素个数

print(b.size)
24

返回数组的维度,对于矩阵,n 行 m 列,数组的形状

print(b.shape)
(2, 12)

返回对象中每个元素的大小,以字节为单位 一个元素类型为 float64 的数组 itemsiz 属性值为 8(float64 占用 64 个 bits,每个字节长度为 8,所以 64/8,占用 8 个字节),又如,一个元素类型为 complex32 的数组 item 属性为 4(32/8)。

print(b.itemsize)
8

本文作者:mykernel

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!