Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: numpy shape confusion

Tags:

python

numpy

I have a numpy array:

>>> type(myArray1)
Out[14]: numpy.ndarray

>>> myArray1.shape
Out[13]: (500,)

I have another array:

>>> type(myArray2)
Out[14]: numpy.ndarray

>>> myArray2.shape
Out[13]: (500,1)

( 1 ) What is the difference between (500,) and (500,1) ?

( 2 ) How do I change (500,) to (500,1)

like image 876
Zanam Avatar asked Feb 10 '26 17:02

Zanam


1 Answers

(1) The difference between (500,) and (500,1) is that the first is the shape of a one-dimensional array, while the second is the shape of a 2-dimensional array whose 2nd dimension has length 1. This may be confusing at first since other languages don't make that distinction.

(2) You can use np.reshape to do that: myArray1.reshape(-1,1). You can also add a dimension to your array using np.expand_dims: np.expand_dims(myArray1, axis = 1).

like image 151
Tobias Avatar answered Feb 12 '26 15:02

Tobias



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!