码迷,mamicode.com
首页 > 编程语言 > 详细

2-1 Numpy-数组

时间:2019-09-28 23:47:24      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:维度   正态分布   一维数组   ack   函数   位数组   随机   运算   ems   


(1) 数组的创建
  1 # !usr/bin/env python
  2 # Author:@vilicute
  3 import numpy as np
  4 # 1、用array创建数组并查看数组的属性
  5 arr1 = np.array([1, 2, 3, 4])  # 一维数组
  6 print("一维数组创建:arr1 = ", arr1)
  7 arr2 = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])  # 二维数组
  8 print("\n二维数组创建:arr2 = \n", arr2)
  9 # 数组属性
 10 print("数组维数:", arr2.ndim)
 11 print("数组维度:", arr2.shape)
 12 print("数组类型:", arr2.dtype)
 13 print("元素个数:", arr2.size)
 14 print("元素大小:", arr2.itemsize)
 15 arr2.shape = 4, 3  # 重新设置维度属性
 16 print("\n重置维度后的数组为:arr2_reshape = \n", arr2)
 17 ‘‘‘
 18 一维数组创建:arr1 =  [1 2 3 4]
 19 二维数组创建:arr2 = 
 20  [[ 1  2  3  4]
 21  [ 5  6  7  8]
 22  [ 9 10 11 12]]
 23 数组维数: 2
 24 数组维度: (3, 4)
 25 数组类型: int32
 26 元素个数: 12
 27 元素大小: 4
 28 重置维度后的数组为:arr2_reshape = 
 29  [[ 1  2  3]
 30  [ 4  5  6]
 31  [ 7  8  9]
 32  [10 11 12]]
 33  ‘‘‘
 34  
 35 # 2、用arange创建数组
 36 arr3 = np.arange(0, 1, 0.1)  # (初值,终值,间隔)  左闭右开
 37 print("\n等差数组:arr3 = ", arr3)
 38 ‘‘‘
 39 等差数组:arr3 =  [0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
 40 ‘‘‘
 41 # 3、用linspace创建数组
 42 arr4 = np.linspace(0, 1, 4)  # (初值,终值,个数)  等差数列
 43 print("\n特殊等差数组:arr4 = ", arr4)
 44 ‘‘‘
 45 特殊等差数组:arr4 =  [0.         0.33333333 0.66666667 1.        ]
 46 ‘‘‘
 47 # 4、用logspace创建数组
 48 arr5 = np.logspace(0, 2, 4)  # (10^0,10^2,个数)  等比数列
 49 print("\n10^等比数组:arr5 = ", arr5)
 50 ‘‘‘
 51 10^等比数组:arr5 =  [  1.           4.64158883  21.5443469  100.        ]
 52 ‘‘‘
 53 # 5、用zeros创建数组
 54 arr6 = np.zeros((3, 3))  # (a,b) 维数
 55 print("\n全0数组:arr6 = \n", arr6)
 56 ‘‘‘
 57 全0数组:arr6 = 
 58  [[0. 0. 0.]
 59   [0. 0. 0.]
 60   [0. 0. 0.]]
 61 ‘‘‘
 62 # 6、用eye创建数组
 63 arr7 = np.eye(3)  # 类似于单位矩阵
 64 print("\n单位对角数组:arr7 = \n", arr7)
 65 ‘‘‘
 66 单位对角数组:arr7 = 
 67  [[1. 0. 0.]
 68   [0. 1. 0.]
 69   [0. 0. 1.]]
 70 ‘‘‘
 71 # 7、用diag创建数组
 72 arr8 = np.diag([1, 2, 3, 4])  # [a,b,c,d] 对角元素
 73 print("\n对角数组:arr8 = \n", arr8)
 74 ‘‘‘
 75 对角数组:arr8 = 
 76  [[1 0 0 0]
 77   [0 2 0 0]
 78   [0 0 3 0]
 79   [0 0 0 4]]
 80 ‘‘‘
 81 # 8、用ones创建数组
 82 arr9 = np.ones((4, 3))  # (a,b) 维数
 83 print("\n单位数组:arr9 = \n", arr9)
 84 ‘‘‘
 85 单位数组:arr9 = 
 86  [[1. 1. 1.]
 87   [1. 1. 1.]
 88   [1. 1. 1.]
 89   [1. 1. 1.]]
 90 ‘‘‘
 91 # 9、自定义数据数组创建
 92 arr10 = np.array([("vilicute", 52, 5.02), ("shame", 55, 55.02)])
 93 print("\n自定义数据类型数组:arr10 = \n", arr10)
 94 ‘‘‘
 95 自定义数据类型数组:arr10 = 
 96  [[‘vilicute‘ ‘52‘ ‘5.02‘]
 97   [‘shame‘ ‘55‘ ‘55.02‘]]
 98 ‘‘‘
 99 # 10、生成随机数组
100 arr11 = np.random.random(10)  # 个数
101 print("\n随机数组:arr11 = \n", arr11)
102 ‘‘‘
103 随机数组:arr11 = 
104  [0.10325528 0.58512919 0.44988683 0.49719158 0.6361162  0.08344581 0.00998028 0.85750635 0.37264001 0.94651211]
105 ‘‘‘
106 # 11、生成服从均匀分布随机数
107 arr12 = np.random.rand(4, 3)
108 print("\n服从均匀分布随机数组:arr12 = \n", arr12)
109 ‘‘‘
110 服从均匀分布随机数组:arr12 = 
111  [[0.85982146 0.31343986 0.89078588]
112   [0.15717079 0.04499381 0.32277901]
113   [0.70737793 0.75456669 0.43207658]
114   [0.73633332 0.05820537 0.73123502]]
115 ‘‘‘
116 # 12、生成服从正态分布随机数
117 arr13 = np.random.randn(4, 3)
118 print("\n服从正态分布随机数组:arr13 = \n", arr13)
119 ‘‘‘
120 服从正态分布随机数组:arr13 = 
121  [[ 0.36057176 -0.71389648 -0.26165942]
122   [ 1.38415272  0.90255961 -1.42104002]
123   [ 0.48616978  1.22208226  0.65215556]
124   [ 0.2997037   1.31383623 -0.10306966]]
125 ‘‘‘
126 # 13、生成给定上下限的随机数组
127 arr14 = np.random.randint(2, 10, size=[2, 5])   # size 维数
128 print("\n给定上下限的随机数组:arr14 = \n", arr14)
129 ‘‘‘
130 给定上下限的随机数组:arr14 = 
131  [[2 8 4 4 7]
132   [3 7 5 6 5]]
133 ‘‘‘

 

(2)数组的访问
 1 # !usr/bin/env python
 2 # Author:@vilicute
 3 import numpy as np
 4 ar = np.random.randint(0,10,size = [4,5])
 5 print(ar)
 6 print(ar[1,3])    # 第二行第四列
 7 print(ar[0,2:4])  # 0行的3,4列元素
 8 print(ar[1:,2:])  # 1行2列之后的元素
 9 print(ar[:,2])    # 第3列元素
10 print(ar[2,:])    # 第3行元素
11 ‘‘‘
12 [[6 0 3 8 9]
13  [8 7 4 8 2]
14  [0 0 1 7 2]
15  [8 2 0 8 7]]
16  
17 8
18 [3 8]
19 [[4 8 2]
20  [1 7 2]
21  [0 8 7]]
22  
23 [3 4 1 0]
24 [0 0 1 7 2]
25 ‘‘‘

 

(3)数组形态的变换
 1 # !usr/bin/env python
 2 # Author:@vilicute
 3 import numpy as np
 4 arr1 = np.arange(12)
 5 print(arr1)
 6 array1 = arr1.reshape(3, 4)
 7 print("\n新的数组形态为:\n", array1)
 8 ndim = arr1.reshape(3, 4).ndim
 9 print("\n数组维度:", ndim)
10 ‘‘‘
11 [ 0  1  2  3  4  5  6  7  8  9 10 11]
12 新的数组形态为:
13  [[ 0  1  2  3]
14  [ 4  5  6  7]
15  [ 8  9 10 11]]
16 数组维度: 2
17 ‘‘‘
18 arr2 = np.random.randint(5, 15, size=[4, 5])
19 print(arr2)
20 arr2_ravel = arr2.ravel()          #数组(横向)展平
21 arr2_flatten = arr2.flatten()      #数组(横向)展平
22 arr2_flatten_F = arr2.flatten(F) #数组(纵向)展平
23 print("\n数组(横向)展平ravel(): ", arr2_ravel)
24 print("\n数组(横向)展平flatten(): ", arr2_flatten)
25 print("\n数组(纵向)展平flatten(): ", arr2_flatten_F)
26 ‘‘‘
27 [[12  5  6  8 10]
28  [11 11  8 11  7]
29  [13  7  5  5 11]
30  [ 8  6 11 13  6]]
31 数组(横向)展平ravel(): [12  5  6  8 10 11 11  8 11  7 13  7  5  5 11  8  6 11 13  6]
32 数组(横向)展平flatten(): [12  5  6  8 10 11 11  8 11  7 13  7  5  5 11  8  6 11 13  6]
33 数组(纵向)展平flatten(): [12 11 13  8  5 11  7  6  6  8  5 11  8 11  5 13 10  7 11  6]
34 ‘‘‘
35 arr3 = arr2*2
36 print("\n乘法计算:\n", arr3)
37 ‘‘‘
38 乘法计算:
39  [[24 10 12 16 20]
40   [22 22 16 22 14]
41   [26 14 10 10 22]
42   [16 12 22 26 12]]
43 ‘‘‘
44 arr_hstack = np.hstack((arr2, arr3)) #横向组合
45 arr_vstack = np.vstack((arr2, arr3)) #纵向组合
46 print("\narr2与arr3横向组合:\n", arr_hstack)
47 print("\narr2与arr3纵向组合:\n", arr_vstack)
48 ‘‘‘ 功能同上
49 arr_hstack = np.concatenate((arr2, arr3), axis=1) #横向组合
50 arr_vstack = np.concatenate((arr2, arr3), axis=0) #纵向组合
51 print("\narr2与arr3横向组合:\n", arr_hstack)
52 print("\narr2与arr3纵向组合:\n", arr_vstack)
53 ‘‘‘
54 ‘‘‘
55 arr2与arr3横向组合:
56  [[12  5  6  8 10 24 10 12 16 20]
57   [11 11  8 11  7 22 22 16 22 14]
58   [13  7  5  5 11 26 14 10 10 22]
59   [ 8  6 11 13  6 16 12 22 26 12]]
60 arr2与arr3纵向组合:
61  [[12  5  6  8 10]
62   [11 11  8 11  7]
63   [13  7  5  5 11]
64   [ 8  6 11 13  6]
65   [24 10 12 16 20]
66   [22 22 16 22 14]
67   [26 14 10 10 22]
68   [16 12 22 26 12]]
69 ‘‘‘
70 arr4 = np.arange(16).reshape(4, 4)
71 print("\narr4=\n", arr4)
72 arr_hsplit = np.hsplit(arr4, 2) #横向分割, <=>np.split(arr4,2,axis = 1)
73 arr_vsplit = np.vsplit(arr4, 2) #纵向分割, <=>np.split(arr4,2,axis = 0)
74 print("\n横向分割:\n", arr_hsplit)
75 print("\n纵向分割:\n", arr_vsplit)
76 ‘‘‘
77 arr4=
78  [[ 0  1  2  3]
79  [ 4  5  6  7]
80  [ 8  9 10 11]
81  [12 13 14 15]]
82 横向分割:
83  [array([[ 0,  1],
84          [ 4,  5],
85          [ 8,  9],
86          [12, 13]]), 
87   array([[ 2,  3],
88          [ 6,  7],
89          [10, 11],
90          [14, 15]])]
91 纵向分割:
92  [array([[0, 1, 2, 3],
93          [4, 5, 6, 7]]), 
94   array([[ 8,  9, 10, 11],
95          [12, 13, 14, 15]])]
96 ‘‘‘

 

(4)数组排序
 1 # !usr/bin/env python
 2 # Author:@vilicute
 3 import numpy as np
 4 arr1 = np.random.randint(10, 100, size=[4, 5])
 5 arr2 = np.random.randint(10, 100, size=[4, 4])
 6 arr3 = np.random.randint(10, 100, size=[4, 3])
 7 arr4 = np.array([小明, 小小, 小红, 小明, 小米, 小迭])
 8 print("\narr1=\n", arr1, "\narr2=\n", arr2, "\narr3=\n", arr3)
 9 arr1.sort(axis=1)
10 print("\n横向排序 arr1 =\n", arr1)
11 print("\narr2=\n", arr2)
12 arr2.sort(axis=0)
13 print("\n纵向排序 arr2 =\n", arr2)
14 print("\narr3=\n", arr3)
15 print("\n排序下标(按行给出):\n", arr3.argsort())
16 print("\narr4=", arr4)
17 print("\n去重:", np.unique(arr4))
18 print("\n重复:", np.tile(arr4, 2))
19 print("\n按行重复:\n", arr1.repeat(2, axis=1))
20 print("\n按列重复:\n", arr1.repeat(2, axis=0))
21 ‘‘‘
22 arr1=
23  [[24 11 78 47 65]
24  [81 54 56 90 45]
25  [75 61 50 22 23]
26  [77 64 63 84 69]] 
27 arr2=
28  [[12 23 37 32]
29  [41 20 58 77]
30  [43 76 42 97]
31  [77 53 28 90]] 
32 arr3=
33  [[53 33 81]
34  [77 22 63]
35  [90 20 66]
36  [28 61 38]]
37 横向排序 arr1 =
38  [[11 24 47 65 78]
39  [45 54 56 81 90]
40  [22 23 50 61 75]
41  [63 64 69 77 84]]
42 arr2=
43  [[12 23 37 32]
44  [41 20 58 77]
45  [43 76 42 97]
46  [77 53 28 90]]
47 纵向排序 arr2 =
48  [[12 20 28 32]
49  [41 23 37 77]
50  [43 53 42 90]
51  [77 76 58 97]]
52 arr3=
53  [[53 33 81]
54  [77 22 63]
55  [90 20 66]
56  [28 61 38]]
57 排序下标(按行给出):
58  [[1 0 2]
59  [1 2 0]
60  [1 2 0]
61  [0 2 1]]
62 arr4= [‘小明‘ ‘小小‘ ‘小红‘ ‘小明‘ ‘小米‘ ‘小迭‘]
63 去重: [‘小小‘ ‘小明‘ ‘小米‘ ‘小红‘ ‘小迭‘]
64 重复: [‘小明‘ ‘小小‘ ‘小红‘ ‘小明‘ ‘小米‘ ‘小迭‘ ‘小明‘ ‘小小‘ ‘小红‘ ‘小明‘ ‘小米‘ ‘小迭‘]
65 按行重复:
66  [[11 11 24 24 47 47 65 65 78 78]
67  [45 45 54 54 56 56 81 81 90 90]
68  [22 22 23 23 50 50 61 61 75 75]
69  [63 63 64 64 69 69 77 77 84 84]]
70 按列重复:
71  [[11 24 47 65 78]
72  [11 24 47 65 78]
73  [45 54 56 81 90]
74  [45 54 56 81 90]
75  [22 23 50 61 75]
76  [22 23 50 61 75]
77  [63 64 69 77 84]
78  [63 64 69 77 84]]
79 ‘‘‘

 

(5)数组统计
 1 # !usr/bin/env python
 2 # Author:@vilicute
 3 import numpy as np
 4 arr1 = np.random.randint(10, 100, size=[4, 5])
 5 print("\narr1=\n", arr1)
 6 arr_sum = np.sum(arr1) #求和
 7 arr_sum0 = arr1.sum(axis=0) #纵向求和
 8 arr_sum1 = arr1.sum(axis=1) #横向求和
 9 arr_mean = np.mean(arr1) #均值
10 arr_mean0 = arr1.mean(axis=0) #纵向均值
11 arr_mean1 = arr1.mean(axis=1) #横向均值
12 arr_std = np.std(arr1) #标准差
13 arr_var = np.var(arr1) #方差
14 arr_min = np.min(arr1) #最小值
15 arr_max = np.max(arr1) #最大值
16 arr_argmin = np.argmin(arr1) #最小值索引
17 arr_argmax = np.argmax(arr1) #最大值索引
18 print("\n求和:", arr_sum) 19 print("\n纵向求和:", arr_sum0) 20 print("\n横向求和:", arr_sum1) 21 print("\n均值:",arr_mean) 22 print("\n纵向均值:", arr_mean0) 23 print("\n横向均值:", arr_mean1) 24 print("\n标准差:", arr_std) 25 print("\n方差:", arr_var) 26 print("\n最小值:", arr_min) 27 print("\n最大值:", arr_max) 28 print("\n最小值索引:", arr_argmin) 29 print("\n最大值索引:", arr_argmax)
30 ‘‘‘ 31 arr1= 32 [[28 54 50 40 75] 33 [93 26 95 81 41] 34 [12 43 73 49 82] 35 [27 26 26 13 37]] 36 求和: 971 37 纵向求和: [160 149 244 183 235] 38 横向求和: [247 336 259 129] 39 均值: 48.55 40 纵向均值: [40.00 37.25 61.00 45.75 58.75] 41 横向均值: [49.4 67.2 51.8 25.8] 42 标准差: 25.437128375663793 43 方差: 647.0475000000001 44 最小值: 12 45 最大值: 95 46 最小值索引: 10 47 最大值索引: 7 48 ‘‘‘

 

(6)数组运算

 1 # !usr/bin/env python
 2 # Author:@vilicute
 3 
 4 # ufunc函数,针对数组所有元素进行操作,效率高
 5 
 6 import numpy as np
 7 
 8 arr1 = np.array([9,6,3])
 9 arr2 = np.array([2,5,6])
10 arr3 = np.array([[1,1,1,],[2,2,2],[3,3,3],[4,4,4]])
11 arr4 = np.array([[4],[5],[6],[7]])
12 
13 print("相加:",arr1+arr2)
14 print("相减:",arr1-arr2)
15 print("相乘:",arr1*arr2)
16 print("相除:",arr1/arr2)
17 print("幂运算:",arr1**arr2)
18 
19 print("比较运算:",arr1<arr2)   # >  <=  >=  ==  !=
20 print("逻辑and 和 or:",np.all(arr1 == arr2),np.any(arr1 == arr2))
21 print("\n一维广播机制相加:\n",arr1+arr3)
22 print("\n二维广播机制相加:\n",arr4+arr3)
23 
24 ‘‘‘
25 相加: [11 11  9]
26 相减: [ 7  1 -3]
27 相乘: [18 30 18]
28 相除: [4.5 1.2 0.5]
29 幂运算: [  81 7776  729]
30 比较运算: [False False  True]
31 逻辑and 和 or: False False
32 一维广播机制相加:
33  [[10  7  4]
34  [11  8  5]
35  [12  9  6]
36  [13 10  7]]
37 二维广播机制相加:
38  [[ 5  5  5]
39  [ 7  7  7]
40  [ 9  9  9]
41  [11 11 11]]
42 ‘‘‘

 

2-1 Numpy-数组

标签:维度   正态分布   一维数组   ack   函数   位数组   随机   运算   ems   

原文地址:https://www.cnblogs.com/vilicute/p/11605376.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!