While not(x==x)vs.While(x!=x)

2024-07-02 14:45:43 发布

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

我在为一个班做作业,那就是:

给定一组N个测量值,(r1,r2。,rN),我们首先将奇数测量值分配给类1,偶数测量值分配给类2。然后重复以下两个步骤:

•更新步骤:计算每个集群内测量的平均值(平均值)。 •分配步骤:将每个测量值分配给平均值最接近的聚类。你知道吗

如果是平局,将测量值分配给簇1。 重复上述步骤,直到集群分配不变。在聚类分配稳定之前,不能预先确定需要多少步骤。你知道吗

我最初用来解决这个问题但没有成功的代码是:

import numpy as np
def clusterAnalysis(reflectance):

    oldCluster=np.zeros(np.size(reflectance))

    Cluster=(np.arange(0,np.size(reflectance))%2)+1

    while np.all(oldCluster!=Cluster):
        oldCluster=np.copy(Cluster)

        m1=np.mean(reflectance[Cluster==1])
        m2=np.mean(reflectance[Cluster==2])

        for i in range(np.size(reflectance)):
            d1= abs(reflectance[i]-m1)
            d2= abs(reflectance[i]-m2)
            if d1<=d2:
                Cluster[i]=1
            else:
                Cluster[i]=2

    return Cluster

这不管用。然而,当我替换

while np.all(oldCluster!=Cluster):

使用:

while not np.all(oldCluster==Cluster):

它确实有用!你知道吗

有人能解释这是为什么吗?你知道吗


Tags: sizenp步骤集群聚类allmean平均值