简体   繁体   中英

Appending rows to a NumPy array

In Python, I want to create an n-by-100 matrix, where the value of n is unknown at the start. This creation involves successively adding a row to the matrix. The code I have tried for this is:

x = numpy.zeros(100)
while true:
    y = loadrow(); # Load a 1-by-100 NumPy array for the new row
    x = numpy.append(x, y, 0)

However, there are three issues with the above, which I am having difficulty solving:

  1. The line x = numpy.zeros(100) initialises the matrix with a row of 100 zeros. However, I want the first row to be the first one I load. How can I create an empty matrix which will only be given data once I append the first row?

  2. The line x = numpy.append(x, y, 0) doesn't add another row to the matrix x . Instead, it just adds y to the end of the first row, to create an even longer row. But if I try x = numpy.append(x, y, 1) , such that I append to axis 1, then I get the error: TypeError: Required argument 'object' (pos 1) not found .

  3. When I successively append rows like this, it seems the I keep making copies of the original array, which will be inefficient as the array grows. Is there any other way to do this when I don't know what the size of the final array will be?

Thank you!

If you MUST use np.append here's a way to do it

In [96]: arr=np.zeros((0,5),int)

In [97]: for i in range(3):
    arr=np.append(arr,np.arange(i,i+5).reshape(1,5),0)
   ....:     

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

Frankly I don't like append . People confuse it with the list append (you didn't), people don't understand dimensions, and it rebuilds the array each time.

Look at it's code - see what it does?

return concatenate((arr, values), axis=axis)

Look also a vstack , hstack . dstack , etc

But better yet, use list append

In [100]: alist=[]

In [101]: for i in range(3):
    alist.append(np.arange(i,i+5).reshape(1,5))
   .....:     

In [102]: np.concatenate(alist,axis=0)
Out[102]: 
array([[0, 1, 2, 3, 4],
       [1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6]])

One possible solution for the first question is that you can take advantage of np.delete to remove the first row (as it contains only zeroes). Although my solution wouldn't solve your entire question it could be helpful to some people who are facing the first part of your question.

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