简体   繁体   中英

Concatenating NumPy arrays

I want to start off with an empty NumPy array, and then add rows to it sequentially (all rows are the same length). However, at the start, I do not know the length of the rows, nor do I know how many rows I will be adding. How can I do this?

So for my three rows:

a = np.array([1, 1, 1])
b = np.array([2, 2, 2])
c = np.array([3, 3, 3])

Here is what I have tried:

x = []
x = np.concatenate(x, a)
x = np.concatenate(x, b)
x = np.concatenate(x, c)

This did not work, because it concatenates a and b into a single array of length 6, rather than an array of size (2, 3). Any help?

Collect all rows in a list and then use vstack :

a = np.array([1, 1, 1])
b = np.array([2, 2, 2])
c = np.array([3, 3, 3])
x = np.vstack([a,b,c])

Instead of starting with

x = []

you can say

x = numpy.zeros([0, 3])    # 2-dimensional array with 0 rows and 3 columns

and then add rows this way:

x = numpy.vstack([x, a])
x = numpy.vstack([x, b])
x = numpy.vstack([x, c])

...but as user2357112 pointed out, it's much more efficient to start with a list, do x.append(each_array) , and only concatenate the list of arrays into a single numpy.ndarray right at the end:

x = []
x.append(a)
x.append(b)
x.append(c)
x = numpy.vstack(x)

NB: numpy.concatenate([x,a]) was working in the direction you didn't expect because, although the default direction for numpy.concatenate is indeed axis=0 , which is conventionally thought of as the row-to-row direction in 2-d arrays, your arrays a , b and c are not 2-d arrays and therefore cannot be thought of as single "rows" to start with. They only have dimension 0, so their existing elements vary along dimension 0—the same dimension along which you're concatenating. To do what you intend, you would either have to define them using double brackets to start with, as in a = numpy.array([[1,1,1]]) or convert them from their existing 1-dimensional state by saying b = b[numpy.newaxis, :] before concatenation. numpy.vstack does the latter for you implicitly.

Another way is to use numpy.concatenate . It's more general than numpy.vstack or numpy.hstack because it allows you to select the axis over which you are concatenating vectors/matrices/arrays.

Example:

>>> X, Y, Z, T = numpy.random.randint(low=0, high=10, size=(4, 3, 1), dtype='I')
>>> numpy.concatenate((X, Y, Z, T), axis=1)
array([[0, 7, 1, 7],
       [8, 4, 7, 7],
       [1, 9, 9, 7]], dtype=uint32)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM