-
Notifications
You must be signed in to change notification settings - Fork 8
/
fcm.py
19 lines (17 loc) · 680 Bytes
/
fcm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import numpy as np
from cvi import *
def fcm_get_u(x, v, m):
distances = pairwise_squared_distances(x, v)
nonzero_distances = np.fmax(distances, np.finfo(np.float64).eps)
inv_distances = np.reciprocal(nonzero_distances)**(1/(m - 1))
return inv_distances.T/np.sum(inv_distances, axis=1)
def fcm(x, c, m=2.0, v=None, max_iter=100, error=0.05):
if v is None: v = x[np.random.randint(x.shape[0], size=c)]
u = fcm_get_u(x, v, m)
for iteration in range(max_iter):
u_old = u
um = u**m
v = np.dot(um, x)/np.sum(um, axis=1, keepdims=True)
u = fcm_get_u(x, v, m)
if np.linalg.norm(u - u_old) < error: break
return v