예제 #1
0
    aic=computeAIC(iner,k,cols)
    if aic<minaic:
        minaic=aic
        numclust=k

km2=KMeans(n_clusters=numclust,random_state=1).fit(imgs_vectors)
km2.predict(imgs_vectors)
Zs=KMeans(n_clusters=numclust,random_state=1).fit_predict(imgs_vectors)

# Plots Cluster Examples
for k in np.unique(Zs):
    plt.figure(k)
    if np.sum(Zs==k)>0:
      datacases=np.append(datacases,len(imgs_vectors[Zs == k, :]))
  	  
      util.plot_img_array(imgs_vectors[Zs==k,:], img_size,grey=True)
  	
    plt.suptitle("Cluster Examples %d/%d"%(k,numclust))
    name="../Figures/Cluster_Examples"+str(k)+"by"+str(numclust)+".pdf"
    plt.savefig(name)
    plt.clf()
    plt.cla()
    plt.close()

#plt.show()


# Plots Cluster Centers
plt.figure(1)
util.plot_img_array(km2.cluster_centers_, img_size,grey=True)
plt.suptitle("Cluster Centers")
#If you learn a model that producces an explicit image basis,
#load the basis elements into the array B below. The shape of the
#array should be the number of components in the basis times
#patch_size[0]*patch_size[1]*3 (the length of one row of data_noisy).
B = np.random.rand(100, 300)

#This function takes your de-denoised data and re-assembles it into
#a complete color image
img_denoised = util.patchToImage(
    util.vectorsToPatches(data_denoised, patch_size), (900, 1200))

#These functions compute the MAE between the clean image,the noisy image and the de-noised image
print "Error of Noisy to Clean: %.4f" % util.eval_recon(img_clean, img_noisy)
print "Error of De-Noised to Clean: %.4f" % util.eval_recon(
    img_clean, img_denoised)

#Plot the clean and noisy images
plt.figure(0, figsize=(7, 3))
util.plot_pair(img_clean, img_noisy, "Clean", "Noisy")

#Plot the clean and de-noised images
plt.figure(1, figsize=(7, 3))
util.plot_pair(img_clean, img_denoised, "Clean", "De-Noised")

#Plot the image basis
plt.figure(2, figsize=(4, 3))
util.plot_img_array(B, patch_size)
plt.suptitle("Image Basis")
plt.show()
#Load the data. imgs is an array of shape (N,8,8) where each 8x8 array
#corresponds to an image. imgs_vectors has shape (N,64) where each row
#corresponds to a single-long-vector respresentation of the corresponding image.
img_size = (8, 8)
imgs, imgs_vectors = util.loadDataQ1()

#You should use the data in imgs_vectors to learn a clustering model.
#This starter code uses random clusters.
K = 5
N = imgs_vectors.shape[0]
Zs = np.random.random_integers(1, K, N)

#The code below shows how to plot examples from clusters as an image array
for k in np.unique(Zs):
    plt.figure(k)
    if np.sum(Zs == k) > 0:
        util.plot_img_array(imgs_vectors[Zs == k, :], img_size, grey=True)
    plt.suptitle("Cluster Exmplars %d/%d" % (k, K))
plt.show()

#The code below shows how to compute and plot cluster centers as an image array
centers = np.zeros((len(np.unique(Zs)), 64))
plt.figure(1)
i = 0
for k in np.unique(Zs):
    centers[i, :] = np.mean(imgs_vectors[Zs == k, :], axis=0)
    i = i + 1
util.plot_img_array(centers, img_size, grey=True)
plt.suptitle("Cluster Centers (K=%d)" % K)
plt.show()
#"""

#If you learn a model that producces an explicit image basis,
#load the basis elements into the array B below. The shape of the
#array should be the number of components in the basis times
#patch_size[0]*patch_size[1]*3 (the length of one row of data_noisy).
B = components

#This function takes your de-denoised data and re-assembles it into
#a complete color image
img_denoised  = util.patchToImage(util.vectorsToPatches(data_denoised,patch_size),(900,1200))

#These functions compute the MAE between the clean image,the noisy image and the de-noised image
print "Error of Noisy to Clean: %.4f"%util.eval_recon(img_clean,img_noisy)
print "Error of De-Noised to Clean: %.4f"%util.eval_recon(img_clean,img_denoised)

#Plot the clean and noisy images
plt.figure(0,figsize=(7,3))
util.plot_pair(img_clean,img_noisy,"Clean","Noisy")

#Plot the clean and de-noised images
plt.figure(1,figsize=(7,3))
util.plot_pair(img_clean,img_denoised,"Clean","De-Noised")

#Plot the image basis
plt.figure(2,figsize=(4,3))
util.plot_img_array(B,patch_size)
plt.suptitle("Image Basis")
plt.show()

##output results models
Zs = cluster.SpectralClustering(n_clusters=K,eigen_solver='arpack', affinity="nearest_neighbors", random_state=random_state).fit_predict(imgs_vectors)

## print number count of each cluster
for k in np.unique(Zs):
	print str(k) +" num of it ", len([ks for ks in Zs if ks==k])





#The code below shows how to plot examples from clusters as an image array
for k in np.unique(Zs):
	count = len([ks for ks in Zs if ks==k])
	plt.figure(k)
	if np.sum(Zs==k)>0:
  	  util.plot_img_array(imgs_vectors[Zs==k,:], img_size,grey=True)
  	plt.suptitle("Cluster Exmplars %d/%d - num of this: %d"%(k,K, count))
#plt.show()

#The code below shows how to compute and plot cluster centers as an image array
centers = np.zeros((len(np.unique(Zs)),64))
plt.figure(1)
i=0
for k in np.unique(Zs):
    centers[i,:] = np.mean(imgs_vectors[Zs==k,:],axis=0)
    i=i+1
util.plot_img_array(centers, img_size,grey=True)
plt.suptitle("Cluster Centers (K=%d)"%K)
#plt.show()