Last time, we looked at Numpy arrays, matplotlib, showing, reshaping an image dataset, and applying functions along an axis of a numpy array.
Today:
# as always:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
X = np.array(range(9)).reshape((3,3))
We already know the slicing notation:
X[:]
X[:1]
X[0:2,1:3]
We can also use it to set values:
X[:,1] = np.zeros([3])
X
X[0:2,1:3] += 10
X
xs = [0,1,2,3]
ys = xs[0:2]
ys[0] = 999
print(xs, ys)
On the other hand, with numpy arrays:
X = np.array([0,1,2,3])
Y = X[0:2]
Y[0] = 999
print(X, Y)
In lists, slicing always makes a fresh copy, whereas for numpy arrays, slicing makes a reference to that part of the array. This makes sense because if you have some huge dataset, you don't want to copy the whole dataset when you want to look at a slice of it.
from scipy.ndimage import imread
G = imread("gauss.jpg")
G.shape
plt.imshow(G[:,:,:])
G_bw = np.apply_along_axis(func1d=np.mean, axis=2, arr=G)
G_bw.shape
plt.imshow(G_bw, cmap="gray")
We want to see how much of each color (each shade of gray) there is in the above picture. One way to do this is to look at the histogram.
print(G_bw[:5,:5])
mi = np.min(G_bw)
mi
Let's flatten and normalize the picture too:
G_bw.shape
G_bw = G_bw.reshape((-1,)) # -1 means: "FIGURE IT OUT NUMPY!!!"
G_bw.shape
This normalizes the data: the min is 0, the max is 1, and everything else is in between.
G_bw = G_bw - np.min(G_bw)
G_bw = G_bw / np.max(G_bw)
print(G_bw[:10])
plt.xlabel('Color Intensity')
plt.ylabel('Probability that a random pixel is in each range')
plt.title("Histogram of the array")
n, bins, patches = plt.hist(G_bw, 40, facecolor='pink')