Reputation: 1342
I'm new in NumPy. While i was reading the NumPy User Guide and making examples , i saw an example that made me to ask a question.
For example Python gave the below results:
>>> import numpy as np
>>> a = np.arange(6)
>>> a.ndim
1
>>> b = np.arange(6).reshape(2,3)
>>> b.ndim
2
>>> c = np.arange(6).reshape(3,2)
>>> c.ndim
2
I expected that c.ndim would given 3 rather than 2. So my question is, are the max dimension sizes of arrays are 2 when these arrays are created with np.arange() function?
Upvotes: 0
Views: 43
Reputation: 2424
What you are actually doing is first creating a 1D array with arange then reshape it.
a = np.arange(20) # of dimension 1
a = a.reshape(4,5)
print(a.ndim) # returns 2 because the array became a 2D 4x5
a = a.reshape(2,5,2)
print(a.ndim) # returns 3 because the array becomes a 3D 2x5x2
to summarize, you are forcing a reshape of a 1D np.array to a 2D using the reshape method, add more arguments to access more dimensions.
Upvotes: 2