Beispiel #1
0
def fitnessFunction(individual, algorithm, n_labels, ground_truth, m1, m2, m3):

    w1 = individual[0]
    w2 = individual[1]
    # w3 = individual[2]
    w3 = 0
    corr = mf.calculateCorrelationMatrix(m1, m2, m3, w1, w2, w3)

    if algorithm == 'complete':
        agglomerative = AgglomerativeClustering(affinity='precomputed', n_clusters=n_labels, linkage='complete').fit(corr)
        labels = agglomerative.labels_
    elif algorithm == 'average':
        agglomerative = AgglomerativeClustering(affinity='precomputed', n_clusters=n_labels, linkage='average').fit(corr)
        labels = agglomerative.labels_
    elif algorithm == 'kmedoids':
        _, clusters = km.kMedoids(corr, n_labels, 100)
        labels = km.sortLabels(clusters)
    
    #fitness = metrics.homogeneity_score(labels, ground_truth)
    #fitness = metrics.adjusted_rand_score(labels, ground_truth)
    #fitness = sum(individual)
    fitness = metrics.calinski_harabaz_score(corr, labels)

    corr = None
    m1 = None
    m2 = None
    m3 = None

    return fitness
Beispiel #2
0
def fitness(indv):
    w1, w2, w3 = indv.solution
    corr = mf.calculateCorrelationMatrix(matrix1, matrix2, matrix3, w1, w2, w3)
    if algorithm == 'complete':      
        agglomerative = AgglomerativeClustering(affinity='precomputed', n_clusters=n_labels, linkage='complete').fit(corr)
        labels = agglomerative.labels_
    elif algorithm == 'average':
        agglomerative = AgglomerativeClustering(affinity='precomputed', n_clusters=n_labels, linkage='average').fit(corr)
        labels = agglomerative.labels_
    elif algorithm == 'kmedoids':
        _, clusters = km.kMedoids(corr, n_labels, 100)
        labels = km.sortLabels(clusters)

    metrics = ce.clusterEvaluation(corr, labels, ground_truth)

	writer.write(str(current_iteration)+': '+str(w1)+' '+str(w2)+' '+str(w3)+' '.join(str(x) for x in metrics)+'\n')
Beispiel #3
0
def fitness(indv):
    w1, w2, w3 = indv.solution
    #return x + 10*sin(5*x) + 7*cos(4*x)
    corr = mf.calculateCorrelationMatrix(matrix1, matrix2, matrix3, w1, w2, w3)
    agglomerative = AgglomerativeClustering(affinity='precomputed',
                                            n_clusters=n_labels,
                                            linkage='complete').fit(corr)
    labels = agglomerative.labels_

    metrics = cl.clusterEvaluation(corr, labels, ground_truth)

    if metrics[0] <= 0:
        return 1

    print(metrics[0] * 100)
    return float(metrics[0]) * 100
Beispiel #4
0
def fitness(indv):
    w1, w2, w3 = indv.solution
    corr = mf.calculateCorrelationMatrix(matrix1, matrix2, matrix3, w1, w2, w3)
    if algorithm == 'complete':
        agglomerative = AgglomerativeClustering(affinity='precomputed',
                                                n_clusters=n_labels,
                                                linkage='complete').fit(corr)
        labels = agglomerative.labels_
    elif algorithm == 'average':
        agglomerative = AgglomerativeClustering(affinity='precomputed',
                                                n_clusters=n_labels,
                                                linkage='average').fit(corr)
        labels = agglomerative.labels_
    elif algorithm == 'kmedoids':
        _, clusters = km.kMedoids(corr, n_labels, 100)
        labels = km.sortLabels(clusters)

    metrics = ce.clusterEvaluation(corr, labels, ground_truth)

    return float(metrics[0]) * 100
Beispiel #5
0
        ground_truth = list(map(int, ground_truth))

        matrix1 = mf.minMaxScale(matrix1)
        matrix2 = mf.minMaxScale(matrix2)
        matrix3 = mf.minMaxScale(matrix3)

        matrix1 = mf.calculateDistances(matrix1)
        matrix2 = mf.calculateDistances(matrix2)
        matrix3 = mf.calculateDistances(matrix3)

        for w1 in np.arange(0.05, 1.05, 0.05):

            w2 = 0
            w3 = 1 - w1

            corr = mf.calculateCorrelationMatrix(matrix1, matrix2, matrix3, w1,
                                                 w2, w3)

            # Hierarchical
            for link in ['complete', 'average']:
                agglomerative = AgglomerativeClustering(
                    affinity='precomputed',
                    n_clusters=n_labels,
                    linkage='complete').fit(corr)
                labels = agglomerative.labels_
                metrics = ce.clusterEvaluation(corr, labels, ground_truth)
                print(metrics)
                ce.saveResultsWithWeights(measure1, measure2, w1,
                                          'hierarchical_' + link, sample,
                                          metrics)

            # K-Medoids