Пример #1
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)
Пример #2
0
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')
file.write("Initial Sil score : " + str(sil_score) + '\n')
file.write("Initial Dunn : " + str(ini_dunn) + '\n')
index = ini_dunn.index(max(ini_dunn))