Merge or merge numpy arrays

How can I join two ndarraux numpy to quickly do the following: optimized numpy without loops?

>>> a = np.random.rand(2,2)
>>> a
array([[ 0.09028802,  0.2274419 ],
       [ 0.35402772,  0.87834376]])

>>> b = np.random.rand(2,2)
>>> b
array([[ 0.4776325 ,  0.73690098],
       [ 0.69181444,  0.672248  ]])

>>> c = ???
>>> c
array([[ 0.09028802,  0.2274419, 0.4776325 ,  0.73690098],
       [ 0.09028802,  0.2274419, 0.69181444,  0.672248  ],
       [ 0.35402772,  0.87834376, 0.4776325 ,  0.73690098],
       [ 0.35402772,  0.87834376, 0.69181444,  0.672248  ]])
+4
source share
6 answers

Go through a promising solution for handling typical cases involving arrays of various shapes, with some built-in comments to explain the method used.

(1) First, we save the shapes of the input arrays.

ma,na = a.shape
mb,nb = b.shape

(2) Further up, initialize a three-dimensional array with the number of columns being the sum of the number of columns in the input arrays aand b. Use np.emptyfor this task.

out = np.empty((ma,mb,na+nb),dtype=a.dtype)

(3) 3D- "na" a a[:,None,:]. , out[:,:,:na], NumPy, , , , dims NumPy. , , /, , , .

out[:,:,:na] = a[:,None,:]

(4) b . out out[:,:,na:], .

out[:,:,na:] = b

(5) - . , . .

out.shape = (ma*mb,na+nb)

, :

ma,na = a.shape
mb,nb = b.shape
out = np.empty((ma,mb,na+nb),dtype=a.dtype)
out[:,:,:na] = a[:,None,:]
out[:,:,na:] = b
out.shape = (ma*mb,na+nb)
+2

, -, a b, . itertools numpy, numpy.hstack :

import numpy as np
from itertools import product

a = np.array([[ 0.09028802,  0.2274419 ],
              [ 0.35402772,  0.87834376]])

b = np.array([[ 0.4776325 ,  0.73690098],
              [ 0.69181444,  0.672248  ],
              [ 0.79941110,  0.52273   ]])

a_inds, b_inds = map(list, zip(*product(range(len(a)), range(len(b)))))

c = np.hstack((a[a_inds], b[b_inds]))

c of:

array([[ 0.09028802,  0.2274419 ,  0.4776325 ,  0.73690098],
       [ 0.09028802,  0.2274419 ,  0.69181444,  0.672248  ],
       [ 0.09028802,  0.2274419 ,  0.7994111 ,  0.52273   ],
       [ 0.35402772,  0.87834376,  0.4776325 ,  0.73690098],
       [ 0.35402772,  0.87834376,  0.69181444,  0.672248  ],
       [ 0.35402772,  0.87834376,  0.7994111 ,  0.52273   ]])

:

product(range(len(a)), range(len(b)) - , :

[(0, 0), (0, 1), (1, 0), (1, 1)]

- : [0, 0, 1, 1], [0, 1, 0, 1], . zip(*zipped_thing). , , tuples, :

[(0, 0, 1, 1), (0, 1, 0, 1)]

numpy , , list product.

+3

, hstack, repeat tile:

>>> a = np.arange(4).reshape(2,2)
>>> b = a+10
>>> a
array([[0, 1],
       [2, 3]])
>>> b
array([[10, 11],
       [12, 13]])
>>> np.hstack([np.repeat(a,len(a),0),np.tile(b,(len(b),1))])
array([[ 0,  1, 10, 11],
       [ 0,  1, 12, 13],
       [ 2,  3, 10, 11],
       [ 2,  3, 12, 13]])

3x3:

>>> a = np.arange(9).reshape(3,3)
>>> b = a+10
>>> np.hstack([np.repeat(a,len(a),0),np.tile(b,(len(b),1))])
array([[ 0,  1,  2, 10, 11, 12],
       [ 0,  1,  2, 13, 14, 15],
       [ 0,  1,  2, 16, 17, 18],
       [ 3,  4,  5, 10, 11, 12],
       [ 3,  4,  5, 13, 14, 15],
       [ 3,  4,  5, 16, 17, 18],
       [ 6,  7,  8, 10, 11, 12],
       [ 6,  7,  8, 13, 14, 15],
       [ 6,  7,  8, 16, 17, 18]])
+3

dstack() broadcast_arrays():

import numpy as np

a = np.random.randint(0, 10, (3, 2))
b = np.random.randint(10, 20, (4, 2))

np.dstack(np.broadcast_arrays(a[:, None], b)).reshape(-1, a.shape[-1] + b.shape[-1])
0

np.hstack, np.vstack. , . , , : np.hstack(appendedarray [:]) np.vstack(appendedarray [:])

0

, , :

a[:2],b[:2]

numpy, :

c = np.vstack(a,b)
-1

All Articles