Machine Learning Prerequisites (Numpy)

3 / 32

NumPy - Arrays

In Machine Learning, NumPy is mostly used for matrix manipulations.

Most of the Machine Learning involves training and building models which are primarily some kind of mathematical equations (linear, non-linear, polynomial, etc.).

These models (mathematical equations) are represented as matrices/vectors and the training the model many a time involves complex matrix manipulations.

A matrix is nothing but a multi-dimensional NumPy array.

A NumPy array is a multi-dimensional array, whose elements are usually numbers of the same data type. You can access each element of the NumPy array by using indexes. Indexes in the NumPy array starts with 0.

e.g. my_array[1,3], which means the element in the second row and fourth column.

Axis in NumPy array

For a 2-D NumPy array:

    `axis = 0` refers to rows of the array

    `axis = 1` refers to columns of the array

For a 3-D NumPy array, there will be another axis i.e. axis = 2.

This information of axis is used heavily in NumPy matrix (multi-dimensional arrays) manipulations.

Rank

The rank of a NumPy array is the number of dimensions of the array.

e.g. if there is a NumPy array (matrix) of dimension 2x3 (i.e. 2 rows and 3 columns), then the rank of this matrix (array) is 2 since it is a 2-dimensional array.

Shape

The shape of a NumPy array is the dimensions of that array.

e.g. if an array (matrix) has dimensions of 2x3, then the shape of this array (matrix) is (2,3) i.e. 2 rows and 3 columns.

Creating a NumPy array

A NumPy array can be created in either of the below ways:

  1. Through passing a Python list
  2. Through passing a Python tuple


Examples of creating NumPy arrays:

(1) Creating a NumPy array by passing a Python list

  • Import Numpy as np

    import numpy as np
    
  • Create a Python list as below:

    my_list = [50, 12, 67, 23]
    
  • Pass the above list to array() function of NumPy

    my_list_array = np.array(my_list)
    
  • Print the above array to see its values

    print(my_list_array)
    

(2) Creating NumPy array by passing a Python tuple

  • Import Numpy as np

    import numpy as np
    
  • Create a Python tuple as below:

    my_tup = (88, 17, 45, 76)
    
  • Pass the above tuple my_tup to array() function of NumPy

    my_tup_arr = np.array(my_tup)
    
  • Print the above array to see its values

    print(my_tup_arr)
    


A NumPy operation is quite faster than a normal Python operation. For example, to multiply two arrays (matrices), NumPy uses Vector operation, while in Python we normally use looping to do the same thing.

We can use the %timeit function of Python to measure and compare the time taken for execution.

INSTRUCTIONS

Creating Numpy array by passing a Python list

Please follow the below steps to create a NumPy array by passing a Python list:

  • Import Numpy as np

    import numpy as np
    
  • Create a Python list as below:

    sample_list = [1,2,3]
    
  • Pass the above list to array() function of NumPy

    list_array = np.array(<<your code comes here>>)
    

Creating Numpy array by passing a Python tuple

Please follow the below steps to create a NumPy array by passing a Python tuple:

  • Import Numpy as np

    import numpy as np
    
  • Create a Python tuple as below:

    tup = (1,2,3)
    
  • Pass the above tuple tup to array() function of NumPy

    my_tup_array = np.array(<<your code comes here>>)
    

You can use the print() function to view the above-created arrays.

See Answer

No hints are availble for this assesment


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...