For any queries you can reach us at infovistarindia@gmail.com / WhatsApp us: +919158876092

NumPy Array

NumPy Array

It is a powerful N-dimensional array which is in the form of rows and columns. NumPy arrays can be initialize from the nested Python list and access its items.

NumPy array is not the same as the Standard Python array, which only manages single dimensional arrays.

  • Single Dimensional NumPy Array:
    import numpy as np  
    a = np.array([4,5,6])  
    print(a)

    The output for the above code will be [4 5 6]

  • Multi-Dimensional arrays
    import numpy as np  
    a = np.array([[4,5,6],[7,8,9]])  
    print(a)
    

    The output for the above code will be [[4 5 6] [7 8 9]]

NumPy Array Attributes

  1. ndarray.ndim

    It returns the number of dimensions i.e. axes of the array.

    import numpy as np  
    a = np.array([[1,2,3],[4,5,6]])  
    print(a.ndim)

    The output of the above code will be 2, because 'a' is a 2-dimensional array.

  2. ndarray.shape

    It returns a tuple of the dimension of the array, that is. (r, c), where r is number of rows and c is the number of columns.

    import numpy as np  
    a = np.array([[1,2,3],[4,5,6]])  
    print(a.shape)

    The output of the above code will be (2, 3), that is 2 rows and 3 columns.

  3. ndarray.size

    It returns the total number of items of the array.

    import numpy as np  
    a = np.array([[1,2,3],[4,5,6]])  
    print(a.size)

    The output of the above code will be 6 that is 2 x 3.

  4. ndarray.dtype

    It returns an object showing the type of the items in the array.

    import numpy as np  
    a = np.array([[1,2,3],[4,5,6]])  
    print(a.dtype)

    The output of the above code will be "int32" that is. 32-bit integer.

    We can explicitly specify the data type of a NumPy array.

    import numpy as np  
    a = np.array([[1,2,3],[4,5,6]], dtype = float)  
    print(a.dtype) 

    The above code will return "float64" that is 64-bit float.

  5. ndarray.itemsize

    It returns the size of each element of the array in bytes.

    import numpy as np  
    a = np.array([[1,2,3],[4,5,6]])  
    print(a.itemsize)

    The output of the above code will be 4 that is 32/8

  6. ndarray.data

    It returns the buffer holding the actual items of the array. This is an additional method of accessing the elements through indexing.

    import numpy as np  
    a = np.array([[1,2,3],[4,5,6]])  
    print(a.data)

    The above code will return the list of items.

  7. ndarray.sum()

    The function will return the sum of all the items of the ndarray.

    import numpy as np  
    a = np.random.random( (2,3) )  
    print(a)
    print(a.sum())

    The matrix generated is [[0.46541517 0.66668157 0.36277909] [0.7115755 0.57306008 0.64267163]],

    Hence, the above code will return 3.422183052180838. Because the random number is used here, therefore you may not get the same output.

  8. ndarray.min()

    The function will return the minimum item value from the ndarray.

    import numpy as np  
    a = np.random.random( (2,3) )  
    print(a.min()) 

    The matrix generated is [[0.46541517 0.66668157 0.36277909] [0.7115755 0.57306008 0.64267163]],

    Hence, the above code will return 0.36277909. Because the random number is used here, therefore you may not get the same output.

  9. ndarray.max()

    The function will return the maximum item value from the ndarray.

    import numpy as np  
    a = np.random.random( (2,3) )  
    print(a.max())  

    The matrix generated is [[0.46541517 0.66668157 0.36277909] [0.7115755 0.57306008 0.64267163]],

    Hence, the above code will return 0.7115755. Because random number is used here, therefore you may not get the same output.