Week 8 - NumPy (Part I) - Introduction to NumPy

Liaw Bei Le · May 15, 2021

Introduction to NumPy:

  1. np.array( )
  2. Indexing
  3. Conditions
  4. np.zeros( ) , np.ones( )
  5. np.arange( )
  6. np.linspace( )
  7. Python Lists vs NumPy Arrays

Cheatsheets

NumPy Guide


What I learnt:

Introduction to NumPy

NumPy: stands for Numerical Python

  • is a Python library
  • used to perform a wide variety of mathematical operations on arrays
  • also has functions for working in domain of linear algebra, fourier transform, and matrices.
  • to import NumPy,

      import numpy as np
    

Array:

Elements are all of the same type, i.e. array dtype.

An array can be indexed by a tuple of nonnegative integers, by booleans, by another array, or by integers.

Rank of the array: the number of dimensions

Shape of the array: a tuple of integers giving the size of the array along each dimension

  • np.array( ):
    Initialize NumPy arrays from Python lists, using nested lists for two- or higher-dimensional data. e.g.:
    a = np.array([1, 2, 3, 4, 5, 6])
    
    a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
    
  • To access the elements in the array, use indexing:
    print(a[0])
    

    Output:

    [1, 2, 3, 4]
    
  • Conditions
    Example 1:
    a>3
    

    Output:

    array([False, False, False, True])
    

    Example 2:

    a[a>3]
    

    Output:

    array([4])
    
  • np.zeros( ):
    Easily create an array filled with 0’s. e.g.:
    np.zeros(2)
    

    Output:

    array([0., 0.])
    
  • np.ones( ):
    Easily create an array filled with 1’s. e.g.:
    np.ones(2)
    

    Output:

    array([1., 1.])
    
  • np.arange( ):
    Create an array with a range of elements. e.g.:
    np.arange(4)
    

    Output:

    array([0, 1, 2, 3])
    
  • np.arange(first number, last number, step size):
    Create an array that contains a range of evenly spaced intervals. e.g.:
    np.arange(2, 9, 2)
    

    Output:

    array([2, 4, 6, 8])
    
  • np.linspace( ):
    Create an array with values that are spaced linearly in a specified interval. e.g.:
    np.linspace(0, 10, num=5)
    

    Output:

    array([ 0. ,  2.5,  5. ,  7.5, 10. ])
    

Python Lists vs NumPy Arrays

For python lists, elements within lists in the same indexes cannot do math operations with one another quickly. It is possible using numpy arrays.

  • Without NumPy, using Python lists:
    a_list=[1,2,3]
    b_list=[2,4,6]
    print(a_list+b_list)
    print(a_list/b_list)
    

    Output:

    [1,2,3,2,4,6]
    

    Output:

    error
    
  • With NumPy, using Python lists:
    np_list_1=np.array(a)
    np_list_2=np.array(b)
    print(np_list_1 + np_list_2)
    print(np_list_1 / np_list_2)
    

    Output:

    array([3,6,9])
    

    Output:

    array([0.5,0.5,0.5])  
    

Thoughts

NumPy looks like it would come in handy in efficiently doing numerical calculations, while Python lists have limitations in doing the same.

NumPy actually has more features, which I will probably learn and then share about them in future posts, so do keep a look out!

Twitter, Facebook