Skip to content

Commit 66e5ed7

Browse files
authored
Add files via upload
All Initial files uploaded
1 parent 401f383 commit 66e5ed7

File tree

90 files changed

+23421
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+23421
-0
lines changed

1-MLiP2017_Introduction.pdf

1.4 MB
Binary file not shown.

Assignments/MLiP2017-ex1-CIFAR10-and-Neural-Networks.ipynb

Lines changed: 589 additions & 0 deletions
Large diffs are not rendered by default.
Binary file not shown.

MLiP2017_Specification.pdf

244 KB
Binary file not shown.
4.06 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

nbs/fig_codes/figures.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import warnings
4+
5+
6+
def plot_kmeans_interactive(min_clusters=1, max_clusters=6):
7+
#from IPython.html.widgets import interact
8+
from ipywidgets import interact
9+
from sklearn.metrics.pairwise import euclidean_distances
10+
from sklearn.datasets.samples_generator import make_blobs
11+
12+
with warnings.catch_warnings():
13+
warnings.filterwarnings('ignore')
14+
15+
X, y = make_blobs(n_samples=300, centers=4,
16+
random_state=0, cluster_std=0.60)
17+
18+
def _kmeans_step(frame=0, n_clusters=4):
19+
rng = np.random.RandomState(2)
20+
labels = np.zeros(X.shape[0])
21+
centers = rng.randn(n_clusters, 2)
22+
23+
nsteps = frame // 3
24+
25+
for i in range(nsteps + 1):
26+
old_centers = centers
27+
if i < nsteps or frame % 3 > 0:
28+
dist = euclidean_distances(X, centers)
29+
labels = dist.argmin(1)
30+
31+
if i < nsteps or frame % 3 > 1:
32+
centers = np.array([X[labels == j].mean(0)
33+
for j in range(n_clusters)])
34+
nans = np.isnan(centers)
35+
centers[nans] = old_centers[nans]
36+
37+
38+
# plot the data and cluster centers
39+
plt.scatter(X[:, 0], X[:, 1], c=labels, s=50, cmap='rainbow',
40+
vmin=0, vmax=n_clusters - 1);
41+
plt.scatter(old_centers[:, 0], old_centers[:, 1], marker='o',
42+
c=np.arange(n_clusters),
43+
s=200, cmap='rainbow')
44+
plt.scatter(old_centers[:, 0], old_centers[:, 1], marker='o',
45+
c='black', s=50)
46+
47+
# plot new centers if third frame
48+
if frame % 3 == 2:
49+
for i in range(n_clusters):
50+
plt.annotate('', centers[i], old_centers[i],
51+
arrowprops=dict(arrowstyle='->', linewidth=1))
52+
plt.scatter(centers[:, 0], centers[:, 1], marker='o',
53+
c=np.arange(n_clusters),
54+
s=200, cmap='rainbow')
55+
plt.scatter(centers[:, 0], centers[:, 1], marker='o',
56+
c='black', s=50)
57+
58+
plt.xlim(-4, 4)
59+
plt.ylim(-2, 10)
60+
61+
if frame % 3 == 1:
62+
plt.text(3.8, 9.5, "1. Reassign points to nearest centroid",
63+
ha='right', va='top', size=14)
64+
elif frame % 3 == 2:
65+
plt.text(3.8, 9.5, "2. Update centroids to cluster means",
66+
ha='right', va='top', size=14)
67+
68+
69+
return interact(_kmeans_step, frame=np.arange(0, 50),
70+
n_clusters=np.arange(min_clusters, max_clusters))
71+
72+
73+
def plot_image_components(x, coefficients=None, mean=0, components=None,
74+
imshape=(8, 8), n_components=6, fontsize=12):
75+
if coefficients is None:
76+
coefficients = x
77+
78+
if components is None:
79+
components = np.eye(len(coefficients), len(x))
80+
81+
mean = np.zeros_like(x) + mean
82+
83+
84+
fig = plt.figure(figsize=(1.2 * (5 + n_components), 1.2 * 2))
85+
g = plt.GridSpec(2, 5 + n_components, hspace=0.3)
86+
87+
def show(i, j, x, title=None):
88+
ax = fig.add_subplot(g[i, j], xticks=[], yticks=[])
89+
ax.imshow(x.reshape(imshape), interpolation='nearest')
90+
if title:
91+
ax.set_title(title, fontsize=fontsize)
92+
93+
show(slice(2), slice(2), x, "True")
94+
95+
approx = mean.copy()
96+
show(0, 2, np.zeros_like(x) + mean, r'$\mu$')
97+
show(1, 2, approx, r'$1 \cdot \mu$')
98+
99+
for i in range(0, n_components):
100+
approx = approx + coefficients[i] * components[i]
101+
show(0, i + 3, components[i], r'$c_{0}$'.format(i + 1))
102+
show(1, i + 3, approx,
103+
r"${0:.2f} \cdot c_{1}$".format(coefficients[i], i + 1))
104+
plt.gca().text(0, 1.05, '$+$', ha='right', va='bottom',
105+
transform=plt.gca().transAxes, fontsize=fontsize)
106+
107+
show(slice(2), slice(-2, None), approx, "Approx")
108+
109+
110+
def plot_pca_interactive(data, n_components=6):
111+
from sklearn.decomposition import PCA
112+
#from IPython.html.widgets import interact
113+
from ipywidgets import interact
114+
115+
pca = PCA(n_components=n_components)
116+
Xproj = pca.fit_transform(data)
117+
118+
def show_decomp(i=0):
119+
plot_image_components(data[i], Xproj[i],
120+
pca.mean_, pca.components_)
121+
122+
interact(show_decomp, i=(0, data.shape[0] - 1));

nbs/fig_codes/plot_2d_separator.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
5+
def plot_2d_separator(classifier, X, fill=False, ax=None, eps=None):
6+
if eps is None:
7+
eps = X.std() / 2.
8+
x_min, x_max = X[:, 0].min() - eps, X[:, 0].max() + eps
9+
y_min, y_max = X[:, 1].min() - eps, X[:, 1].max() + eps
10+
xx = np.linspace(x_min, x_max, 100)
11+
yy = np.linspace(y_min, y_max, 100)
12+
13+
X1, X2 = np.meshgrid(xx, yy)
14+
X_grid = np.c_[X1.ravel(), X2.ravel()]
15+
try:
16+
decision_values = classifier.decision_function(X_grid)
17+
levels = [0]
18+
fill_levels = [decision_values.min(), 0, decision_values.max()]
19+
except AttributeError:
20+
# no decision_function
21+
decision_values = classifier.predict_proba(X_grid)[:, 1]
22+
levels = [.5]
23+
fill_levels = [0, .5, 1]
24+
25+
if ax is None:
26+
ax = plt.gca()
27+
if fill:
28+
ax.contourf(X1, X2, decision_values.reshape(X1.shape),
29+
levels=fill_levels, colors=['blue', 'red'])
30+
else:
31+
ax.contour(X1, X2, decision_values.reshape(X1.shape), levels=levels,
32+
colors="black")
33+
ax.set_xlim(x_min, x_max)
34+
ax.set_ylim(y_min, y_max)
35+
ax.set_xticks(())
36+
ax.set_yticks(())
37+
38+
39+
if __name__ == '__main__':
40+
from sklearn.datasets import make_blobs
41+
from sklearn.linear_model import LogisticRegression
42+
X, y = make_blobs(centers=2, random_state=42)
43+
clf = LogisticRegression().fit(X, y)
44+
plot_2d_separator(clf, X, fill=True)
45+
plt.scatter(X[:, 0], X[:, 1], c=y)
46+
plt.show()

nbs/fig_codes/plot_digits_dataset.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Taken from example in scikit-learn examples
2+
# Authors: Fabian Pedregosa <fabian.pedregosa@inria.fr>
3+
# Olivier Grisel <olivier.grisel@ensta.org>
4+
# Mathieu Blondel <mathieu@mblondel.org>
5+
# Gael Varoquaux
6+
# License: BSD 3 clause (C) INRIA 2011
7+
8+
import numpy as np
9+
import matplotlib.pyplot as plt
10+
from matplotlib import offsetbox
11+
from sklearn import datasets, decomposition
12+
13+
14+
def digits_plot():
15+
digits = datasets.load_digits(n_class=6)
16+
n_digits = 500
17+
X = digits.data[:n_digits]
18+
y = digits.target[:n_digits]
19+
n_samples, n_features = X.shape
20+
21+
def plot_embedding(X, title=None):
22+
x_min, x_max = np.min(X, 0), np.max(X, 0)
23+
X = (X - x_min) / (x_max - x_min)
24+
25+
plt.figure()
26+
ax = plt.subplot(111)
27+
for i in range(X.shape[0]):
28+
plt.text(X[i, 0], X[i, 1], str(digits.target[i]),
29+
color=plt.cm.Set1(y[i] / 10.),
30+
fontdict={'weight': 'bold', 'size': 9})
31+
32+
if hasattr(offsetbox, 'AnnotationBbox'):
33+
# only print thumbnails with matplotlib > 1.0
34+
shown_images = np.array([[1., 1.]]) # just something big
35+
for i in range(X.shape[0]):
36+
dist = np.sum((X[i] - shown_images) ** 2, 1)
37+
if np.min(dist) < 1e5:
38+
# don't show points that are too close
39+
# set a high threshold to basically turn this off
40+
continue
41+
shown_images = np.r_[shown_images, [X[i]]]
42+
imagebox = offsetbox.AnnotationBbox(
43+
offsetbox.OffsetImage(digits.images[i], cmap=plt.cm.gray_r),
44+
X[i])
45+
ax.add_artist(imagebox)
46+
plt.xticks([]), plt.yticks([])
47+
if title is not None:
48+
plt.title(title)
49+
50+
n_img_per_row = 10
51+
img = np.zeros((10 * n_img_per_row, 10 * n_img_per_row))
52+
for i in range(n_img_per_row):
53+
ix = 10 * i + 1
54+
for j in range(n_img_per_row):
55+
iy = 10 * j + 1
56+
img[ix:ix + 8, iy:iy + 8] = X[i * n_img_per_row + j].reshape((8, 8))
57+
58+
plt.imshow(img, cmap=plt.cm.binary)
59+
plt.xticks([])
60+
plt.yticks([])
61+
plt.title('A selection from the 64-dimensional digits dataset')
62+
print("Computing PCA projection")
63+
pca = decomposition.PCA(n_components=2).fit(X)
64+
X_pca = pca.transform(X)
65+
plot_embedding(X_pca, "Principal Components projection of the digits")
66+
plt.figure()
67+
plt.title("First Principal Component")
68+
plt.matshow(pca.components_[0, :].reshape(8, 8), cmap="gray")
69+
plt.axis('off')
70+
plt.figure()
71+
plt.title("Second Principal Component")
72+
plt.matshow(pca.components_[1, :].reshape(8, 8), cmap="gray")
73+
plt.axis('off')
74+
plt.show()

nbs/fig_codes/plot_helpers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from matplotlib.colors import ListedColormap
2+
3+
cm3 = ListedColormap(['#0000aa', '#ff2020', '#50ff50'])
4+
cm2 = ListedColormap(['#0000aa', '#ff2020'])

nbs/fig_codes/plot_pca.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from sklearn.decomposition import PCA
2+
import matplotlib.pyplot as plt
3+
import numpy as np
4+
5+
6+
def plot_pca_illustration():
7+
rnd = np.random.RandomState(5)
8+
X_ = rnd.normal(size=(300, 2))
9+
X_blob = np.dot(X_, rnd.normal(size=(2, 2))) + rnd.normal(size=2)
10+
11+
pca = PCA()
12+
pca.fit(X_blob)
13+
X_pca = pca.transform(X_blob)
14+
15+
S = X_pca.std(axis=0)
16+
17+
fig, axes = plt.subplots(2, 2, figsize=(10, 10))
18+
axes = axes.ravel()
19+
20+
axes[0].set_title("Original data")
21+
axes[0].scatter(X_blob[:, 0], X_blob[:, 1], c=X_pca[:, 0], linewidths=0,
22+
s=60, cmap='viridis')
23+
axes[0].set_xlabel("feature 1")
24+
axes[0].set_ylabel("feature 2")
25+
axes[0].arrow(pca.mean_[0], pca.mean_[1], S[0] * pca.components_[0, 0],
26+
S[0] * pca.components_[0, 1], width=.1, head_width=.3,
27+
color='k')
28+
axes[0].arrow(pca.mean_[0], pca.mean_[1], S[1] * pca.components_[1, 0],
29+
S[1] * pca.components_[1, 1], width=.1, head_width=.3,
30+
color='k')
31+
axes[0].text(-1.5, -.5, "Component 2", size=14)
32+
axes[0].text(-4, -4, "Component 1", size=14)
33+
axes[0].set_aspect('equal')
34+
35+
axes[1].set_title("Transformed data")
36+
axes[1].scatter(X_pca[:, 0], X_pca[:, 1], c=X_pca[:, 0], linewidths=0,
37+
s=60, cmap='viridis')
38+
axes[1].set_xlabel("First principal component")
39+
axes[1].set_ylabel("Second principal component")
40+
axes[1].set_aspect('equal')
41+
axes[1].set_ylim(-8, 8)
42+
43+
pca = PCA(n_components=1)
44+
pca.fit(X_blob)
45+
X_inverse = pca.inverse_transform(pca.transform(X_blob))
46+
47+
axes[2].set_title("Transformed data w/ second component dropped")
48+
axes[2].scatter(X_pca[:, 0], np.zeros(X_pca.shape[0]), c=X_pca[:, 0],
49+
linewidths=0, s=60, cmap='viridis')
50+
axes[2].set_xlabel("First principal component")
51+
axes[2].set_aspect('equal')
52+
axes[2].set_ylim(-8, 8)
53+
54+
axes[3].set_title("Back-rotation using only first component")
55+
axes[3].scatter(X_inverse[:, 0], X_inverse[:, 1], c=X_pca[:, 0],
56+
linewidths=0, s=60, cmap='viridis')
57+
axes[3].set_xlabel("feature 1")
58+
axes[3].set_ylabel("feature 2")
59+
axes[3].set_aspect('equal')
60+
axes[3].set_xlim(-8, 4)
61+
axes[3].set_ylim(-8, 4)
62+
63+
64+
def plot_pca_whitening():
65+
rnd = np.random.RandomState(5)
66+
X_ = rnd.normal(size=(300, 2))
67+
X_blob = np.dot(X_, rnd.normal(size=(2, 2))) + rnd.normal(size=2)
68+
69+
pca = PCA(whiten=True)
70+
pca.fit(X_blob)
71+
X_pca = pca.transform(X_blob)
72+
73+
fig, axes = plt.subplots(1, 2, figsize=(10, 10))
74+
axes = axes.ravel()
75+
76+
axes[0].set_title("Original data")
77+
axes[0].scatter(X_blob[:, 0], X_blob[:, 1], c=X_pca[:, 0], linewidths=0, s=60, cmap='viridis')
78+
axes[0].set_xlabel("feature 1")
79+
axes[0].set_ylabel("feature 2")
80+
axes[0].set_aspect('equal')
81+
82+
axes[1].set_title("Whitened data")
83+
axes[1].scatter(X_pca[:, 0], X_pca[:, 1], c=X_pca[:, 0], linewidths=0, s=60, cmap='viridis')
84+
axes[1].set_xlabel("First principal component")
85+
axes[1].set_ylabel("Second principal component")
86+
axes[1].set_aspect('equal')
87+
axes[1].set_xlim(-3, 4)

0 commit comments

Comments
 (0)