kittyKat
kittyKat

Reputation: 121

How to add element to empty 2d numpy array

I'm trying to insert elements to an empty 2d numpy array. However, I am not getting what I want.

I tried np.hstack but it is giving me a normal array only. Then I tried using append but it is giving me an error.

Error:

ValueError: all the input arrays must have same number of dimensions

randomReleaseAngle1 = np.random.uniform(20.0, 77.0, size=(5, 1))
randomVelocity1 = np.random.uniform(40.0, 60.0, size=(5, 1))
randomArray =np.concatenate((randomReleaseAngle1,randomVelocity1),axis=1)

arr1 = np.empty((2,2), float)
arr = np.array([])

for i in randomArray:
    data = [[170, 68.2, i[0], i[1]]] 
    df = pd.DataFrame(data, columns = ['height', 'release_angle', 'velocity', 'holding_angle']) 
    test_y_predictions = model.predict(df)
    print(test_y_predictions) 
    if (np.any(test_y_predictions == 1)):
        arr = np.hstack((arr, np.array([i[0], i[1]])))
        arr1 = np.append(arr1, np.array([i[0], i[1]]), axis=0) 

print(arr)
print(arr1)

I wanted to get something like

[[1.5,2.2],
[3.3,4.3],
[7.1,7.3],
[3.3,4.3],
[3.3,4.3]]

However, I'm getting

[56.60290125 49.79106307 35.45102444 54.89380834 47.09359271 49.19881675
 22.96523274 44.52753514 67.19027156 54.10421167]

Upvotes: 1

Views: 7857

Answers (2)

hpaulj
hpaulj

Reputation: 231335

The recommended list append approach:

In [39]: alist = []                                                                                          
In [40]: for i in range(3): 
    ...:     alist.append([i, i+10]) 
    ...:                                                                                                     
In [41]: alist                                                                                               
Out[41]: [[0, 10], [1, 11], [2, 12]]
In [42]: np.array(alist)                                                                                     
Out[42]: 
array([[ 0, 10],
       [ 1, 11],
       [ 2, 12]])

If we start with a empty((2,2)) array:

In [47]: arr = np.empty((2,2),int)                                                                           
In [48]: arr                                                                                                 
Out[48]: 
array([[139934912589760, 139934912589784],
       [139934871674928, 139934871674952]])
In [49]: np.concatenate((arr, [[1,10]],[[2,11]]), axis=0)                                                    
Out[49]: 
array([[139934912589760, 139934912589784],
       [139934871674928, 139934871674952],
       [              1,              10],
       [              2,              11]])

Note that empty does not mean the same thing as the list []. It's a real 2x2 array, with 'unspecified' values. And those values remain when we add other arrays to it.

I could start with an array with a 0 dimension:

In [51]: arr = np.empty((0,2),int)                                                                           
In [52]: arr                                                                                                 
Out[52]: array([], shape=(0, 2), dtype=int64)
In [53]: np.concatenate((arr, [[1,10]],[[2,11]]), axis=0)                                                    
Out[53]: 
array([[ 1, 10],
       [ 2, 11]])

That looks more like the list append approach. But why start with the (0,2) array in the first place?

np.concatenate takes a list of arrays (or lists that can be made into arrays). I used nested lists that make (1,2) arrays. With this I can join them on axis 0.

Each concatenate makes a new array. So if done iteratively it is more expensive than the list append.

np.append just takes 2 arrays and does a concatenate. So doesn't add much. hstack tweaks shapes and joins on the 2nd (horizontal) dimension. vstack is another variant. But they all end up using concatenate.

Upvotes: 1

Akaisteph7
Akaisteph7

Reputation: 6436

With the hstack method, you can just reshape after you get the final array:

arr = arr.reshape(-1, 2)
print(arr)

The other method can be more easily done in a similar way:

        arr1 = np.append(arr1, np.array([i[0], i[1]]) # in the loop

arr1 = arr1.reshape(-1, 2)
print(arr1)

Upvotes: 0

Related Questions