forked from ganml/dcpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfsc.py
More file actions
133 lines (118 loc) · 4.21 KB
/
fsc.py
File metadata and controls
133 lines (118 loc) · 4.21 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 27 21:38:19 2025
@author: gjgan
"""
import numpy as np
import matplotlib.pyplot as plt
from ucimlrepo import fetch_ucirepo
from dcutil import createCM
from kmeans import kmeans2
def fsc(X, k=3, alpha=2, tol=1e-6, maxit=100):
if alpha <= 1:
raise ValueError("Invalid alpha")
X = np.ascontiguousarray(X)
n, d = X.shape
epsilon = 1e-8
ind = np.random.choice(n, k, replace=False)
clusterCenters = X[ind,:]
featureWeight = np.ones((k, d)) / d
dm = np.zeros((n, k))
for i in range(k):
dm[:,i] = np.sum(np.multiply(np.square(X-clusterCenters[i,:]), d**(-alpha)), axis=1)
clusterMembership = np.argmin(dm, axis=1)
objectiveValue = np.sum(dm[list(range(n)), clusterMembership]).item()
numIter = 1
while numIter < maxit:
# update feature weight
for i in range(k):
bInd = clusterMembership==i
if np.any(bInd):
dv = np.pow(np.sum(np.square(X[bInd,:]-clusterCenters[i,:])+epsilon, axis=0), -1/(alpha-1))
featureWeight[i,:] = dv / np.sum(dv)
else:
featureWeight[i,:] = 1/d
# update cluster centers
for i in range(k):
bInd = clusterMembership==i
if np.any(bInd):
clusterCenters[i,:] = np.mean(X[bInd], axis=0)
else:
clusterCenters[i,:] = X[np.random.randint(0, n),:]
# update cluster membership
for i in range(k):
dm[:,i] = np.sum(np.multiply(np.square(X-clusterCenters[i,:]), featureWeight[clusterMembership,:]**alpha), axis=1)
clusterMembership = np.argmin(dm, axis=1)
objectiveValue_ = objectiveValue
objectiveValue = np.sum(dm[list(range(n)), clusterMembership]).item()
numIter += 1
if np.abs(objectiveValue-objectiveValue_) < tol:
break;
return clusterMembership, clusterCenters, featureWeight, objectiveValue, numIter
def fsc2(X, k=3, alpha=2, numrun=10, maxit=100):
bestCM, bestCC, bestFW, bestOV, bestIters = fsc(X, k=k, alpha=alpha, maxit=maxit)
print([bestOV, bestIters])
for i in range(numrun-1):
cm, cc, fw, ov, iters = fsc(X, k=k, alpha=alpha, maxit=maxit)
print([ov, iters])
if ov < bestOV:
bestCM, bestCC, bestFW, bestOV, bestIters = cm, cc, fw, ov, iters
return bestCM, bestCC, bestFW, bestOV, bestIters
# examples
# synthetic data
np.random.seed(1)
X = np.zeros((300, 3))
y = np.zeros(300, dtype=int)
for i in range(3):
ind = [100*i+j for j in range(100)]
X[ind,:] = np.random.rand(100,3)*2 + 4*i
X[ind,i] = np.random.rand(100)*12
y[ind] = i
ind = np.random.permutation(list(range(300)))
X = X[ind,:]
y = y[ind]
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(projection='3d')
ax.scatter(X[:,0], X[:,1],X[:,2], color="k", s=12)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.savefig("3d.pdf", bbox_inches='tight')
yhat, cc, fw, ov, iters = fsc2(X, k=3, alpha=2, numrun=100)
cm1 = createCM(y, yhat)
print([ov, iters])
print(cm1)
print(np.array_str(fw, precision=4, suppress_small=True))
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(projection='3d')
markers = ["s", "+", "."]
for i in range(3):
ind = yhat==i
ax.scatter(X[ind,0], X[ind,1],X[ind,2], color="black", s=16, marker=markers[i])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.savefig("3dfsc.pdf", bbox_inches='tight')
yhat, bcc, bov, biters = kmeans2(X)
cm2 = createCM(y, yhat)
print([bov, biters])
print(cm2)
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(projection='3d')
markers = ["s", "+", "."]
for i in range(3):
ind = yhat==i
ax.scatter(X[ind,0], X[ind,1],X[ind,2], color="black", s=16, marker=markers[i])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.savefig("3dkmeans.pdf", bbox_inches='tight')
# iris data
iris = fetch_ucirepo(id=53)
X = iris.data.features
y = iris.data.targets
yhat, cc, fw, ov, iters = fsc2(X, k=3, alpha=3)
cm1 = createCM(y, yhat)
print([ov, iters])
print(cm1)
print(np.array_str(fw, precision=4, suppress_small=True))