File: gmeans_examples.py

package info (click to toggle)
python-pyclustering 0.10.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 11,128 kB
  • sloc: cpp: 38,888; python: 24,311; sh: 384; makefile: 105
file content (180 lines) | stat: -rwxr-xr-x 6,839 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""!

@brief Examples of usage and demonstration of abilities of G-Means algorithm in cluster analysis.

@authors Andrei Novikov (pyclustering@yandex.ru)
@date 2014-2020
@copyright BSD-3-Clause

"""


from pyclustering.samples.definitions import SIMPLE_SAMPLES, FCPS_SAMPLES

from pyclustering.cluster import cluster_visualizer
from pyclustering.cluster.gmeans import gmeans

from pyclustering.utils import read_sample


def template_clustering(sample_path, k_init=1, ccore=True, **kwargs):
    sample = read_sample(sample_path)

    gmeans_instance = gmeans(sample, k_init, ccore, repeat=5).process()
    clusters = gmeans_instance.get_clusters()
    centers = gmeans_instance.get_centers()

    visualize = kwargs.get('visualize', True)
    if visualize:
        visualizer = cluster_visualizer()
        visualizer.append_clusters(clusters, sample)
        visualizer.append_cluster(centers, None, marker='*', markersize=10)
        visualizer.show()

    return sample, clusters


def cluster_sample1():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE1)

def cluster_sample2():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE2)

def cluster_sample3():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE3)

def cluster_sample4():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE4)

def cluster_sample5():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE5)

def cluster_sample6():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE6)

def cluster_sample7():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE7)

def cluster_sample8():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE8)

def cluster_sample9():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE9)

def cluster_sample10():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE10)

def cluster_sample11():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE11)

def cluster_sample12():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE12)

def cluster_sample13():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE13)

def cluster_sample14():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE14)

def cluster_elongate():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_ELONGATE)

def cluster_lsun():
    template_clustering(FCPS_SAMPLES.SAMPLE_LSUN)

def cluster_tetra():
    template_clustering(FCPS_SAMPLES.SAMPLE_TETRA)

def cluster_hepta():
    template_clustering(FCPS_SAMPLES.SAMPLE_HEPTA)

def cluster_two_diamonds():
    template_clustering(FCPS_SAMPLES.SAMPLE_TWO_DIAMONDS)

def cluster_chainlink():
    template_clustering(FCPS_SAMPLES.SAMPLE_CHAINLINK)

def cluster_wingnut():
    template_clustering(FCPS_SAMPLES.SAMPLE_WING_NUT)

def cluster_atom():
    template_clustering(FCPS_SAMPLES.SAMPLE_ATOM)

def cluster_engytime():
    template_clustering(FCPS_SAMPLES.SAMPLE_ENGY_TIME)


def display_simple_clustering_results():
    (simple1, simple1_clusters) = template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE1, visualize=False)
    (simple2, simple2_clusters) = template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE2, visualize=False)
    (simple3, simple3_clusters) = template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE3, visualize=False)
    (simple4, simple4_clusters) = template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE4, visualize=False)
    (simple5, simple5_clusters) = template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE5, visualize=False)
    (simple6, simple6_clusters) = template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE6, visualize=False)
    (simple7, simple7_clusters) = template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE7, visualize=False)
    (simple8, simple8_clusters) = template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE8, visualize=False)
    (simple9, simple9_clusters) = template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE9, visualize=False)

    visualizer = cluster_visualizer(9, 3)
    visualizer.append_clusters(simple1_clusters, simple1, 0, markersize=3)
    visualizer.append_clusters(simple2_clusters, simple2, 1, markersize=3)
    visualizer.append_clusters(simple3_clusters, simple3, 2, markersize=3)
    visualizer.append_clusters(simple4_clusters, simple4, 3, markersize=3)
    visualizer.append_clusters(simple5_clusters, simple5, 4, markersize=3)
    visualizer.append_clusters(simple6_clusters, simple6, 5, markersize=6)
    visualizer.append_clusters(simple7_clusters, simple7, 6, markersize=6)
    visualizer.append_clusters(simple8_clusters, simple8, 7, markersize=6)
    visualizer.append_clusters(simple9_clusters, simple9, 8, markersize=6)
    visualizer.show()


def display_fcps_clustering_results():
    (simple3, simple3_clusters) = template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE3, visualize=False)
    (lsun, lsun_clusters) = template_clustering(FCPS_SAMPLES.SAMPLE_LSUN, visualize=False)
    (target, target_clusters) = template_clustering(FCPS_SAMPLES.SAMPLE_TARGET, visualize=False)
    (two_diamonds, two_diamonds_clusters) = template_clustering(FCPS_SAMPLES.SAMPLE_TWO_DIAMONDS, visualize=False)
    (wing_nut, wing_nut_clusters) = template_clustering(FCPS_SAMPLES.SAMPLE_WING_NUT, visualize=False)
    (chainlink, chainlink_clusters) = template_clustering(FCPS_SAMPLES.SAMPLE_CHAINLINK, visualize=False)
    (hepta, hepta_clusters) = template_clustering(FCPS_SAMPLES.SAMPLE_HEPTA, visualize=False)
    (tetra, tetra_clusters) = template_clustering(FCPS_SAMPLES.SAMPLE_TETRA, visualize=False)
    (atom, atom_clusters) = template_clustering(FCPS_SAMPLES.SAMPLE_ATOM, visualize=False)

    visualizer = cluster_visualizer(9, 3)
    visualizer.append_clusters(simple3_clusters, simple3, 0, markersize=3)
    visualizer.append_clusters(lsun_clusters, lsun, 1, markersize=3)
    visualizer.append_clusters(target_clusters, target, 2, markersize=3)
    visualizer.append_clusters(two_diamonds_clusters, two_diamonds, 3, markersize=3)
    visualizer.append_clusters(wing_nut_clusters, wing_nut, 4, markersize=3)
    visualizer.append_clusters(chainlink_clusters, chainlink, 5, markersize=6)
    visualizer.append_clusters(hepta_clusters, hepta, 6, markersize=6)
    visualizer.append_clusters(tetra_clusters, tetra, 7, markersize=6)
    visualizer.append_clusters(atom_clusters, atom, 8, markersize=6)
    visualizer.show()


cluster_sample1()
cluster_sample2()
cluster_sample3()
cluster_sample4()
cluster_sample5()
cluster_sample6()
cluster_sample7()
cluster_sample8()
cluster_sample9()
cluster_sample10()
cluster_sample11()
cluster_sample12()
cluster_sample13()
cluster_sample14()
cluster_elongate()
cluster_lsun()
cluster_tetra()
cluster_hepta()
cluster_two_diamonds()
cluster_chainlink()
cluster_wingnut()
cluster_atom()
cluster_engytime()

# display_simple_clustering_results()
# display_fcps_clustering_results()