Week 8

numpy and broadcasting

Broadcasting is a feature of numpy that lets us combine arrays of different sizes.

In [2]:
import numpy as np
a = np.arange(12).reshape(4, 3)*100
In [3]:
print(a)
[[   0  100  200]
 [ 300  400  500]
 [ 600  700  800]
 [ 900 1000 1100]]
In [4]:
print(a +1)
[[   1  101  201]
 [ 301  401  501]
 [ 601  701  801]
 [ 901 1001 1101]]
In [5]:
a[:2, :2] = 0
print(a)
[[   0    0  200]
 [   0    0  500]
 [ 600  700  800]
 [ 900 1000 1100]]

Example

In [6]:
a = np.arange(12).reshape(4, 3)*100
print(a)
[[   0  100  200]
 [ 300  400  500]
 [ 600  700  800]
 [ 900 1000 1100]]
In [8]:
b = np.array([1, 2, 3])
print(b)
[1 2 3]
In [9]:
print(a+b)
[[   1  102  203]
 [ 301  402  503]
 [ 601  702  803]
 [ 901 1002 1103]]

Example

In [10]:
a = np.arange(12).reshape(4, 3)*100
print(a)
[[   0  100  200]
 [ 300  400  500]
 [ 600  700  800]
 [ 900 1000 1100]]
In [11]:
b = np.array([1, 2, 3])
print(b)
[1 2 3]
In [12]:
a.shape
Out[12]:
(4, 3)
In [13]:
b.shape
Out[13]:
(3,)

Example

In [14]:
c = np.array([1, 2, 3, 4])
In [15]:
a.shape
Out[15]:
(4, 3)
In [17]:
c.shape
Out[17]:
(4,)
In [18]:
a+c
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-637545a26abd> in <module>()
----> 1 a+c

ValueError: operands could not be broadcast together with shapes (4,3) (4,) 
In [19]:
d = c.reshape(4, 1)
In [20]:
print(d)
[[1]
 [2]
 [3]
 [4]]
In [21]:
print(a)
[[   0  100  200]
 [ 300  400  500]
 [ 600  700  800]
 [ 900 1000 1100]]
In [22]:
print(a+d)
[[   1  101  201]
 [ 302  402  502]
 [ 603  703  803]
 [ 904 1004 1104]]

Example

In [23]:
import matplotlib.pyplot as plt
In [62]:
a = np.zeros((10, 10, 3))
In [25]:
plt.imshow(a)
plt.show()
In [26]:
a[2:4, :, :].shape
Out[26]:
(2, 10, 3)
In [63]:
r = np.array([1,0,0])
In [28]:
r.shape
Out[28]:
(3,)
In [64]:
a[2:4, :, :] = r
In [65]:
plt.imshow(a)
plt.axis('off')
plt.show()
In [31]:
a = np.zeros((10, 10, 3))
plt.imshow(a)
plt.show()
In [35]:
colors = np.random.rand(10, 3)
In [33]:
a.shape
Out[33]:
(10, 10, 3)
In [36]:
plt.imshow(a+colors)
plt.show()
In [37]:
colors2 = colors.reshape(10, 1, 3)
In [38]:
a.shape
Out[38]:
(10, 10, 3)
In [40]:
plt.imshow(a+colors2)
plt.show()

Boolean numpy arrays

A boolean array is an array with True/False values. Such arrays can obtained by applying a logical operator some other numpy array:

In [41]:
a = np.array([True, False, False])
In [42]:
print(a)
[ True False False]
In [43]:
a = np.arange(16).reshape(4, 4)
In [44]:
print(a)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
In [45]:
b = (a>12)
print(b)
[[False False False False]
 [False False False False]
 [False False False False]
 [False  True  True  True]]
In [46]:
c = (a==12)
print(c)
[[False False False False]
 [False False False False]
 [False False False False]
 [ True False False False]]
In [47]:
d = (a%3==0)
print(d)
[[ True False False  True]
 [False False  True False]
 [False  True False False]
 [ True False False  True]]
In [48]:
print(a)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

Logical operators for combining n=boolean arrays:

  • ~ is the negation
  • & is the logical "and"
  • | is the logical "or"
In [49]:
print(a)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
In [50]:
e = ~(a%3==0)
print(e)
[[False  True  True False]
 [ True  True False  True]
 [ True False  True  True]
 [False  True  True False]]
In [51]:
f = (a%2==0) & (a>6)
print(f)
[[False False False False]
 [False False False False]
 [ True False  True False]
 [ True False  True False]]

Indexing with numpy arrays

In [52]:
print(a)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
In [53]:
b = (a%2 == 0)
print(b)
[[ True False  True False]
 [ True False  True False]
 [ True False  True False]
 [ True False  True False]]

If a is a numpy array and b is a boolean numpy array of the same shape, then a[b] selects the entries of a for which the corresponding entry of b is True:

In [54]:
c= a[b]
print(c)
[ 0  2  4  6  8 10 12 14]
In [55]:
a[b] = 100
In [56]:
print(a)
[[100   1 100   3]
 [100   5 100   7]
 [100   9 100  11]
 [100  13 100  15]]
In [58]:
a = np.random.rand(5, 5)
b = np.random.rand(5, 5)
print(a)
[[ 0.06347242  0.47524395  0.73076712  0.86907655  0.34119944]
 [ 0.79030082  0.49790989  0.99864049  0.26032555  0.05807025]
 [ 0.10573496  0.60309552  0.5588097   0.20108758  0.37336813]
 [ 0.40623165  0.21376378  0.05175436  0.02813601  0.83887499]
 [ 0.22017819  0.30855364  0.55869749  0.55198007  0.63684804]]
In [59]:
print(b)
[[ 0.49000621  0.4153697   0.35892572  0.0191408   0.45363977]
 [ 0.62492006  0.39503466  0.72398441  0.06327726  0.05133522]
 [ 0.43938995  0.60276403  0.13054657  0.1297018   0.80812207]
 [ 0.46168547  0.03780926  0.50397639  0.85767539  0.71395244]
 [ 0.46111829  0.83929616  0.34499378  0.38029769  0.15554853]]
In [60]:
a[a>b] = 0
print(a)
[[ 0.06347242  0.          0.          0.          0.34119944]
 [ 0.          0.          0.          0.          0.        ]
 [ 0.10573496  0.          0.          0.          0.37336813]
 [ 0.40623165  0.          0.05175436  0.02813601  0.        ]
 [ 0.22017819  0.30855364  0.          0.          0.        ]]

imread()

The imread() function can be used to read an image file and convert it into a numpy array:

In [1]:
import numpy as np
import matplotlib.pyplot as plt
In [2]:
tiger = plt.imread('tiger.jpg')
In [3]:
print(tiger)
[[[ 60  73  19]
  [ 62  75  21]
  [ 63  78  23]
  ..., 
  [211 212 170]
  [216 221 181]
  [221 227 191]]

 [[ 53  65  15]
  [ 53  67  16]
  [ 55  69  16]
  ..., 
  [213 214 170]
  [220 222 183]
  [225 229 192]]

 [[ 44  57  11]
  [ 45  58  12]
  [ 45  61  14]
  ..., 
  [215 214 170]
  [220 222 182]
  [227 229 192]]

 ..., 
 [[ 91 123  47]
  [ 89 121  45]
  [ 93 121  47]
  ..., 
  [ 81  75  39]
  [ 75  62  30]
  [ 76  59  29]]

 [[ 96 128  52]
  [ 87 117  43]
  [ 79 106  35]
  ..., 
  [ 76  70  34]
  [ 72  59  25]
  [ 74  57  27]]

 [[ 85 116  40]
  [ 73 103  29]
  [ 62  89  18]
  ..., 
  [ 74  68  32]
  [ 72  59  25]
  [ 77  61  28]]]
In [4]:
tiger.shape
Out[4]:
(394, 640, 3)
In [5]:
tiger = tiger/255
In [6]:
print(tiger)
[[[ 0.23529412  0.28627451  0.0745098 ]
  [ 0.24313725  0.29411765  0.08235294]
  [ 0.24705882  0.30588235  0.09019608]
  ..., 
  [ 0.82745098  0.83137255  0.66666667]
  [ 0.84705882  0.86666667  0.70980392]
  [ 0.86666667  0.89019608  0.74901961]]

 [[ 0.20784314  0.25490196  0.05882353]
  [ 0.20784314  0.2627451   0.0627451 ]
  [ 0.21568627  0.27058824  0.0627451 ]
  ..., 
  [ 0.83529412  0.83921569  0.66666667]
  [ 0.8627451   0.87058824  0.71764706]
  [ 0.88235294  0.89803922  0.75294118]]

 [[ 0.17254902  0.22352941  0.04313725]
  [ 0.17647059  0.22745098  0.04705882]
  [ 0.17647059  0.23921569  0.05490196]
  ..., 
  [ 0.84313725  0.83921569  0.66666667]
  [ 0.8627451   0.87058824  0.71372549]
  [ 0.89019608  0.89803922  0.75294118]]

 ..., 
 [[ 0.35686275  0.48235294  0.18431373]
  [ 0.34901961  0.4745098   0.17647059]
  [ 0.36470588  0.4745098   0.18431373]
  ..., 
  [ 0.31764706  0.29411765  0.15294118]
  [ 0.29411765  0.24313725  0.11764706]
  [ 0.29803922  0.23137255  0.11372549]]

 [[ 0.37647059  0.50196078  0.20392157]
  [ 0.34117647  0.45882353  0.16862745]
  [ 0.30980392  0.41568627  0.1372549 ]
  ..., 
  [ 0.29803922  0.2745098   0.13333333]
  [ 0.28235294  0.23137255  0.09803922]
  [ 0.29019608  0.22352941  0.10588235]]

 [[ 0.33333333  0.45490196  0.15686275]
  [ 0.28627451  0.40392157  0.11372549]
  [ 0.24313725  0.34901961  0.07058824]
  ..., 
  [ 0.29019608  0.26666667  0.1254902 ]
  [ 0.28235294  0.23137255  0.09803922]
  [ 0.30196078  0.23921569  0.10980392]]]
In [7]:
plt.figure(figsize=(10,10))
plt.imshow(tiger)
plt.show()
In [8]:
head = tiger[25:275, 100:375, :]
plt.figure(figsize=(10,10))
plt.imshow(head)
plt.show()
In [9]:
head[:, :, 1] = 0
head[:, :, 2] = 0
In [10]:
plt.figure(figsize=(10,10))
plt.imshow(head)
plt.show()
In [11]:
plt.figure(figsize=(10,10))
plt.imshow(tiger)
plt.show()
In [12]:
tiger[:, 500:550, :] = [0,1,0]
plt.figure(figsize=(10,10))
plt.imshow(tiger)
plt.show()

Project 5

In [13]:
face = plt.imread('face.png')
In [96]:
print(face)
[[[ 0.1882353   0.1882353   0.1882353   1.        ]
  [ 0.1882353   0.1882353   0.1882353   1.        ]
  [ 0.1882353   0.1882353   0.1882353   1.        ]
  ..., 
  [ 0.25490198  0.25490198  0.25490198  1.        ]
  [ 0.25490198  0.25490198  0.25490198  1.        ]
  [ 0.25098041  0.25098041  0.25098041  1.        ]]

 [[ 0.18431373  0.18431373  0.18431373  1.        ]
  [ 0.1882353   0.1882353   0.1882353   1.        ]
  [ 0.1882353   0.1882353   0.1882353   1.        ]
  ..., 
  [ 0.25490198  0.25490198  0.25490198  1.        ]
  [ 0.25490198  0.25490198  0.25490198  1.        ]
  [ 0.25098041  0.25098041  0.25098041  1.        ]]

 [[ 0.18431373  0.18431373  0.18431373  1.        ]
  [ 0.18431373  0.18431373  0.18431373  1.        ]
  [ 0.18431373  0.18431373  0.18431373  1.        ]
  ..., 
  [ 0.25490198  0.25490198  0.25490198  1.        ]
  [ 0.25490198  0.25490198  0.25490198  1.        ]
  [ 0.25490198  0.25490198  0.25490198  1.        ]]

 ..., 
 [[ 0.40784314  0.40784314  0.40784314  1.        ]
  [ 0.40784314  0.40784314  0.40784314  1.        ]
  [ 0.41176471  0.41176471  0.41176471  1.        ]
  ..., 
  [ 0.10980392  0.10980392  0.10980392  1.        ]
  [ 0.10196079  0.10196079  0.10196079  1.        ]
  [ 0.09411765  0.09411765  0.09411765  1.        ]]

 [[ 0.41176471  0.41176471  0.41176471  1.        ]
  [ 0.41176471  0.41176471  0.41176471  1.        ]
  [ 0.41176471  0.41176471  0.41176471  1.        ]
  ..., 
  [ 0.11372549  0.11372549  0.11372549  1.        ]
  [ 0.09803922  0.09803922  0.09803922  1.        ]
  [ 0.09019608  0.09019608  0.09019608  1.        ]]

 [[ 0.41176471  0.41176471  0.41176471  1.        ]
  [ 0.41176471  0.41176471  0.41176471  1.        ]
  [ 0.40392157  0.40392157  0.40392157  1.        ]
  ..., 
  [ 0.10588235  0.10588235  0.10588235  1.        ]
  [ 0.09803922  0.09803922  0.09803922  1.        ]
  [ 0.09411765  0.09411765  0.09411765  1.        ]]]
In [14]:
face.shape
Out[14]:
(266, 400, 4)
In [15]:
plt.figure(figsize=(10,10))
plt.imshow(face)
plt.show()
In [99]:
face0 = face[:, :, 0]
In [100]:
face0.shape
Out[100]:
(266, 400)
In [102]:
plt.figure(figsize=(10,10))
plt.imshow(face0, cmap='gray')
plt.show()

np.mean() and np.median()

In [103]:
a = np.random.rand(3,3)
In [104]:
print(a)
[[ 0.19124945  0.20750455  0.90263537]
 [ 0.51706769  0.22485345  0.2842018 ]
 [ 0.72431818  0.98136056  0.22422924]]
In [105]:
np.mean(a)
Out[105]:
0.47304669954684608
In [106]:
np.median(a)
Out[106]:
0.28420179784964472