0% found this document useful (0 votes)
17 views3 pages

Week 2 exercises-SOLN

Uploaded by

zyl178377
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Week 2 exercises-SOLN

Uploaded by

zyl178377
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Week 2 Exercises

• The exercise below should be completed using tools provided by numpy arrays.
• You can use other given/developed functions from class notebook
#import numpy as np
%pylab inline
from pathlib import Path
import requests
import gzip
from sklearn.datasets import make_blobs
from scipy import stats as st

Populating the interactive namespace from numpy and matplotlib

Exercise 1
• Create a function get_MNIST() that downloads (or loads) the MNIST data, reshapes it,
and stores it appropriately into the variables images and labels.
def get_MNIST():

#download data
mnist_url = "http://yann.lecun.com/exdb/mnist/"
img_file = "train-images-idx3-ubyte.gz"
labels_file = "train-labels-idx1-ubyte.gz"

for fname in [img_file, labels_file]:


if Path(fname).is_file() :
#print(f"Found: {fname}")
continue
#print(f"Downloading: {fname}")
r = requests.get(mnist_url + fname)
with open(fname, 'wb') as foo:
foo.write(r.content)

#open images and place into images variable


with gzip.open(img_file, 'rb') as foo:
f = foo.read()
images = np.array([b for b in f[16:]]).reshape(-1, 28*28)
with gzip.open("train-labels-idx1-ubyte.gz", 'rb') as foo:
f = foo.read()
labels = np.array([b for b in f[8:]])

return images,labels

images,labels = get_MNIST()
Exercise 2
• Create a function visualize_6_digits() that creates a figure with subpanels to visualize 6
MNIST images. The 6 indices must be passed as an input.
def visualize_6_digits(image_ids):
fig,ax = plt.subplots(1,6,figsize=(12,12))
for i in range(6):
ax[i].imshow(images[image_ids[i]].reshape(28,28),cmap="Greys")
return

image_ids = [5,2,10,44,25,5]
visualize_6_digits(image_ids)

Exercise 3
• Create a find_kNN() function and apply it to the example blob dataset to correctly find
the 10 nearest neighbors.
• [Hint, use your function from last week that computes Euclidean distances.]
def dist(X, w):
return sqrt(sum( (X-w)**2,axis=1))

def find_kNN(p,k):
return argsort(dist(X, p))[:k]

X, y = make_blobs(
n_samples = 200,
n_features = 2,
centers = 5,
cluster_std = 1,
random_state = 1
)

w = array([0, 0])
nearest_neighbors = find_kNN(p=w,k=10)
nearest_neighbors

array([ 79, 74, 43, 90, 71, 45, 185, 176, 22, 96])
scatter(X[:,0],X[:,1],c=y);
scatter(w[0],w[1],marker='*',c='black',s=100)
scatter(X[nearest_neighbors,0],X[nearest_neighbors,1],s=1,c='black',);

You might also like