File: silhouette_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 (100 lines) | stat: -rwxr-xr-x 3,482 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
"""!

@brief Examples of usage and demonstration of abilities of Silhouette algorithm.

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

"""

from pyclustering.cluster import cluster_visualizer
from pyclustering.cluster.center_initializer import kmeans_plusplus_initializer
from pyclustering.cluster.kmeans import kmeans
from pyclustering.cluster.silhouette import silhouette_ksearch_type, silhouette_ksearch

from pyclustering.samples.definitions import SIMPLE_SAMPLES, FCPS_SAMPLES

from pyclustering.utils import read_sample


def find_optimal_amout_clusters(sample_path, kmin, kmax, algorithm):
    sample = read_sample(sample_path)
    search_instance = silhouette_ksearch(sample, kmin, kmax, algorithm=algorithm).process()

    amount = search_instance.get_amount()
    scores = search_instance.get_scores()

    print("Sample: '%s', Scores: '%s'" % (sample_path, str(scores)))

    initial_centers = kmeans_plusplus_initializer(sample, amount).initialize()
    kmeans_instance = kmeans(sample, initial_centers).process()

    clusters = kmeans_instance.get_clusters()

    visualizer = cluster_visualizer()
    visualizer.append_clusters(clusters, sample)
    visualizer.show()


def sample_simple01():
    find_optimal_amout_clusters(SIMPLE_SAMPLES.SAMPLE_SIMPLE1, 2, 10, silhouette_ksearch_type.KMEANS)

def sample_simple02():
    find_optimal_amout_clusters(SIMPLE_SAMPLES.SAMPLE_SIMPLE2, 2, 10, silhouette_ksearch_type.KMEANS)

def sample_simple03():
    find_optimal_amout_clusters(SIMPLE_SAMPLES.SAMPLE_SIMPLE3, 2, 10, silhouette_ksearch_type.KMEANS)

def sample_simple04():
    find_optimal_amout_clusters(SIMPLE_SAMPLES.SAMPLE_SIMPLE4, 2, 10, silhouette_ksearch_type.KMEANS)

def sample_simple05():
    find_optimal_amout_clusters(SIMPLE_SAMPLES.SAMPLE_SIMPLE5, 2, 10, silhouette_ksearch_type.KMEANS)

def sample_simple06():
    find_optimal_amout_clusters(SIMPLE_SAMPLES.SAMPLE_SIMPLE6, 2, 10, silhouette_ksearch_type.KMEANS)

def sample_simple07():
    find_optimal_amout_clusters(SIMPLE_SAMPLES.SAMPLE_SIMPLE7, 2, 10, silhouette_ksearch_type.KMEANS)

def sample_simple08():
    find_optimal_amout_clusters(SIMPLE_SAMPLES.SAMPLE_SIMPLE8, 2, 10, silhouette_ksearch_type.KMEANS)

def sample_simple09():
    find_optimal_amout_clusters(SIMPLE_SAMPLES.SAMPLE_SIMPLE9, 2, 10, silhouette_ksearch_type.KMEANS)

def sample_simple10():
    find_optimal_amout_clusters(SIMPLE_SAMPLES.SAMPLE_SIMPLE10, 2, 10, silhouette_ksearch_type.KMEANS)

def sample_simple11():
    find_optimal_amout_clusters(SIMPLE_SAMPLES.SAMPLE_SIMPLE11, 2, 10, silhouette_ksearch_type.KMEANS)

def sample_simple12():
    find_optimal_amout_clusters(SIMPLE_SAMPLES.SAMPLE_SIMPLE12, 2, 10, silhouette_ksearch_type.KMEANS)

def sample_simple13():
    find_optimal_amout_clusters(SIMPLE_SAMPLES.SAMPLE_SIMPLE13, 2, 10, silhouette_ksearch_type.KMEANS)

def sample_simple14():
    find_optimal_amout_clusters(SIMPLE_SAMPLES.SAMPLE_SIMPLE14, 2, 10, silhouette_ksearch_type.KMEANS)

def sample_hepta():
    find_optimal_amout_clusters(FCPS_SAMPLES.SAMPLE_HEPTA, 2, 10, silhouette_ksearch_type.KMEANS)


sample_simple01()
sample_simple02()
sample_simple03()
sample_simple04()
sample_simple05()
sample_simple06()
sample_simple07()
sample_simple08()
sample_simple09()
sample_simple10()
sample_simple11()
sample_simple12()
sample_simple13()
sample_simple14()
sample_hepta()