8
NumPy append
とconcatenate
の違いは何ですか?NumPyを連結して連結する
私の見解では、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'が' https://github.com/numpy/numpy/blob/v1.10.0を参照してくださいnp.concatenate'のターンで定義されています/numpy/lib/function_base.py#L4098-L4150 –
[hstack/vstackとvstackを組み合わせてvs列\ _stackを使用する必要がある場合は複製可能です](http://stackoverflow.com/questions/33356442/when-should- i-use-hstack-vstack-vs-append-vs-concatenate-vs-column-stack) – Simon