How do I concatenate two one-dimensional arrays in NumPy?

爷,独闯天下 提交于 2020-07-22 21:34:03

问题


I have two arrays A = [a1, ..., an] and B = [b1, ..., bn]. I want to get new matrix C that is equal to

[[a1, b1],
 [a2, b2],
 ...
 [an, bn]]

How can I do it using numpy.concatenate?


回答1:


How about this very simple but fastest solution ?

In [73]: a = np.array([0, 1, 2, 3, 4, 5])
In [74]: b = np.array([1, 2, 3, 4, 5, 6])
In [75]: ab = np.array([a, b])
In [76]: c = ab.T

In [77]: c
Out[77]: 
array([[0, 1],
       [1, 2],
       [2, 3],
       [3, 4],
       [4, 5],
       [5, 6]])

But, as Divakar pointed out, using np.column_stack gives the answer directly like:

In [85]: np.column_stack([a, b])
Out[85]: 
array([[0, 1],
       [1, 2],
       [2, 3],
       [3, 4],
       [4, 5],
       [5, 6]])

Efficiency (in descending order)

Interestingly, my simple solution turns out to be the fastest. (little faster than np.concatenate, twice as fast as np.column_stack and thrice as fast as np.vstack)

In [86]: %timeit np.array([a, b]).T
100000 loops, best of 3: 4.44 µs per loop

In [87]: %timeit np.concatenate((a[:,None], b[:,None]), axis=1)
100000 loops, best of 3: 5.6 µs per loop

In [88]: %timeit np.column_stack([a, b])
100000 loops, best of 3: 9.5 µs per loop

In [89]: %timeit np.vstack((a, b)).T
100000 loops, best of 3: 14.7 µs per loop



回答2:


You can also use np.vstack then transpose the matrix after

import numpy as np
A = [1, 2, 3]
B = [4, 5, 6]
C = np.vstack((A, B)).T



回答3:


In [26]: A=np.arange(5)
In [27]: B=np.arange(10,15)
In [28]: np.concatenate((A[:,None], B[:,None]), axis=1)
Out[28]: 
array([[ 0, 10],
       [ 1, 11],
       [ 2, 12],
       [ 3, 13],
       [ 4, 14]])
In [29]: _.tolist()
Out[29]: [[0, 10], [1, 11], [2, 12], [3, 13], [4, 14]]

np.column_stack, np.vstack, np.stack all do the same thing, just expanding the dimensions of the arrays in different ways.

np.stack((A,B),-1) expands the arrays like I did, with a newaxis indexing.

np.column_stack((A,B)) uses:

arr = array(arr, copy=False, subok=True, ndmin=2).T

np.vstack((A,B)).T uses:

concatenate([atleast_2d(_m) for _m in tup], 0)

As a matter of curiosity, note this vstack equivalent:

In [38]: np.concatenate((A[None],B[None]))
Out[38]: 
array([[ 0,  1,  2,  3,  4],
       [10, 11, 12, 13, 14]])


来源:https://stackoverflow.com/questions/42909716/how-do-i-concatenate-two-one-dimensional-arrays-in-numpy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!