数组拼接方法一
思路:首先将数组转成列表,然后利用列表的拼接函数append()、extend()等进行拼接处理,最后将列表转成数组。
示例1:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>> import numpy as np >>> a = np.array([ 1 , 2 , 5 ]) >>> b = np.array([ 10 , 12 , 15 ]) >>> a_list = list (a) >>> b_list = list (b) >>> a_list.extend(b_list) >>> a_list [ 1 , 2 , 5 , 10 , 12 , 15 ] >>> a = np.array(a_list) >>> a array([ 1 , 2 , 5 , 10 , 12 , 15 ]) |
该方法只适用于简单的一维数组拼接,由于转换过程很耗时间,对于大量数据的拼接一般不建议使用。
数组拼接方法二
思路:numpy提供了numpy.append(arr, values, axis=none)函数。对于参数规定,要么一个数组和一个数值;要么两个数组,不能三个及以上数组直接append拼接。
示例2:
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
|
>>> a = np.arange( 5 ) >>> a array([ 0 , 1 , 2 , 3 , 4 ]) >>> np.append(a, 10 ) array([ 0 , 1 , 2 , 3 , 4 , 10 ]) >>> a array([ 0 , 1 , 2 , 3 , 4 ]) >>> b = np.array([ 11 , 22 , 33 ]) >>> b array([ 11 , 22 , 33 ]) >>> np.append(a,b) array([ 0 , 1 , 2 , 3 , 4 , 11 , 22 , 33 ]) >>> a array([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]]) >>> b = np.array([[ 7 , 8 , 9 ],[ 10 , 11 , 12 ]]) >>> b array([[ 7 , 8 , 9 ], [ 10 , 11 , 12 ]]) >>> np.append(a,b) array([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ]) |
numpy的数组没有动态改变大小的功能,numpy.append()函数每次都会重新分配整个数组,并把原来的数组复制到新数组中。
数组拼接方法三
思路:numpy提供了numpy.concatenate((a1,a2,...), axis=0)函数。能够一次完成多个数组的拼接。其中a1,a2,...是数组类型的参数
示例3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
>>> a = np.array([ 1 , 2 , 3 ]) >>> b = np.array([ 11 , 22 , 33 ]) >>> c = np.array([ 44 , 55 , 66 ]) >>> np.concatenate((a,b,c),axis = 0 ) # 默认情况下,axis=0可以不写 array([ 1 , 2 , 3 , 11 , 22 , 33 , 44 , 55 , 66 ]) #对于一维数组拼接,axis的值不影响最后的结果 >>> a = np.array([[ 1 , 2 , 3 ],[ 4 , 5 , 6 ]]) >>> b = np.array([[ 11 , 21 , 31 ],[ 7 , 8 , 9 ]]) >>> np.concatenate((a,b),axis = 0 ) array([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 11 , 21 , 31 ], [ 7 , 8 , 9 ]]) >>> np.concatenate((a,b),axis = 1 ) #axis=1表示对应行的数组进行拼接 array([[ 1 , 2 , 3 , 11 , 21 , 31 ], [ 4 , 5 , 6 , 7 , 8 , 9 ]]) |
对numpy.append()和numpy.concatenate()两个函数的运行时间进行比较
示例4:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
>>> from time import clock as now >>> a = np.arange( 9999 ) >>> b = np.arange( 9999 ) >>> time1 = now() >>> c = np.append(a,b) >>> time2 = now() >>> print time2 - time1 28.2316728446 >>> a = np.arange( 9999 ) >>> b = np.arange( 9999 ) >>> time1 = now() >>> c = np.concatenate((a,b),axis = 0 ) >>> time2 = now() >>> print time2 - time1 20.3934997107 |
可知,concatenate()效率更高,适合大规模的数据拼接
ps:更多示例
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
|
import numpy as np a = np.array([[ 1 , 2 ], [ 3 , 4 ]]) a.shape out[ 3 ]: ( 2 , 2 ) b = np.array([[ 5 , 6 ]]) b.shape out[ 5 ]: ( 1 , 2 ) np.concatenate((a, b)) out[ 6 ]: array([[ 1 , 2 ], [ 3 , 4 ], [ 5 , 6 ]]) c = np.concatenate((a, b)) c.shape out[ 8 ]: ( 3 , 2 ) d = np.concatenate((a, b), axis = 0 ) d.shape out[ 10 ]: ( 3 , 2 ) e = np.concatenate((a, b), axis = 1 ) traceback (most recent call last): file "<ipython-input-11-05a280a2cb02>" , line 1 , in <module> e = np.concatenate((a, b), axis = 1 ) valueerror: all the input array dimensions except for the concatenation axis must match exactly e = np.concatenate((a, b.t), axis = 1 ) e.shape out[ 13 ]: ( 2 , 3 ) import numpy as np a = np.array([[ 1 , 2 ], [ 3 , 4 ]]) a.shape out[ 3 ]: ( 2 , 2 ) b = np.array([[ 5 , 6 ]]) b.shape out[ 5 ]: ( 1 , 2 ) np.concatenate((a, b)) out[ 6 ]: array([[ 1 , 2 ], [ 3 , 4 ], [ 5 , 6 ]]) c = np.concatenate((a, b)) c.shape out[ 8 ]: ( 3 , 2 ) d = np.concatenate((a, b), axis = 0 ) d.shape out[ 10 ]: ( 3 , 2 ) e = np.concatenate((a, b), axis = 1 ) traceback (most recent call last): file "<ipython-input-11-05a280a2cb02>" , line 1 , in <module> e = np.concatenate((a, b), axis = 1 ) valueerror: all the input array dimensions except for the concatenation axis must match exactly e = np.concatenate((a, b.t), axis = 1 ) e.shape out[ 13 ]: ( 2 , 3 ) |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_38150441/article/details/80488800