In [1]:
from IPython.display import HTML
import requests
HTML(requests.get("https://git.io/fh5WI").text)
Out[1]:
go top

1. Numpy Arrays


  • Numpy is one of the most important Python libraries used for scientific computing and data analysis.
  • The main tool it provides are numpy arrays.
In [2]:
import numpy as np
Converting a list into a numpy array:
In [8]:
x = np.array([1, 2, 3])
x
Out[8]:
array([1, 2, 3])
In [9]:
x[0]
Out[9]:
1
In [10]:
x[0] = 100
x
Out[10]:
array([100,   2,   3])
We can perform computations on the whole numpy arrays at once:
In [11]:
x
Out[11]:
array([100,   2,   3])
In [12]:
y = np.array([2, 3, 4])
y
Out[12]:
array([2, 3, 4])
In [13]:
x + y
Out[13]:
array([102,   5,   7])
In [14]:
10*x
Out[14]:
array([1000,   20,   30])
In [15]:
x**2
Out[15]:
array([10000,     4,     9])
In [16]:
np.sin(x)
Out[16]:
array([-0.50636564,  0.90929743,  0.14112001])
go top

2. Performance Advantage


Numpy computations are typically a lot faster than computations done using pure Python.

Example. Calculate an array of squares of integers from 0 to 1,000,000 using numpy:
In [36]:
np.arange(10)
Out[36]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [46]:
%%timeit -n 5

x = np.arange(10**6)
y = x**2
2.25 ms ± 907 µs per loop (mean ± std. dev. of 7 runs, 5 loops each)
The same computation using list comprehensions:
In [47]:
%%timeit -n 5

x = range(10**6)
y = [n**2 for n in x]
251 ms ± 3.66 ms per loop (mean ± std. dev. of 7 runs, 5 loops each)
go top

3. Plotting with Numpy and Bokeh


Bokeh is one of Python libraries for producing graphs and plots.

In [3]:
import bokeh.plotting as bk
bk.output_notebook() # show plots in the notebook
Loading BokehJS ...
In [4]:
x = [1, 2, 3, 4]
y = [4, 3, 2, 3]

p = bk.figure(plot_width=300, plot_height=300)
p.line(x, y, color='blue')
p.circle(x, y, size = 10, color='red')
bk.show(p)
In [5]:
x = np.linspace(0, 1, 5)
x
Out[5]:
array([0.  , 0.25, 0.5 , 0.75, 1.  ])
In [6]:
x = np.linspace(0, 12, 200)
y = np.sin(x)
z = np.cos(x)

p = bk.figure(plot_width=600, plot_height=300)
p.line(x, y, color='blue', line_width=2)
p.line(x, z, color='red', line_width=2)
bk.show(p)
go top

4. Operations on Numpy Arrays


4.1 Slicing

Array slicing works the same as for lists:

my_array[start : stop]
my_array[start : stop : step]
In [51]:
a = np.arange(15)
a
Out[51]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
In [52]:
a[6:10]
Out[52]:
array([6, 7, 8, 9])
In [53]:
a[1:10:2]
Out[53]:
array([1, 3, 5, 7, 9])
go top

4.2 Boolean Arrays


Boolean arrays let us select elements of an array using a logical condition.
Create an array of 15 random integers from the range 0-100:
In [8]:
a = np.random.randint(0, 100, 15)
a
Out[8]:
array([51, 68, 14, 21, 42, 51, 19, 97, 44, 92, 34, 86, 86,  4, 85])
In [9]:
large = (a > 50)
large
Out[9]:
array([ True,  True, False, False, False,  True, False,  True, False,
        True, False,  True,  True, False,  True])
Create a subarray of elements of a for which large is true:
In [11]:
a[large]
Out[11]:
array([51, 68, 51, 97, 92, 86, 86, 85])
In [12]:
a[a>50]
Out[12]:
array([51, 68, 51, 97, 92, 86, 86, 85])
This can be used to modify arrays:
In [84]:
a
Out[84]:
array([45, 62, 96, 24, 42, 40, 56, 34, 73, 12,  4, 18, 92, 95, 36])
In [85]:
a[a>50] = 0
a
Out[85]:
array([45,  0,  0, 24, 42, 40,  0, 34,  0, 12,  4, 18,  0,  0, 36])
go top

5. Multidimensional Arrays


Numpy arrays can have more than one dimension.

In [86]:
a = np.arange(70).reshape(10, 7)
a
Out[86]:
array([[ 0,  1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12, 13],
       [14, 15, 16, 17, 18, 19, 20],
       [21, 22, 23, 24, 25, 26, 27],
       [28, 29, 30, 31, 32, 33, 34],
       [35, 36, 37, 38, 39, 40, 41],
       [42, 43, 44, 45, 46, 47, 48],
       [49, 50, 51, 52, 53, 54, 55],
       [56, 57, 58, 59, 60, 61, 62],
       [63, 64, 65, 66, 67, 68, 69]])
In [87]:
a[1, 2]
Out[87]:
9
In [88]:
a[1,2] = -1
a
Out[88]:
array([[ 0,  1,  2,  3,  4,  5,  6],
       [ 7,  8, -1, 10, 11, 12, 13],
       [14, 15, 16, 17, 18, 19, 20],
       [21, 22, 23, 24, 25, 26, 27],
       [28, 29, 30, 31, 32, 33, 34],
       [35, 36, 37, 38, 39, 40, 41],
       [42, 43, 44, 45, 46, 47, 48],
       [49, 50, 51, 52, 53, 54, 55],
       [56, 57, 58, 59, 60, 61, 62],
       [63, 64, 65, 66, 67, 68, 69]])
In [91]:
a[2:6, :3]
Out[91]:
array([[14, 15, 16],
       [21, 22, 23],
       [28, 29, 30],
       [35, 36, 37]])
In [89]:
a[1, :]
Out[89]:
array([ 7,  8, -1, 10, 11, 12, 13])
In [90]:
a[:, 2]
Out[90]:
array([ 2, -1, 16, 23, 30, 37, 44, 51, 58, 65])
In [92]:
a[a%2 == 0] = 0
a
Out[92]:
array([[ 0,  1,  0,  3,  0,  5,  0],
       [ 7,  0, -1,  0, 11,  0, 13],
       [ 0, 15,  0, 17,  0, 19,  0],
       [21,  0, 23,  0, 25,  0, 27],
       [ 0, 29,  0, 31,  0, 33,  0],
       [35,  0, 37,  0, 39,  0, 41],
       [ 0, 43,  0, 45,  0, 47,  0],
       [49,  0, 51,  0, 53,  0, 55],
       [ 0, 57,  0, 59,  0, 61,  0],
       [63,  0, 65,  0, 67,  0, 69]])
In [93]:
b = a**2
b
Out[93]:
array([[   0,    1,    0,    9,    0,   25,    0],
       [  49,    0,    1,    0,  121,    0,  169],
       [   0,  225,    0,  289,    0,  361,    0],
       [ 441,    0,  529,    0,  625,    0,  729],
       [   0,  841,    0,  961,    0, 1089,    0],
       [1225,    0, 1369,    0, 1521,    0, 1681],
       [   0, 1849,    0, 2025,    0, 2209,    0],
       [2401,    0, 2601,    0, 2809,    0, 3025],
       [   0, 3249,    0, 3481,    0, 3721,    0],
       [3969,    0, 4225,    0, 4489,    0, 4761]])
go top

6. Example: Image Manipulation


  • We will use multidimensional numpy arrays to modify an image.
  • We will use Matplotlib which is another Python plotting library.
In [3]:
import matplotlib.pyplot as plt
Read an image file and represent it as a 3-dimensional numpy array:
In [4]:
x = plt.imread("bird.jpg")
x
Out[4]:
array([[[ 36,  25,  19],
        [ 36,  25,  19],
        [ 36,  27,  20],
        ...,
        [ 27,  31,  16],
        [ 26,  30,  15],
        [ 26,  30,  15]],

       [[ 36,  25,  19],
        [ 37,  26,  20],
        [ 36,  27,  20],
        ...,
        [ 27,  31,  16],
        [ 26,  30,  15],
        [ 27,  29,  15]],

       [[ 37,  26,  20],
        [ 37,  26,  20],
        [ 36,  27,  20],
        ...,
        [ 28,  30,  16],
        [ 28,  30,  16],
        [ 27,  29,  15]],

       ...,

       [[ 98, 106,  82],
        [ 97, 105,  81],
        [ 97, 105,  81],
        ...,
        [ 66,  60,  36],
        [ 65,  59,  35],
        [ 65,  59,  35]],

       [[ 98, 106,  82],
        [ 97, 105,  81],
        [ 97, 105,  81],
        ...,
        [ 65,  59,  35],
        [ 65,  59,  35],
        [ 65,  59,  35]],

       [[ 98, 106,  82],
        [ 97, 105,  81],
        [ 97, 105,  81],
        ...,
        [ 65,  59,  35],
        [ 64,  58,  34],
        [ 64,  58,  34]]], dtype=uint8)

RGB Coordinates

Color of each pixel is represented by 3 integers, which give intensities of its Red, Green, and Blue components:
  • 0 = color off
  • 255 = full on
Shape of the image array (rows, columns, depth):
In [95]:
x.shape
Out[95]:
(2272, 2016, 3)
For image manipulation it is convenient to convert RGB coordinates to floats in the range 0-1:
In [5]:
x = x/255
x
Out[5]:
array([[[0.14117647, 0.09803922, 0.0745098 ],
        [0.14117647, 0.09803922, 0.0745098 ],
        [0.14117647, 0.10588235, 0.07843137],
        ...,
        [0.10588235, 0.12156863, 0.0627451 ],
        [0.10196078, 0.11764706, 0.05882353],
        [0.10196078, 0.11764706, 0.05882353]],

       [[0.14117647, 0.09803922, 0.0745098 ],
        [0.14509804, 0.10196078, 0.07843137],
        [0.14117647, 0.10588235, 0.07843137],
        ...,
        [0.10588235, 0.12156863, 0.0627451 ],
        [0.10196078, 0.11764706, 0.05882353],
        [0.10588235, 0.11372549, 0.05882353]],

       [[0.14509804, 0.10196078, 0.07843137],
        [0.14509804, 0.10196078, 0.07843137],
        [0.14117647, 0.10588235, 0.07843137],
        ...,
        [0.10980392, 0.11764706, 0.0627451 ],
        [0.10980392, 0.11764706, 0.0627451 ],
        [0.10588235, 0.11372549, 0.05882353]],

       ...,

       [[0.38431373, 0.41568627, 0.32156863],
        [0.38039216, 0.41176471, 0.31764706],
        [0.38039216, 0.41176471, 0.31764706],
        ...,
        [0.25882353, 0.23529412, 0.14117647],
        [0.25490196, 0.23137255, 0.1372549 ],
        [0.25490196, 0.23137255, 0.1372549 ]],

       [[0.38431373, 0.41568627, 0.32156863],
        [0.38039216, 0.41176471, 0.31764706],
        [0.38039216, 0.41176471, 0.31764706],
        ...,
        [0.25490196, 0.23137255, 0.1372549 ],
        [0.25490196, 0.23137255, 0.1372549 ],
        [0.25490196, 0.23137255, 0.1372549 ]],

       [[0.38431373, 0.41568627, 0.32156863],
        [0.38039216, 0.41176471, 0.31764706],
        [0.38039216, 0.41176471, 0.31764706],
        ...,
        [0.25490196, 0.23137255, 0.1372549 ],
        [0.25098039, 0.22745098, 0.13333333],
        [0.25098039, 0.22745098, 0.13333333]]])
We will use the following function to display an image:
In [2]:
def imshow(x):
    plt.figure(figsize=(10,10))
    plt.imshow(x)
    plt.show()
In [6]:
imshow(x)
In [99]:
imshow(x/2)
In [102]:
imshow(np.minimum(1, x*2))
In [9]:
imshow(x[300:750, 600:1400, :])
In [10]:
y = x.copy()
y[:,:,1] = 0
y[:,:,2] = 0
imshow(y)
In [18]:
z = x.copy()
z[300:700, 600:1400, 0] =  1
z[700:1100, 600:1400, 1] =  1
z[1100:1500, 600:1400, 2] =  1
imshow(z)
In [ ]: