Python Numpy Array - A Beginner Guide

Posted in

Python Numpy Array - A Beginner Guide
vinaykhatri

Vinay Khatri
Last updated on October 15, 2024

    NumPy is one of the best Python data science libraries . It comes with a built-in data structure, "Array", which standard Python lacks. Although Standard Python provides Python List and a dedicated Python array module as an alternative, both of them are not efficient.

    Here in this Python tutorial, we will go through the NumPy Arrays in Python and discuss how to initialize and use a NumPy array with the help of some examples.

    What is Python NumPy Array?

    The NumPy array is a Python container data structure, similar to the Python List. But unlike a Python list, the Python NumPy Array can store only elements of similar data types. The Python Numpy Arrays are also known as ndarray, and to define a NumPy array, we can use array() Function.

    Note: Before using numpy in your  Python program, make sure that numpy is installed for your Python environment. Else use pip install numpy the command to install numpy

    Numpy Array Syntax

    import numpy as np
    arr = np.arrray(object,dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None)

    NumPy Array() function Parameters

    object (mandatory) should be an array-like structure such as Python List, and it should contain data elements of a similar data type. Apart from the object parameter, all the other parameters of NumPy array() function are optional to know more about all the parameters check the official documentation of NumPy array .

    Create a NumPy Array

    The straightforward way to create a numpy array by using the Python list.

    my_list = [1,2,4,5,7,10]

    Once you defined the list then using the numpy.array() function you can convert it into the NumPy array object.

    numpy_array = numpy.array(my_list )

    Example

    import numpy as np
    
    my_list = [1,2,4,5,7,10]
    numpy_array = np.array(my_list)
    
    print(numpy_array)
    print(type(numpy_array))

    Output

    [ 1 2 4 5 7 10]
    <class 'numpy.ndarray'>

    Mathematical Operations on NumPy array

    Mathematical or Aithematic operations like additions, multiplication, division, and subtraction are where the NumPy Array excel as compared to Standard Python List and Arrays. Using the +, -, / and * operators we can perform mathematical operations on the elements of the array.

    Example

    >>> import numpy as np
    
    >>> my_list = [1,2,4,5,7,10]
    >>> numpy_array = np.array(my_list)
    
    >>> #addition
    >>> numpy_array + 10
    array([11, 12, 14, 15, 17, 20])
    
    >>> #substraction
    >>> numpy_array -10
    array([-9, -8, -6, -5, -3, 0])
    
    >>> #multiplication
    >>> numpy_array * 100
    array([ 100, 200, 400, 500, 700, 1000])
    
    >>> #division
    >>> numpy_array / 100
    array([0.01, 0.02, 0.04, 0.05, 0.07, 0.1 ])
    
    
    >>> #floor division
    >>> numpy_array // 2
    array([0, 1, 2, 2, 3, 5], dtype=int32)
    
    >>> #modulo
    >>> numpy_array % 2
    array([1, 0, 0, 1, 1, 0], dtype=int32)

    Shape, datatype, and size of NumPy Array

    Once you have defined a numpy array, you can check its shape, size, and datatype. The shape is the attribute of the Python Numpy Array and it returns a tuple of integer numbers values representing the number of rows and columns of the array. The size Property of array returns the total number of elements present in the array. The dtype Property of NumPy array returns the data type of elements.

    1-Dimensional NumPy Array

    import numpy as np
    
    my_list = [1,2,3,4,5]
    
    one_d = np.array(my_list)
    
    print("The shape of one_d array: ", one_d.shape)
    print("The total number of elements in one_d array: ", one_d.size)
    print("The data type of one_d array: ", one_d.dtype)

    Output

    The shape of one_d array: (5,)
    The total number of elements in one_d array: 5
    The data type of one_d array: int32

    2-Dimensional NumPy Array

    import numpy as np
    
    my_list = [[1,2,3,4,5],
               [5,7,8,9,10]
              ]
    
    two_d = np.array(my_list)
    
    print("The shape of two_d array: ", two_d.shape)
    print("The total number of elements in two_d array: ", two_d.size)
    print("The data type of two_d array: ", two_d.dtype)

    Output

    The shape of two_d array: (2, 5)
    The total number of elements in two_d array: 10
    The data type of two_d array: int32

    3-Dimensional NumPy Array

    import numpy as np
    
    my_list = [
                [
                   [1,2,3,4,5],
                   [5,7,8,9,10]
                 ],
                 [
                   [11,12,13,14,15],
                   [16,17,18,19,20]
                 ]
               ]
    
    three_d = np.array(my_list)
    
    print("The shape of three_d array: ", three_d.shape)
    print("The total number of elements in three_d array: ", three_d.size)
    print("The data type of three_d array: ", three_d.dtype)

    Output

    The shape of three_d array: (2, 2, 5)
    The total number of elements in three_d array: 20
    The data type of three_d array: int32

    Conclusion

    In this Python tutorial, you learned what is a Python NumPy array and how to initialize it in Python. In this tutorial, we have only discussed examples up to 3-D array, but numpy is capable of defining n-d Arrays. And if we directly compare it with Python List and Python standard Array module the NumPy arrays are way faster and efficient.

    People are also reading:

    Leave a Comment on this Post

    0 Comments