def ClusterPopulation(max_gen, population, data):
    """

    :param max_gen:
    :param population:
    :param data:
    :return:
    """

    pop = np.array(population)
    plab1 = []
    pcenter = []
    cluster1 = int(input("Enter the cluster for new population\n"))

    for i in range(0, max_gen):
        if (i % cluster1 == 1):  # Checking the condition of Kmeans Clustering
            K1.insert(i, cluster1)
            u, label, t, l = Kmeans_clu(
                cluster1,
                population)  # Storing the values Center(u) anb label(u)
            plab1.insert(i, label)
            pcenter.insert(i, u)
            plab = np.array(plab1)
            plab = plab[0]
            pcenter = np.array(pcenter)
            pcenter = pcenter[0]
            print('Value of label after Clustering of population\n', plab)
            One_LengthCluster(
                cluster1, plab, pcenter, pop,
                max_gen)  #To check if any label has one point associated
        else:
            print("Not need of clustering for this generation of\n", i)
Exemplo n.º 2
0
def ClusterPopulation(max_gen, population, data):
    # print("Value of population",population)
    pop = np.array(population)
    # print("Data type of population",pop.dtype)
    # print("Population is in array form\n",pop)
    plab = []
    pcenter = []
    cluster1 = int(input("Enter the cluster for new population\n"))

    for i in range(max_gen):
        if (i % cluster1 == 1):
            print("CLustering will occur", i)
            K1.insert(i, cluster1)
            print('value of K1', K1)
            u, label, t, l = Kmeans_clu(cluster1, population)
            A1.insert(i, t)
            plab.insert(i, label)
            pcenter.insert(i, u)
            # print('Value of newcenter\n',pcenter)
            print('Value of new label after Clustering of population\n', plab)
            LC = Counter(l.labels_)
            print("VAlue of LAbel and Number Cluster Associated with them\n",
                  LC)
            LC1 = [t for (t, v) in LC.items() if v == 1]
            t1 = np.array(LC1)
            if (t1.size):
                One_LengthCluster(t1, cluster1, plab, pcenter, l, LC, pop)
            else:
                print("no lenght 1 cluster\n")
                return (plab, pcenter)
        else:
            print("Not need of clustering\n", i)
Exemplo n.º 3
0
def Repair(pop, s, i, x):  # Checking if it is going beyond the boundary
    Newlab = []  ##Store the labels in lab
    Newcenter = []
    new_center = []
    new_pop = []
    pbm_index_newpop = []
    sil_score_newpop = []
    C = []

    pop = np.array(pop)
    A = pop.max(axis=0)  #Getting the maximum from the column
    B = pop.min(axis=0)  #Getting the minimum from the column
    for j in range(0, len(s)):
        if (s[j] < A[i]):
            s[j] = A[i]
        elif (s[j] > B[i]):
            s[j] = B[i]
        else:
            s[j] = s[j]

    print("Value of offspring for the {} is {} for axis{}\n".format(x, s, i))
    center1 = s[0:4]
    #print("Value of center 1 is\n", center1)
    center2 = s[4:8]
    #print("Value of center 2 is\n", center2)
    center3 = s[8:12]
    #print("Value of center 3 is\n", center3)
    C.append(center1)
    C.append(center2)
    C.append(center3)
    C = np.array(C)
    print("Value of Initial Center\n", C)
    data = genfromtxt('iris.csv',
                      delimiter=',',
                      skip_header=0,
                      usecols=range(0, 4))  ##Read the input data
    actual_label = genfromtxt('iris.csv',
                              delimiter=',',
                              dtype=str,
                              skip_header=0,
                              usecols=(4))
    u, lab = Kmeans_clu(3, data, C)
    Newlab.insert(i, lab)  #Store the labels in lab
    Newcenter.insert(i, u)
    features = len(data[0])
    max_cluster = 3
    cluster = 3
    new_pbm = cal_pbm_index(cluster, data, u, lab)
    pbm_index_newpop.insert(
        i, new_pbm)  #Store the second objective function(PBM Ind1ex)
    s = silhouette_score(data, lab)
    sil_score_newpop.insert(
        i, s)  #Store the first objective funvtion(Silhouette Score)
    new_center = pop_create(max_cluster, features, cluster, u)
    new_pop.insert(i, new_center)
Exemplo n.º 4
0
pbm_index = []
tau_init = 0.8
CN = {}
RN = {}
for mm in range(
        0, max_gen
):  ## Initialize dictionary of size upto length maximum generation
    CN[mm] = {}
    RN[mm] = {}

for i in range(0, chromosome):
    cluster = random.randint(
        2, max_cluster
    )  ##Randomly selects cluster number from 2 to root(poplation)
    K.insert(i, cluster)  ##Store the number of clusters in clu
    u, label = Kmeans_clu(cluster, data)
    lab.insert(i, label)  ##Store the labels in lab
    center.insert(i, u)
    new_pbm = cal_pbm_index(cluster, data, u, label)
    pbm_index.insert(i,
                     new_pbm)  ##Store the second objective function(PBM Index)
    dn = calculate_dunn_index(data, label, cluster)
    ini_dunn.insert(i, dn)
    s = silhouette_score(data, label)
    sil_score.insert(i,
                     s)  ##Store the first objective funvtion(Silhouette Score)
    new_center = pop_create(max_cluster, features, cluster, u)
    population.insert(i, new_center)  ##Store the populations in population

file.write("Initial Clusters : " + str(K) + '\n')
file.write("Initial PBM : " + str(pbm_index) + '\n')
Exemplo n.º 5
0
population = []
sil_score = []
pbm_index = []
K = []
pcenter = []
plab = []
K1 = []
z2 = {}
A1 = []
{}

for i in range(0, chromosome):
    cluster = 3  #random.randint(2, max_cluster)  ##Randomly selects cluster number from 2 to root(poplation)
    K.insert(i, cluster)  ##Store the number of clusters in clu
    #print('value of K is ',K)
    u, label, z1, A1 = Kmeans_clu(cluster, data)
    #print("centers and labels : ", u, label)
    lab.insert(i, label)  ##Store the labels in lab
    center.insert(i, u)
    #print('Value of center in main \n',center)
    new_pbm = cal_pbm_index(cluster, data, u, label)
    pbm_index.insert(
        i, new_pbm)  ##Store the second objective function(PBM Ind1ex)
    s = silhouette_score(data, label)
    sil_score.insert(i,
                     s)  ##Store the first objective funvtion(Silhouette Score)
    new_center = pop_create(max_cluster, features, cluster, u)
    population.insert(i, new_center)
    #print("VAlue of population in main\n" ,population)

ClusterPopulation(max_gen, population,