Lecture 16

Last time: Numpy arrays.

Let's recall some of it:

In [1]:
# gotta import
import numpy as np

# defining a new array:
X = np.array([[1,2,3],[4,5,6]])  # will make an array with X.shape = (2,3)
print("nice array: \n", X, "\nThe shape was", X.shape, "\n")
X = np.zeros([3,3]) # the shape is given as a list
print("zeros:\n", X)

print("")

# accessing and setting
X[0,1] = 999
print("after setting X[0,1] to 999:")
print(X)

# make a 1x9 array into a 3x3 array
X = np.array(range(9))
np.reshape(X, (3,3))
# or: 
X.reshape((3,3))
nice array: 
 [[1 2 3]
 [4 5 6]] 
The shape was (2, 3) 

zeros:
 [[ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]

after setting X[0,1] to 999:
[[   0.  999.    0.]
 [   0.    0.    0.]
 [   0.    0.    0.]]
Out[1]:
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
In [2]:
np.linspace(1,2,11)   # 11 points between 0 and and 1 inclusive
Out[2]:
array([ 1. ,  1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8,  1.9,  2. ])

Of course we could have done this with list comprehensions ([1 + 0.1*x for x in range(11)]) but anything you do in numpy will be faster.

Vectorization:

In [3]:
X = np.array(range(9))
print(X)
print(X + 10)
[0 1 2 3 4 5 6 7 8]
[10 11 12 13 14 15 16 17 18]
In [4]:
np.array([1,2,3]) + np.array([4,5,6])
Out[4]:
array([5, 7, 9])

Numpy figures out how to use the function with the array you gave.


But maybe you want to control it youself:

In [5]:
arr = np.array(range(9)).reshape(3,3) + 1
In [6]:
arr
Out[6]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
In [7]:
np.sum(arr)
Out[7]:
45
In [8]:
np.apply_along_axis(np.sum, 0, arr)
Out[8]:
array([12, 15, 18])
In [9]:
np.apply_along_axis(np.sum, 1, arr)
Out[9]:
array([ 6, 15, 24])

There is also: np.apply_over_axis

Plotting with Matplotlib

This is very very similar to Matlab's plotting functions.

In [10]:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

from math import pi
import math
In [11]:
xs = np.linspace(0, 2*pi, 200)
ys = np.cos(xs)   
# alternatively, we could have done:  ys = np.vectorize(math.cos)(xs)
zs = np.sin(xs)
In [12]:
plt.plot(xs, ys)
plt.plot(xs, zs)
Out[12]:
[<matplotlib.lines.Line2D at 0x10cc87588>]

Pictures and the Faces Dataset:

In [13]:
X = np.genfromtxt("faces.txt", delimiter=None) # load the face dataset
In [14]:
X.shape
Out[14]:
(4916, 576)

There are 4916 rows, 576 columns. Each row is actually a 24x24 image.

In [15]:
im = X[0].reshape(24,24)
In [16]:
plt.imshow(im, cmap="gray", interpolation = "none")
Out[16]:
<matplotlib.image.AxesImage at 0x117f2aa90>
In [17]:
im = im.transpose()  # it was sideways, so let's transpose
# we could also have said:    im.T
In [18]:
plt.imshow(im, cmap="gray", interpolation = "none")
Out[18]:
<matplotlib.image.AxesImage at 0x118344f60>
In [19]:
# Let's make our lives easier with a helper function
def showimg(imvec):
    plt.figure(figsize=(2,2))
    img = np.reshape(imvec,(24,24)) # convert vectorized data point t 
    plt.imshow( img.T , cmap="gray", interpolation="none")
    
In [20]:
startface = 50
for i in range(startface, startface+10):
    showimg(X[i,:])

Average Face:

In [21]:
avg_face = np.apply_along_axis(np.mean, 0, X)   # axis 0 is the first axis
avg_face_image = np.reshape(avg_face, (24, 24))
plt.imshow(avg_face_image.transpose(), cmap="gray", interpolation="none")
Out[21]:
<matplotlib.image.AxesImage at 0x118543438>
In [22]:
plt.imshow(avg_face_image.transpose(), cmap="gray")
Out[22]:
<matplotlib.image.AxesImage at 0x11819a668>