简体   繁体   中英

Initialise a numpy array of a specific shape

I want to initialise a numpy array of a specific shape such that when I append numbers to it it will 'fill up' in that shape.

The length of the array will vary - and that is fine I do not mind how long it is - but I want it to have 4 columns. Ideally somthing similar to the following:

array = np.array([:, 4])
print(array)
array = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

Again the actual length of the array would not be defines. That way if I was to append a different array it would work as follows

test_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
array = np.append(array, test_array)
print(array)
array = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]

Is there any way to do this?

If I understand well your issue, I think you do not need to initialize your array. You sould check first that your array size divides by 4.

import numpy as np

l = test_array.shape[0]
cols = 4
rows = l / cols
my_array = np.reshape(test_array, (rows, cols))

The kind of behavior that you seek is unusual. You should explain why you need it. If you want something readily grows, use Python list . numpy arrays have a fixed size. Values can be assigned to an array in various ways, but to grow it, you need to create a new array with some version of concatenate . (Yes, there is a resize function/method, but that's not commonly used.)

I'll illustrate the value assignment options:

Initial an array with a known size. In your case the 5 could be larger than anticipated, and the 4 is the desired number of 'columns'.

In [1]: arr = np.zeros((5,4), dtype=int)                                                     
In [2]: arr                                                                                  
Out[2]: 
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])

Assign 4 values to one row:

In [3]: arr[0] = [1,2,3,4]                                                                   

Assign 3 values starting at a given point in a flat view of the array:

In [4]: arr.flat[4:7] = [1,2,3]                                                              
In [5]: arr                                                                                  
Out[5]: 
array([[1, 2, 3, 4],
       [1, 2, 3, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])

This array, while defined as (5,4) shape, can be viewed as (20,) 1d array. I had to choose the appropriate slice values in the flat view.

More commonly we assign values to a block of rows (or a variety of other indexed areas). arr[2:, :] is a (3,4) portion of arr . So we need to assign (3,4) array to it (or an equivalent list structure). To get full benefit of this sort of assignment you need to read up on broadcasting .

In [6]: arr[2:,:] = np.reshape(list(range(10,22)),(3,4))                                     
In [7]: arr                                                                                  
Out[7]: 
array([[ 1,  2,  3,  4],
       [ 1,  2,  3,  0],
       [10, 11, 12, 13],
       [14, 15, 16, 17],
       [18, 19, 20, 21]])
In [8]: arr.ravel()                                                                          
Out[8]: 
array([ 1,  2,  3,  4,  1,  2,  3,  0, 10, 11, 12, 13, 14, 15, 16, 17, 18,
       19, 20, 21])

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