From 2a13f375d49d9175e16b1f0cd953d347bcca43dc Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Sun, 29 Nov 2015 18:26:05 +0100 Subject: Finish assignment 4 --- Assignment 4/ex41.py | 24 +++++++++++++++--------- Assignment 4/packages/clusterPlot.py | 9 ++++----- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Assignment 4/ex41.py b/Assignment 4/ex41.py index 5ae66db..c3a9fc5 100644 --- a/Assignment 4/ex41.py +++ b/Assignment 4/ex41.py @@ -11,19 +11,27 @@ import sys sys.path.insert(0, './packages') import numpy as np -from scipy import io as sciio +import scipy.io as sciio from sklearn import cluster from clusterPlot import clusterPlot from clusterVal import clusterVal import matplotlib.pyplot as plt # 4.1.1 -n = 1 +plt.figure(figsize=(16,10)) +for n in range(1,5): + synth = sciio.loadmat('./data/synth' + str(n) + '.mat') + X = synth['X'] + y = synth['y'] + centroid, label, inertia = cluster.k_means(X, 4) + plt.subplot(2, 2, n) + clusterPlot(X, label, centroid, y) +plt.show() + +n = 1 synth = sciio.loadmat('./data/synth' + str(n) + '.mat') X = synth['X'] y = synth['y'] -centroid, label, inertia = cluster.k_means(X, 4) -clusterPlot(X, label, centroid, y) # 4.1.2 entropies, purities, rands, jaccards = [], [], [], [] @@ -35,8 +43,6 @@ for i in range(1, 11): rands.append(rand) jaccards.append(jaccard) -print(entropies, purities, rands, jaccards) - x = np.arange(1,11) plt.figure(figsize=(8,8)) plt.subplot(2,2,1) @@ -56,8 +62,8 @@ plt.show() # 4.1.3 faces = sciio.loadmat('./data/wildfaces.mat') X = faces['X'] -k = 0 -centroid, label, inertia = cluster.k_means(X, 10) +k = 10 +centroid, label, inertia = cluster.k_means(X, k) n = 10 plt.figure(figsize=(n*2,4)) @@ -73,7 +79,7 @@ plt.show() # 4.1.4 digits = sciio.loadmat('./data/digits.mat') X = digits['X'] -k = 20 +k = 10 plt.figure(figsize=(6,4)) for k in range(0,24): diff --git a/Assignment 4/packages/clusterPlot.py b/Assignment 4/packages/clusterPlot.py index 2f37a3d..4abc2c0 100644 --- a/Assignment 4/packages/clusterPlot.py +++ b/Assignment 4/packages/clusterPlot.py @@ -4,7 +4,7 @@ Created on Mon Apr 14 09:01:18 2014 """ -def clusterPlot(X, clusterid, centroids='None', y='None', covars='None', figsize=(16,10)): +def clusterPlot(X, clusterid, centroids='None', y='None', covars='None', figsize=None): ''' CLUSTERPLOT Plots a clustering of a data set as well as the true class labels. If data is more than 2-dimensional it should be first projected @@ -27,7 +27,7 @@ def clusterPlot(X, clusterid, centroids='None', y='None', covars='None', figsize covars M-by-M-by-K tensor of covariance matrices (optional) ''' import numpy as np - from matplotlib.pyplot import figure, cm, plot, hold, legend, xlim, show + from matplotlib.pyplot import figure, cm, plot, hold, legend, xlim X = np.asarray(X) @@ -43,7 +43,8 @@ def clusterPlot(X, clusterid, centroids='None', y='None', covars='None', figsize ncolors = np.max([C,K]) # plot data points color-coded by class, cluster markers and centroids - figure(figsize=figsize) + if figsize != None: + figure(figsize=figsize) hold(True) colors = [0]*ncolors for color in range(ncolors): @@ -71,5 +72,3 @@ def clusterPlot(X, clusterid, centroids='None', y='None', covars='None', figsize legend(legend_items, numpoints=1, markerscale=.75, prop={'size': 9}) xlim(X[:,0].min()*1.1, X[:,0].max()*1.2) - - show() -- cgit v1.2.3