示例#1
0
    fcm = FCM(data, x, 100)  #create the fcm model
    fcm.initializeCenters()  #initialize the centers of clusters
    current = 0
    while ((current < fcm.max_iter) and (not fcm.membershipConvergence())
           ):  #while the membership values not converging
        fcm.updateMembershipDegrees()
        fcm.updateCenters()
        current += 1
        """temporaryClusters=fcm.makeClusters()
        for item in temporaryClusters:
            if len(item)==0:
                maxrand=len(data)
                randomIndex=random.randint(10,maxrand)-11
                row = data[randomIndex] 
                item.append(row)"""
        #print(fcm.membershipMatrix[0][0])
        #plotData(temporaryClusters,fcm.data)
        #plotCenters(fcm.centers)

    finalClusters = fcm.makeClusters(
    )  #get clusters based on the membership values in the membership matrix(defuzzification)
    print("after " + str(current) + " iterations:")
    num = 0
    for cluster in finalClusters:
        num += 1
        print("Cluster " + str(num) + " having " + str(len(cluster)) +
              " datapoints")
    plotData(finalClusters, fcm.data)  #visualise each cluster in a color
    #print(fcm.membershipMatrix)
    #plotCenters(fcm.centers)