1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 23 14:45:21 2015
@author: Camil Staps, s4498062
This is Python 2 code.
"""
import sys
sys.path.insert(0, './packages')
import numpy as np
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
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']
# 4.1.2
entropies, purities, rands, jaccards = [], [], [], []
for i in range(1, 11):
_, label, _ = cluster.k_means(X, i)
entropy, purity, rand, jaccard = clusterVal(y, label)
entropies.append(entropy)
purities.append(purity)
rands.append(rand)
jaccards.append(jaccard)
x = np.arange(1,11)
plt.figure(figsize=(8,8))
plt.subplot(2,2,1)
plt.plot(x, entropies, label='Entropy')
plt.legend()
plt.subplot(2,2,2)
plt.plot(x, purities, label='Purity')
plt.legend(loc=4)
plt.subplot(2,2,3)
plt.plot(x, rands, label='Rand')
plt.legend(loc=4)
plt.subplot(2,2,4)
plt.plot(x, jaccards, label='Jaccard')
plt.legend(loc=4)
plt.show()
# 4.1.3
faces = sciio.loadmat('./data/wildfaces.mat')
X = faces['X']
k = 10
centroid, label, inertia = cluster.k_means(X, k)
n = 10
plt.figure(figsize=(n*2,4))
for k in range(0,n):
plt.subplot(2, n, k + 1)
plt.imshow(np.reshape(X[k,:], (3,40,40)).T)
plt.axis('off')
plt.subplot(2, n, k + 1 + n)
plt.imshow(np.reshape(centroid[label[k],:], (3,40,40)).T)
plt.axis('off')
plt.show()
# 4.1.4
digits = sciio.loadmat('./data/digits.mat')
X = digits['X']
k = 10
plt.figure(figsize=(6,4))
for k in range(0,24):
plt.subplot(4, 6, k + 1)
plt.imshow(np.reshape(X[k], (16,16)), cmap=plt.cm.binary)
plt.axis('off')
plt.show()
centroid, label, inertia = cluster.k_means(X, k)
plt.figure(figsize=(6,4))
for k in range(0,24):
plt.subplot(4, 6, k + 1)
plt.imshow(np.reshape(centroid[label[k]], (16,16)), cmap=plt.cm.binary)
plt.axis('off')
plt.show()
|