12
votes

Est un algorithme de C-signifie flou disponible pour Python?

J'ai des points dans un espace 3 dimensions et je voudrais les regrouper. Je connais le module Python "Cluster", mais il n'a que K-moyen. Connaissez-vous un module qui a FCM (FUZZY C-MOYEN)?

(Si vous connaissez d'autres modules Python liés au regroupement, vous pouvez les nommer comme un bonus. Mais la question importante est celle d'un algorithme FCM dans Python.)

matlab

Il semble être assez facile d'utiliser FCM dans matlab ( exemple ). N'est-ce pas comme ça disponible pour Python?

numpy, scipe et sage

Je n'ai pas trouvé FCM dans Numpy , Sciped ou Sage . J'ai téléchargé la documentation et la recherche. Aucun résultat

python-cluster

Il semble que le module de cluster ajouta Fuzzy C-moyen avec la prochaine version (voir feuille de route ). Mais j'en ai besoin maintenant


2 commentaires

Je regarderais numpy et sciped .


Certaines des options sous Cette autre question pourrait être utile pour vous.


4 Réponses :


7
votes

7
votes

regarder paquet Scikit-Fuzzy . Il possède la fonctionnalité logique floue de base, y compris la clustering Fuzzy C-Winding.


0 commentaires

1
votes

Je l'ai fait à partir de zéro, en utilisant l'initialisation K ++ (avec des graines fixes et 5 centroïdes. Il ne faut pas être trop difficile de l'adonter à votre nombre de centroïdes souhaité):

# K++ initialization Algorithm:
import random
def initialize(X, K):
    C = [X[0]]
    for k in range(1, K):
        D2 = scipy.array([min([scipy.inner(c-x,c-x) for c in C]) for x in X])
        probs = D2/D2.sum()
        cumprobs = probs.cumsum()
        np.random.seed(20)            # fixxing seeds
        #random.seed(0)               # fixxing seeds
        r = scipy.rand()        
        for j,p in enumerate(cumprobs):
            if r < p:
                i = j
                break
        C.append(X[i])
    return C

a = initialize(data2,5)   # "a" is the centroids initial array... I used 5 centroids

# Now the Fuzzy c means algorithm:
m = 1.5     # Fuzzy parameter (it can be tuned)
r = (2/(m-1))

# Initial centroids:
c1,c2,c3,c4,c5 = a[0],a[1],a[2],a[3],a[4]

# prepare empty lists to add the final centroids:
cc1,cc2,cc3,cc4,cc5 = [],[],[],[],[]

n_iterations = 10000

for j in range(n_iterations):
    u1,u2,u3,u4,u5 = [],[],[],[],[]

    for i in range(len(data2)):
        # Distances (of every point to each centroid):
        a = LA.norm(data2[i]-c1)    
        b = LA.norm(data2[i]-c2)
        c = LA.norm(data2[i]-c3)
        d = LA.norm(data2[i]-c4)
        e = LA.norm(data2[i]-c5)

        # Pertenence matrix vectors:
        U1 = 1/(1 + (a/b)**r + (a/c)**r + (a/d)**r + (a/e)**r) 
        U2 = 1/((b/a)**r + 1 + (b/c)**r + (b/d)**r + (b/e)**r)
        U3 = 1/((c/a)**r + (c/b)**r + 1 + (c/d)**r + (c/e)**r)
        U4 = 1/((d/a)**r + (d/b)**r + (d/c)**r + 1 + (d/e)**r)
        U5 = 1/((e/a)**r + (e/b)**r + (e/c)**r + (e/d)**r + 1)

        # We will get an array of n row points x K centroids, with their degree of pertenence       
        u1.append(U1)
        u2.append(U2)
        u3.append(U3)
        u4.append(U4)
        u5.append(U5)        

    # now we calculate new centers:
    c1 = (np.array(u1)**2).dot(data2) / np.sum(np.array(u1)**2)
    c2 = (np.array(u2)**2).dot(data2) / np.sum(np.array(u2)**2)
    c3 = (np.array(u3)**2).dot(data2) / np.sum(np.array(u3)**2)
    c4 = (np.array(u4)**2).dot(data2) / np.sum(np.array(u4)**2)
    c5 = (np.array(u5)**2).dot(data2) / np.sum(np.array(u5)**2)

    cc1.append(c1)
    cc2.append(c2)
    cc3.append(c3)
    cc4.append(c4)
    cc5.append(c5) 

    if (j>5):  
        change_rate1 = np.sum(3*cc1[j] - cc1[j-1] - cc1[j-2] - cc1[j-3])/3
        change_rate2 = np.sum(3*cc2[j] - cc2[j-1] - cc2[j-2] - cc2[j-3])/3
        change_rate3 = np.sum(3*cc3[j] - cc3[j-1] - cc3[j-2] - cc3[j-3])/3
        change_rate4 = np.sum(3*cc4[j] - cc4[j-1] - cc4[j-2] - cc4[j-3])/3
        change_rate5 = np.sum(3*cc5[j] - cc5[j-1] - cc5[j-2] - cc5[j-3])/3        
        change_rate = np.array([change_rate1,change_rate2,change_rate3,change_rate4,change_rate5])
        changed = np.sum(change_rate>0.0000001)
        if changed == 0:
            break

print(c1)  # to check a centroid coordinates   c1 - c5 ... they are the last centroids calculated, so supposedly they converged.
print(U)  # this is the degree of pertenence to each centroid (so n row points x K centroids columns).


0 commentaires

2
votes

python

Il y a un package flou-c-signifie dans le PYPI. Découvrez le lien: FUZZY-C-signifie Python

C'est le moyen le plus simple d'utiliser FCM dans Python. J'espère que ça vous aide.


0 commentaires