NumPy ndarray — Understanding Dimensions, Shape & Attributes


In NumPy, every array you create is an ndarray — the core data structure that stores elements of the same type in a continuous block of memory. It enables fast mathematical and logical operations across dimensions. Understanding properties like ndim, shape, dtype, and strides is essential for mastering how NumPy manages multidimensional data efficiently.


Numpy array dimensions


Axis of Two dimensional array We can create powerful multidimensional arrays in numpy. We can arrage the elements in number of rows and columns.

Let us try some attributes to know details of the array. These attributes we will apply to different types of arrays ( dimension ).
baseBase object, if memory is from other object
dtypetype of data. Integer, float, Python object, etc
databuffer object showing start of the data
flagsDetails about Memory Layout
itemsizeSize in bytes
ndimNumber of dimensions or axis
nbytes Total bytes used by all the elements
shaperows and columns ,tuple , length indicates numbrer of axes,
product of elements is same as size
sizeTotal number of elements
stridesReturns a tuple with bytes consumed by each dimension

Single dimensional array

Single dimensional array
import numpy as my_np 
my_array = my_np.array([0,1,2]) 
print(my_array)
Output
[0 1 2]
Let us try above attributes to know details of the array.
import numpy as my_np 

my_array = my_np.array([ 0,  1,  2])

print(my_array.base)        # None  
print(my_array.dtype)       # int32
print(my_array.data)        # <memory at 0x0000021772F6F108>
print(my_array.flags)       #  	  C_CONTIGUOUS : True
                            #     F_CONTIGUOUS : False
                            #     OWNDATA : True
                            #     WRITEABLE : True
                            #     ALIGNED : True
                            #     WRITEBACKIFCOPY : False
                            #     UPDATEIFCOPY : False 
print(my_array.itemsize)    # 4
print(my_array.ndim)        # 1 
print(my_array.nbytes)      # 12 
print(my_array.shape)       # (3,) 
print(my_array.size)        # 3 
print(my_array.strides)     # (4,) 

Two dimensional array

Two dimensional array
import numpy as my_np 
my_array = my_np.array([[0,  1,  2],[3,  4,  5],[6,  7,  8]])
print(my_array[1][2]) # output is 5
import numpy as my_np 

my_array = my_np.array([[0,  1,  2],[3,  4,  5],[6,  7,  8]])

print(my_array.base)        # None  
print(my_array.dtype)       # int32
print(my_array.data)        # <memory at 0x0000021772EC28B8>
print(my_array.flags)       #  C_CONTIGUOUS : True
                            #     F_CONTIGUOUS : True
                            #     OWNDATA : True
                            #     WRITEABLE : True
                            #     ALIGNED : True
                            #     WRITEBACKIFCOPY : False
                            #     UPDATEIFCOPY : False 
print(my_array.itemsize)    # 4
print(my_array.ndim)        # 2 
print(my_array.nbytes)      # 36 
print(my_array.shape)       # (3,3) 
print(my_array.size)        # 9 
print(my_array.strides)     # (12,4) 

Three Dimensional array

Three dimensional array
import numpy as my_np 
#x=complex(98,97)
my_array = my_np.array([[[ 0,  1,  2],[ 3,  4,  5],[ 6,  7,  8]],
                        [[ 9, 10, 11],[12, 13, 14],[15, 16, 17]],
                        [[18, 19, 20],[21, 22, 23],[24, 25, 26]]])
print(my_array[2][2][1]) # 25
Let us try above attributes to know details of the array.
import numpy as my_np 
my_array = my_np.array([[[ 0,  1,  2],[ 3,  4,  5],[ 6,  7,  8]],
                        [[ 9, 10, 11],[12, 13, 14],[15, 16, 17]],
                        [[18, 19, 20],[21, 22, 23],[24, 25, 26]]])
print(my_array.base)        # None  
print(my_array.dtype)       # int32
print(my_array.data)        # <memory at 0x00000217728F4E58>
print(my_array.flags)       #  C_CONTIGUOUS : True
                            #     F_CONTIGUOUS : False
                            #     OWNDATA : True
                            #     WRITEABLE : True
                            #     ALIGNED : True
                            #     WRITEBACKIFCOPY : False
                            #     UPDATEIFCOPY : False 
print(my_array.itemsize)    # 4
print(my_array.ndim)        # 3 
print(my_array.nbytes)      # 108 
print(my_array.shape)       # (3,3,3) 
print(my_array.size)        # 27 
print(my_array.strides)     # (36,12,4) 
Read more on shape

Adding dimension while creating array

import numpy as np
npr = np.array([ 0,  1,  2],ndmin=3) # adding dimension to array
print(npr)  # [[[0 1 2]]] # print array 
print(npr.ndim) # 3 ( display dimension of the array ) 
Numpy random.randint , Random Integers
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com



26-02-2024

Very well written, visuals have really helped. Great job, thanks!




Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer