Numpy n dimensional Arrays ( ndarray )


Youtube Live session on Tkinter


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
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    26-02-2024

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

    Post your comments , suggestion , error , requirements etc here





    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 FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer