You are on page 1of 26

3/17/2017 HW5

In[38]: import numpy as np

import mltools as ml

import matplotlib.pyplot as plt

import scipy.linalg

iris = np.genfromtxt("data/iris.txt",delimiter=None)

X, Y = iris[:,0:2], iris[:,-1] # get first two features & target

In[13]: plt.plot(X)
plt.show()

le:///Users/devonkenneally/Downloads/HW5.html 1/26
3/17/2017 HW5

In[27]: K = [5,20]
I = ()
for i in range (6):
for k in K:
i = ml.cluster.kmeans(X,k)
if(I == () or I[2] > i[2]):
I = i
ml.plotClassify2D(None,X,i[0])
plt.show()
print "SUMD is" ,i[2]

le:///Users/devonkenneally/Downloads/HW5.html 2/26
3/17/2017 HW5

SUMD is 20.9546301963

SUMD is 6.03797090446

le:///Users/devonkenneally/Downloads/HW5.html 3/26
3/17/2017 HW5

SUMD is 21.5528660333

SUMD is 6.05741136637

le:///Users/devonkenneally/Downloads/HW5.html 4/26
3/17/2017 HW5

SUMD is 25.6541611009

SUMD is 6.45211652596

le:///Users/devonkenneally/Downloads/HW5.html 5/26
3/17/2017 HW5

SUMD is 23.4386437573

SUMD is 6.24938981815

le:///Users/devonkenneally/Downloads/HW5.html 6/26
3/17/2017 HW5

SUMD is 21.5819690003

SUMD is 4.85326284878

In[28]: print "BEST VALUE IS", I[2]

BEST VALUE IS 4.85326284878

le:///Users/devonkenneally/Downloads/HW5.html 7/26
3/17/2017 HW5

In[32]: for k in K:
r1 = ml.cluster.agglomerative(X,k,"min")
r2 = ml.cluster.agglomerative(X,k,"max")
print "K VALUE OF ", k
print "SINGLE LINKAGE"
ml.plotClassify2D(None,X,r1[0])
plt.show()
print "COMPLETE LINKAGE"
ml.plotClassify2D(None,X,r2[0])
plt.show()

le:///Users/devonkenneally/Downloads/HW5.html 8/26
3/17/2017 HW5

K VALUE OF 5
SINGLE LINKAGE

COMPLETE LINKAGE

K VALUE OF 20
SINGLE LINKAGE

le:///Users/devonkenneally/Downloads/HW5.html 9/26
3/17/2017 HW5

COMPLETE LINKAGE

Complete and single linkage aren't too dierent, higher K results in closer clustering. The distance is noticable
smaller than that of K means

le:///Users/devonkenneally/Downloads/HW5.html 10/26
3/17/2017 HW5

In[44]: X = np.genfromtxt("data/faces.txt", delimiter=None) # load face dataset


plt.figure()
# pick a data point i for display
img = np.reshape(X[i,:],(24,24)) # convert vectorized data point to 24x2
4 image patch
plt.imshow( img.T , cmap="gray") # display image patch; you may have to
squint
plt.show()

In[56]: mu = np.mean( X, axis=0, keepdims=True ) # find mean over data points


X0 = X - mu # zero-center the data
U,S,Vh = scipy.linalg.svd(X0, False) # X0 = U * diag(S) * Vh
Xhat = U[:,0:k].dot( np.diag(S[0:k]) ).dot( Vh[0:k,:] ) # approx using k
largest eigendir
mu = np.mean( X, axis=0, keepdims=True ) # find mean over data points
X0 = X - mu # zero-center the data
print "ZERO MEAN IS: ", X0

ZERO MEAN IS: [[ 9.95240033 15.13161107 10.03254679 ..., -18.58


136697
-84.98494711 -94.25386493]
[ -64.04759967 -65.86838893 -61.96745321 ..., -19.58136697
-57.98494711 -97.25386493]
[ -80.04759967 -76.86838893 -74.96745321 ..., -35.58136697
-38.98494711 -40.25386493]
...,
[ -66.04759967 -64.86838893 -64.96745321 ..., -72.58136697
-71.98494711 -68.25386493]
[ -21.04759967 -17.86838893 -15.96745321 ..., -69.58136697
-70.98494711 -72.25386493]
[ -29.04759967 -30.86838893 -28.96745321 ..., 152.41863303
150.01505289 149.74613507]]

le:///Users/devonkenneally/Downloads/HW5.html 11/26
3/17/2017 HW5

In[63]: U,S,vh = scipy.linalg.svd(X0,full_matrices=False)


W = U.dot( np.diag(S) )

X0 = W .dot( vh)

print"RESULT: ", X0

RESULT: [[ 93. 97. 91. ..., 79. 14. 6.]


[ 19. 16. 19. ..., 78. 41. 3.]
[ 3. 5. 6. ..., 62. 60. 60.]
...,
[ 17. 17. 16. ..., 25. 27. 32.]
[ 62. 64. 65. ..., 28. 28. 28.]
[ 54. 51. 52. ..., 250. 249. 250.]]

In[64]: X0s = []
K2 = [1,2,3,4,5,6,7,8,9,10]
for k in K2:
Xhat = U[:,0:k].dot( np.diag(S[0:k]) ).dot( Vh[0:k,:] ) # approx usi
ng k largest eigendir
MSE = np.mean( (X0 - Xhat)**2 )
X0s.append(MSE)
plt.plot(K2,X0s)
plt.show()

le:///Users/devonkenneally/Downloads/HW5.html 12/26
3/17/2017 HW5

In[72]: for j in range(3):


alpha = 2*np.median(np.abs(W[:,j]))
f1 = mu + alpha*X0[j,:]
f2 = mu - alpha*X0[j,:]

img = np.reshape( f1, (24,24) ) # reshape to square


img2 = np.reshape( f2, (24,24) ) # reshape to square
plt.imshow( img.T , cmap="gray") # draw each image
plt.show()
plt.imshow( img2.T , cmap="gray") # draw each image
plt.show()

le:///Users/devonkenneally/Downloads/HW5.html 13/26
3/17/2017 HW5

le:///Users/devonkenneally/Downloads/HW5.html 14/26
3/17/2017 HW5

le:///Users/devonkenneally/Downloads/HW5.html 15/26
3/17/2017 HW5

le:///Users/devonkenneally/Downloads/HW5.html 16/26
3/17/2017 HW5

In[73]: K3 = [5,10,50,100]

for k in K3:
for i in range(2):
Xhat = U[:,0:k].dot( np.diag(S[0:k]) ).dot( Vh[0:k,:] )
alpha = 2*np.median(np.abs(W[:,j]))
f1 = mu + alpha*Xhat[j,:]
f2 = mu - alpha*Xhat[j,:]
img = np.reshape( f1, (24,24) ) # reshape to square
img2 = np.reshape( f2, (24,24) ) # reshape to square
plt.imshow( img.T , cmap="gray") # draw each image
plt.show()
plt.imshow( img2.T , cmap="gray") # draw each image
plt.show()

le:///Users/devonkenneally/Downloads/HW5.html 17/26
3/17/2017 HW5

le:///Users/devonkenneally/Downloads/HW5.html 18/26
3/17/2017 HW5

le:///Users/devonkenneally/Downloads/HW5.html 19/26
3/17/2017 HW5

le:///Users/devonkenneally/Downloads/HW5.html 20/26
3/17/2017 HW5

le:///Users/devonkenneally/Downloads/HW5.html 21/26
3/17/2017 HW5

le:///Users/devonkenneally/Downloads/HW5.html 22/26
3/17/2017 HW5

le:///Users/devonkenneally/Downloads/HW5.html 23/26
3/17/2017 HW5

le:///Users/devonkenneally/Downloads/HW5.html 24/26
3/17/2017 HW5

In[78]: idx = np.random.choice(25, 25, replace=False) # pick some data at random


or otherwise; a list / vector of integer indices
import mltools.transforms
coord,params = ml.transforms.rescale( W[:,0:2] ) # normalize scale of
"W" locations
plt.figure(); plt.hold(True); # you may need this for pyplot
for i in idx:
# compute where to place image (scaled W values) & size
loc = (coord[i,0],coord[i,0]+0.5, coord[i,1],coord[i,1]+0.5)
img = np.reshape( X[i,:], (24,24) ) # reshape to square
plt.imshow( img.T , cmap="gray", extent=loc ) # draw each image
plt.axis( (-2,3,-2,3) ) # se

le:///Users/devonkenneally/Downloads/HW5.html 25/26
3/17/2017 HW5

In[79]: plt.show()

le:///Users/devonkenneally/Downloads/HW5.html 26/26

You might also like