Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a numpy array with shape (0, 2)?

Tags:

python

numpy

I'm trying to understand how a np.array can be created with shape(0,2).

Is this even possible?

So

np.array([]).shape
#output (0,)

np.array([[],[]]).shape
#output (2, 0)

can I get (0, 2) ?

(without using .T)

like image 240
Alex Avatar asked Oct 18 '25 10:10

Alex


2 Answers

Use a function such as np.zeros. No need to worry about the value it generates since there are no values in a 0 x 2 array:

arr = np.zeros((0, 2))
arr.shape

(0, 2)
like image 186
busybear Avatar answered Oct 21 '25 08:10

busybear


It can be done by np.empty((0,2)) or np.zeros((0,2)).

np.empty(), unlike np.zeros(), does not set the array values to zero and it is slightly faster.

Extra information: In your code np.array([]).shape outputs a rank-1 array which is equal to its transpose.

like image 44
Safak Ozdek Avatar answered Oct 21 '25 09:10

Safak Ozdek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!