1

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]
2
  • Use list append. This array append is slow and hard to get right. Look at arr and arr1 before you use them. Commented Jul 25, 2019 at 4:24
  • I guess you should use list which then can be converted into numpy array if you need. Commented Jul 25, 2019 at 4:30

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.