Massive stop-drive arrays without additional checks

I have two arrays of Numpy labelsand more_labels. In one case, both 1D arrays having the shapes (m,) and (n), in the other case both arrays are two-dimensional, have the shapes (m, k) and (n, k). I would like to combine them so that the resulting array has the form (m + n,) in the 1D case or (m + n, k) in the 2D case.

Currently, I have to handle two cases separately, for example:

if(labels.ndim > 1):
    numpy.vstack(labels,more_labels)
else
    numpy.hstack(labels,more_labels)

Is there a Numpy method to handle both cases together?

+4
source share
1 answer

np.concatenate() . , , axis , 0.

numpy.concatenate((a1, a2, ...), axis=0)

.

:

n [18]: a = np.array([1,2, 3])

In [19]: b = np.array([0,0, 3])

In [20]: np.hstack((a, b))
Out[20]: array([1, 2, 3, 0, 0, 3])

In [21]: np.concatenate((a, b))
Out[21]: array([1, 2, 3, 0, 0, 3])

In [22]: a = np.array([[1],[2], [3]])

In [23]: b = np.array([[0],[0], [3]])

In [24]: np.vstack((a, b))
Out[24]: 
array([[1],
       [2],
       [3],
       [0],
       [0],
       [3]])

In [25]: np.concatenate((a, b))
Out[25]: 
array([[1],
       [2],
       [3],
       [0],
       [0],
       [3]])
+3

Source: https://habr.com/ru/post/1674403/


All Articles