Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python NumPy追加vs连接_Python_Numpy - Fatal编程技术网

Python NumPy追加vs连接

Python NumPy追加vs连接,python,numpy,Python,Numpy,NumPyappend和concatenate之间有什么区别 我的观察结果是,concatenate要快一点,如果未指定轴,append会使数组变平 In [52]: print a [[1 2] [3 4] [5 6] [5 6] [1 2] [3 4] [5 6] [5 6] [1 2] [3 4] [5 6] [5 6] [5 6]] In [53]: print b [[1 2] [3 4] [5 6] [5 6] [1 2] [3 4] [5 6]

NumPy
append
concatenate
之间有什么区别

我的观察结果是,
concatenate
要快一点,如果未指定轴,
append
会使数组变平

In [52]: print a
[[1 2]
 [3 4]
 [5 6]
 [5 6]
 [1 2]
 [3 4]
 [5 6]
 [5 6]
 [1 2]
 [3 4]
 [5 6]
 [5 6]
 [5 6]]

In [53]: print b
[[1 2]
 [3 4]
 [5 6]
 [5 6]
 [1 2]
 [3 4]
 [5 6]
 [5 6]
 [5 6]]

In [54]: timeit -n 10000 -r 5 np.concatenate((a, b))
10000 loops, best of 5: 2.05 µs per loop

In [55]: timeit -n 10000 -r 5 np.append(a, b, axis = 0)
10000 loops, best of 5: 2.41 µs per loop

In [58]: np.concatenate((a, b))
Out[58]: 
array([[1, 2],
       [3, 4],
       [5, 6],
       [5, 6],
       [1, 2],
       [3, 4],
       [5, 6],
       [5, 6],
       [1, 2],
       [3, 4],
       [5, 6],
       [5, 6],
       [5, 6],
       [1, 2],
       [3, 4],
       [5, 6],
       [5, 6],
       [1, 2],
       [3, 4],
       [5, 6],
       [5, 6],
       [5, 6]])

In [59]: np.append(a, b, axis = 0)
Out[59]: 
array([[1, 2],
       [3, 4],
       [5, 6],
       [5, 6],
       [1, 2],
       [3, 4],
       [5, 6],
       [5, 6],
       [1, 2],
       [3, 4],
       [5, 6],
       [5, 6],
       [5, 6],
       [1, 2],
       [3, 4],
       [5, 6],
       [5, 6],
       [1, 2],
       [3, 4],
       [5, 6],
       [5, 6],
       [5, 6]])

In [60]: np.append(a, b)
Out[60]: 
array([1, 2, 3, 4, 5, 6, 5, 6, 1, 2, 3, 4, 5, 6, 5, 6, 1, 2, 3, 4, 5, 6, 5,
       6, 5, 6, 1, 2, 3, 4, 5, 6, 5, 6, 1, 2, 3, 4, 5, 6, 5, 6, 5, 6])

np.append
使用
np.concatenate

def append(arr, values, axis=None):
    arr = asanyarray(arr)
    if axis is None:
        if arr.ndim != 1:
            arr = arr.ravel()
        values = ravel(values)
        axis = arr.ndim-1
    return concatenate((arr, values), axis=axis)

np.append
np.concatenate
中依次定义。请参见