Python NumPy Tutorial for Beginners

What is NumPy?

NumPy is an open-source linear algebra library for Python and it supports a large number of mathematical operations. It is incredibly fast and has bound to C libraries.  NumPy provides NumPy array which can be used for multidimensional arrays and matrices multiplication.

NumPy arrays come in two flavors, Vectors and Matrices. Vectors are strictly 1Dimensional arrays and Matrices are 2 Dimensional arrays.

Numpy installation on Ubuntu

The Numpy library by default will not be available with Python package. If you try to import it you will the below error message.

techies@techiescorner:~$ python3
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import numpy as np
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'

To install NumPy execute below command on Ubuntu terminal

sudo pip install numpy

Collecting numpy
  Downloading numpy-1.18.1-cp36-cp36m-manylinux1_x86_64.whl (20.1 MB)
     |████████████████████████████████| 20.1 MB 1.3 MB/s 
Installing collected packages: numpy
Successfully installed numpy-1.18.1

Now try to import the library again and this time you will not get an error message.

techies@techiescorner:~$ python3
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>>

From the above output, we can confirm that the installed version is numpy-1.18.1

Start with NumPy array

In this section, we learn various methods to create a NumPy array using Python and NumPy library.

The simplest way to create an array in NumPy is python List

techies@techiescorner:~$ python3
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
Type "help", "copyright", "credits" or "license" for more information.
>>> my_list = [1,2,3]
>>> my_list
[1, 2, 3]
>>> import numpy as np
>>> np.array(my_list)
array([1, 2, 3])
>>> 

Here we have created a list (my_list) and then imported NumPy as np (alias name) and converted the list to an array

>>> count = np.array(my_list)
>>> count
array([1, 2, 3])

Now I assigned the value of the array to a variable (count) and printed it in the terminal.

 

Next, we will create a 2Dimensional array or matrices.

>>> my_2d = [[1,2,3],[4,5,6],[7,8,9]]
>>> np.array(my_2d)
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> 

Here the number of brackets (Red color) indicates that it’s a 2D array. There are other built-in mechanisms to generate array for example np.arange(), usage is given below.

np.arange() usage

>>> np.arange(0,10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> 

Here we have created an array with the help of ‘arange’ function.  Next, we will add a step size in the above ‘arange’ function.

>>> np.arange(0,10,2)
array([0, 2, 4, 6, 8])
>>> 
>>> np.arange(0,10,3)
array([0, 3, 6, 9])
>>> 

Here we have added step size as 2 and 3 (Marked in red color)  and it generated the output as 3 space and 3 space.

np.zeros() and np.ones()

These are special types of functions used to create arrays of all zeros and ones. To understand more please read the examples below.

>>> np.zeros(4)
array([0., 0., 0., 0.])

np.zeros(4) this will generate a 1 dimension array of all zeros. To generate 2 dimension array you need to specify the number rows and columns as follows.

>>> np.zeros((3,4))
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])
>>> 

We created 2 rows and 3 columns matrics. This is the same for np.ones() function

>>> np.ones(5)
array([1., 1., 1., 1., 1.])

NumPy Indexing

NumPy provides indexing to select a particular value or a range of values.  Check the example to understand it easily.

>>> count = np.arange(0,10)
>>> count
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

>>> count [6]
6

>>> count [0:5]
array([0, 1, 2, 3, 4])
>>> 

Here I have created an array called “count” contains the number from 0 to 10. to get a particular value I have used index 6 (count [6] ) same as in python and it printed the 6th value. Next to get a range of values use “count [0:5] “.

Adding a few more examples.

>>> count [:4]
array([0, 1, 2, 3])
>>> 
>>> count [3:]
array([3, 4, 5, 6, 7, 8, 9])

The above example is self-explanatory.

Next, we will look into the 2D array or matrices indexing. Create a 2D array named “count_2d” as follows

>>> count_2D = np.array([[10,20,30],[40,50,60],[70,80,90]])
>>> count_2D
array([[10, 20, 30],
       [40, 50, 60],
       [70, 80, 90]])
>>> count_2D[0][0]
10

>>> count_2D[2][1]
80
>>> count_2D[2,1]
80

To get the first value (10) we used indexing as count_2D[0][0], it means the value 10 is present in the 0th row and 0th column (Indexing starts from zero).

A few more examples, slicing the array with indexing. We can fetch a particular set of values from the array.

1) >>> count_2D[:2]
        c0     c1  . c2
array([[10, 20, 30],  R1

       [40, 50, 60]]) R2

2) >>> count_2D[:2,1:]

array([[20, 30],
       [50, 60]])

NumPy boolean operation

I will explain this with the help of an example.

>>> count = np.arange(0,10)
>>> count
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> count > 5
array([False, False, False, False, False, False,  True,  True,  True, True])
>>> bool_value = count > 5
>>> bool_value
array([False, False, False, False, False, False,  True,  True,  True, True])
>>> count[bool_value]
array([6, 7, 8, 9])

 

NumPy operation

This section contains the arithmetic operation with arrays. Please see the example of arithmetic operation.

>>> count = np.arange(0,11)
>>> count
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
>>> count + count
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18, 20])
>>> count + 2
array([ 2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])
>>> 
>>> count - count
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
>>> 
>>> count * count
array([  0,   1,   4,   9,  16,  25,  36,  49,  64,  81, 100])
>>> count * 4
array([ 0,  4,  8, 12, 16, 20, 24, 28, 32, 36, 40])

Next, we will perform division of (0 / 0) ideally, it will give you an error in python but in NumPy, it won’t give you an error but it shows a “nan” value. Look at the example given below.

>>> count / count
__main__:1: RuntimeWarning: invalid value encountered in true_divide
array([nan,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])
>>> 

Look at another example here we will divide one with an array value.

>>> 1 / count
__main__:1: RuntimeWarning: divide by zero encountered in true_divide
array([       inf, 1.        , 0.5       , 0.33333333, 0.25      ,
       0.2       , 0.16666667, 0.14285714, 0.125     , 0.11111111,
      0.1       ])
>> 

The output contains the value infinity (inf), here also the NumPy doesn’t show any error.

Universal array functions

To get a square root of an array we will use the function np.sqrt(), look at the example below.

>>> count = np.arange(0,11,2)
>>> count
array([ 0,  2,  4,  6,  8, 10])
>>> np.sqrt(count)
array([0.        , 1.41421356, 2.        , 2.44948974, 2.82842712,
       3.16227766])
>>> 

Here, I have created an array named count and generated square root of it with np.sqrt() NumPy universal function.

To get the maximum value in the array use np.max() function.

>>> np.max(count)
10

and for minimum value np.min() function

>>> np.min(count)
0

 

Leave a Reply

Your email address will not be published. Required fields are marked *