Python是否有模糊C-Means算法?

2024-05-29 07:50:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我在三维空间里有一些点,想把它们聚在一起。我知道Pythons模块“cluster”,但它只有K-Means。你知道有FCM(模糊C-均值)的模块吗?

(如果您知道其他一些与集群相关的python模块,您可以将它们命名为额外的功能。但重要的问题是python中FCM算法的问题。)

Matlab

在Matlab(example)中使用FCM似乎很容易。Python不提供类似的功能吗?

牛膝、腰肢和鼠尾草

我没有在NumPySciPySage中找到FCM。我已经下载了文档并进行了搜索。没有结果

Python集群

似乎集群模块将在下一个版本中添加模糊C-Means(参见Roadmap)。但我现在需要它


Tags: 模块功能算法example集群命名means均值
3条回答

看看scikit-fuzzy包。它具有非常基本的模糊逻辑功能,包括模糊c均值聚类。

我从头开始,使用K++初始化(使用固定种子和5个质心)。将其添加到所需的质心数应该不会太困难):

# 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).

我知道它不是很Python,但我希望它可以作为一个起点,为您的完整的模糊C均值算法。我认为,当数据不易分离时(例如,当“t-SNE可视化”将所有数据显示在一起,而不是显示清楚分离的组时,“软聚类”是一种方法。在这种情况下,强制数据严格地只属于一个集群可能是危险的)。我将尝试使用m=1.1,m=2.0,这样您就可以看到模糊参数对pertenence矩阵的影响。

PEACH将提供一些模糊C-Means功能: http://code.google.com/p/peach/

但是,由于wiki是空的,因此似乎没有任何可用的文档。一个example for using FCM with PEACH可以在它的网站上找到。

相关问题 更多 >

    热门问题