Completed
Branch master (5cc02c)
by Andrei
01:30
created

pyclustering.utils.examples.cluster_distances()   F

Complexity

Conditions 9

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 9
dl 0
loc 40
rs 3
1
"""!
2
3
@brief Examples of usage utils.
4
5
@authors Andrei Novikov ([email protected])
6
@date 2014-2016
7
@copyright GNU Public License
8
9
@cond GNU_PUBLIC_LICENSE
10
    PyClustering is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
    
15
    PyClustering is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
    GNU General Public License for more details.
19
    
20
    You should have received a copy of the GNU General Public License
21
    along with this program. If not, see <http://www.gnu.org/licenses/>.
22
@endcond
23
24
"""
25
26
import pyclustering.utils as utils;
27
28
from pyclustering.cluster.agglomerative import agglomerative;
29
from pyclustering.samples.definitions import SIMPLE_SAMPLES;
30
31
import matplotlib.pyplot as plt;
32
33
34
def cluster_distances(path_sample, amount_clusters):
35
    distances = ['euclidian', 'manhattan', 'avr-inter', 'avr-intra', 'variance'];
36
    
37
    sample = utils.read_sample(path_sample);
38
    
39
    agglomerative_instance = agglomerative(sample, amount_clusters);
40
    agglomerative_instance.process();
41
    
42
    obtained_clusters = agglomerative_instance.get_clusters();
43
    
44
    print("Measurements for:", path_sample);
45
    
46
    for index_cluster in range(len(obtained_clusters)):
47
        for index_neighbor in range(index_cluster + 1, len(obtained_clusters), 1):
48
            cluster1 = obtained_clusters[index_cluster];
49
            cluster2 = obtained_clusters[index_neighbor];
50
            
51
            center_cluster1 = utils.centroid(sample, cluster1);
52
            center_cluster2 = utils.centroid(sample, cluster2);
53
            
54
            for index_distance_type in range(len(distances)):
55
                distance = None;
56
                distance_type = distances[index_distance_type];
57
        
58
                if (distance_type == 'euclidian'):
59
                    distance = utils.euclidean_distance(center_cluster1, center_cluster2);
60
                    
61
                elif (distance_type == 'manhattan'):
62
                    distance = utils.manhattan_distance(center_cluster1, center_cluster2);
63
                    
64
                elif (distance_type == 'avr-inter'):
65
                    distance = utils.average_inter_cluster_distance(cluster1, cluster2, sample);
66
                
67
                elif (distance_type == 'avr-intra'):
68
                    distance = utils.average_intra_cluster_distance(cluster1, cluster2, sample);
69
                
70
                elif (distance_type == 'variance'):
71
                    distance = utils.variance_increase_distance(cluster1, cluster2, sample);
72
            
73
            print("\tDistance", distance_type, "from", index_cluster, "to", index_neighbor, "is:", distance);
74
        
75
76
def display_two_dimensional_cluster_distances(path_sample, amount_clusters):
77
    distances = ['euclidian', 'manhattan', 'avr-inter', 'avr-intra', 'variance'];
78
    
79
    ajacency = [ [0] * amount_clusters for i in range(amount_clusters) ];
80
    
81
    sample = utils.read_sample(path_sample);
82
    
83
    agglomerative_instance = agglomerative(sample, amount_clusters);
84
    agglomerative_instance.process();
85
    
86
    obtained_clusters = agglomerative_instance.get_clusters();
87
    stage = utils.draw_clusters(sample, obtained_clusters, display_result = False);
88
    
89
    for index_cluster in range(len(ajacency)):
90
        for index_neighbor_cluster in range(index_cluster + 1, len(ajacency)):
91
            if ( (index_cluster == index_neighbor_cluster) or (ajacency[index_cluster][index_neighbor_cluster] is True) ):
92
                continue;
93
            
94
            ajacency[index_cluster][index_neighbor_cluster] = True;
95
            ajacency[index_neighbor_cluster][index_cluster] = True;
96
            
97
            cluster1 = obtained_clusters[index_cluster];
98
            cluster2 = obtained_clusters[index_neighbor_cluster];
99
            
100
            center_cluster1 = utils.centroid(sample, cluster1);
101
            center_cluster2 = utils.centroid(sample, cluster2);
102
            
103
            x_maximum, x_minimum, y_maximum, y_minimum = None, None, None, None;
104
            x_index_maximum, y_index_maximum = 1, 1;
105
            
106
            if (center_cluster2[0] > center_cluster1[0]):
107
                x_maximum = center_cluster2[0];
108
                x_minimum = center_cluster1[0];
109
                x_index_maximum = 1;
110
            else:
111
                x_maximum = center_cluster1[0];
112
                x_minimum = center_cluster2[0];
113
                x_index_maximum = -1;
114
            
115
            if (center_cluster2[1] > center_cluster1[1]):
116
                y_maximum = center_cluster2[1];
117
                y_minimum = center_cluster1[1];
118
                y_index_maximum = 1;
119
            else:
120
                y_maximum = center_cluster1[1];
121
                y_minimum = center_cluster2[1];
122
                y_index_maximum = -1;
123
            
124
            print("Cluster 1:", cluster1, ", center:", center_cluster1);
125
            print("Cluster 2:", cluster2, ", center:", center_cluster2);
126
            
127
            stage.annotate(s = '', xy = (center_cluster1[0], center_cluster1[1]), xytext = (center_cluster2[0], center_cluster2[1]), arrowprops = dict(arrowstyle = '<->'));
128
            
129
            for index_distance_type in range(len(distances)):
130
                distance = None;
131
                distance_type = distances[index_distance_type];
132
                
133
                if (distance_type == 'euclidian'):
134
                    distance = utils.euclidean_distance(center_cluster1, center_cluster2);
135
                    
136
                elif (distance_type == 'manhattan'):
137
                    distance = utils.manhattan_distance(center_cluster1, center_cluster2);
138
                    
139
                elif (distance_type == 'avr-inter'):
140
                    distance = utils.average_inter_cluster_distance(cluster1, cluster2, sample);
141
                
142
                elif (distance_type == 'avr-intra'):
143
                    distance = utils.average_intra_cluster_distance(cluster1, cluster2, sample);
144
                
145
                elif (distance_type == 'variance'):
146
                    distance = utils.variance_increase_distance(cluster1, cluster2, sample);
147
                
148
                print("\tCluster distance -", distance_type, ":", distance);
149
                
150
                x_multiplier = index_distance_type + 3;
151
                if (x_index_maximum < 0):
152
                    x_multiplier = len(distances) - index_distance_type + 3;
153
                
154
                y_multiplier = index_distance_type + 3;
155
                if (y_index_maximum < 0):
156
                    y_multiplier = len(distances) - index_distance_type + 3;
157
                
158
                x_text = x_multiplier * (x_maximum - x_minimum) / (len(distances) + 6) + x_minimum;
159
                y_text = y_multiplier * (y_maximum - y_minimum) / (len(distances) + 6) + y_minimum;
160
                
161
                #print(x_text, y_text, "\n");
162
                stage.text(x_text, y_text, distance_type + " {:.3f}".format(distance), fontsize = 9, color='blue');
163
    
164
    plt.show();
165
166
167
def display_cluster_distances_simple_sample_01():
168
    display_two_dimensional_cluster_distances(SIMPLE_SAMPLES.SAMPLE_SIMPLE1, 2);
169
170
def display_cluster_distances_simple_sample_02():
171
    display_two_dimensional_cluster_distances(SIMPLE_SAMPLES.SAMPLE_SIMPLE2, 3);
172
173
def display_cluster_distances_simple_sample_03():
174
    display_two_dimensional_cluster_distances(SIMPLE_SAMPLES.SAMPLE_SIMPLE3, 4);
175
176
177
def print_cluster_distances_simple_sample_07():
178
    cluster_distances(SIMPLE_SAMPLES.SAMPLE_SIMPLE7, 2);
179
180
def print_cluster_distances_simple_sample_08():
181
    cluster_distances(SIMPLE_SAMPLES.SAMPLE_SIMPLE8, 4);
182
183
184
display_cluster_distances_simple_sample_01();
185
display_cluster_distances_simple_sample_02();
186
display_cluster_distances_simple_sample_03();
187
188
print_cluster_distances_simple_sample_07();
189
print_cluster_distances_simple_sample_08();
190
191