-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
100 lines (63 loc) · 1.78 KB
/
main.py
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
99
100
import numpy as np
import sklearn as sk
import sklearn.datasets as ds
import skfuzzy as fuzz
from plot import plot
import cmeans
def generate_data(num_samples, num_features, c, shuffle=True):
x = ds.make_blobs(num_samples, num_features, c, shuffle=False)[0]
x = x.T
labels = np.zeros(num_samples)
j = int(num_samples / c)
for i in range(c):
p = i * j
q = (i + 1) * j
print()
labels[p:q] = i
return x, labels
def verify_clusters(x, c, v, u, labels):
ssd_actual = 0
for i in range(c):
# All points in class
x1 = x[labels == i]
# Mean of class
m = np.mean(x1, axis=0)
for pt in x1:
ssd_actual += np.linalg.norm(pt - m)
clm = np.argmax(u, axis=0)
ssd_clusters = 0
for i in range(c):
# Points clustered in a class
x2 = x[clm == i]
for pt in x2:
ssd_clusters += np.linalg.norm(pt - v[i])
print(ssd_clusters / ssd_actual)
num_samples = 3000
num_features = 2
c = 3
fuzzifier = 1.2
error = 0.001
maxiter = 100
# np.random.seed(100)
x, labels = generate_data(num_samples, num_features, c, shuffle=False)
v, v0, u, u0, d, t = cmeans.fcm(x, c, fuzzifier, error, maxiter)
plot(x.T, v, u, c)
print("Blobs")
verify_clusters(x.T, c, v, u, labels)
iris = ds.load_iris()
labels = iris.target_names
target = iris.target
iris = np.array(iris.data).T
c = 3
v, v0, u, u0, d, t = cmeans.fcm(iris, c, fuzzifier, error, maxiter)
iris = iris.T
print("Iris")
verify_clusters(iris, c, v, u, target)
digits = ds.load_digits()
labels = digits.target
digits = np.array(digits.data).T
c = 10
v, v0, u, u0, d, t = cmeans.pcm(digits, c, fuzzifier, error, maxiter)
print("Digits")
verify_clusters(digits.T, c, v, u, labels)
plot(digits.T, v, u, c)