-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclustering.py
More file actions
92 lines (75 loc) · 2.99 KB
/
clustering.py
File metadata and controls
92 lines (75 loc) · 2.99 KB
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
import numpy as np
# -------------------------------------------------------------
# Berechne die Membership-Matrix U nach Formel (7)
# -------------------------------------------------------------
def membership_matrix(samples, cluster, m=2.0):
samples = np.array(samples, dtype=float).reshape(-1, 3)
cluster = np.array(cluster, dtype=float).reshape(-1, 3)
n = len(samples) # Anzahl Samples
c = len(cluster) # Anzahl Cluster
U = np.zeros((c, n), dtype=float)
for i in range(c):
for j in range(n):
dist_i = np.linalg.norm(samples[j] - cluster[i]) + 1e-8
denom = 0.0
for k in range(c):
dist_k = np.linalg.norm(samples[j] - cluster[k]) + 1e-8
denom += (dist_i / dist_k) ** (2 / (m - 1))
U[i, j] = 1.0 / denom
return U
# -------------------------------------------------------------
# Berechne die neuen Clusterzentren V nach Formel (8)
# -------------------------------------------------------------
def cluster_centers(U, samples, m=2.0):
c, n = U.shape
samples = np.array(samples, dtype=float).reshape(-1, 3)
V = np.zeros((c, 3), dtype=float)
for i in range(c):
top = np.zeros(3, dtype=float)
bot = 0.0
for j in range(n):
w = (U[i, j] ** m)
top += w * samples[j]
bot += w
V[i] = top / (bot + 1e-8)
return V
# -------------------------------------------------------------
# Zielfunktion J(U, V) nach Formel (6) — optional zur Kontrolle
# -------------------------------------------------------------
def objective_function(U, samples, cluster, m=2.0):
samples = np.array(samples, dtype=float)
cluster = np.array(cluster, dtype=float)
c, n = U.shape
J = 0.0
for i in range(c):
for j in range(n):
dist = np.linalg.norm(samples[j] - cluster[i])
J += (U[i, j] ** m) * (dist ** 2)
return J
# -------------------------------------------------------------
# Hauptoptimierungsschleife: Iteriere (7) und (8)
# -------------------------------------------------------------
def optimizing(samples, colors=6, m=2.0, max_iter=20, tol=1e-5):
samples = np.array(samples, dtype=float).reshape(-1, 3)
# Initiale Clusterzentren (RGB + CMY)
cluster = np.array([
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
[1.0, 1.0, 0.0],
[1.0, 0.0, 1.0],
[0.0, 1.0, 1.0]
], dtype=float)
for iteration in range(max_iter):
# 1️⃣ Membership-Matrix nach Formel (7)
U = membership_matrix(samples, cluster, m)
# 2️⃣ Clusterzentren nach Formel (8)
new_cluster = cluster_centers(U, samples, m)
# 3️⃣ Abbruchbedingung
if np.allclose(new_cluster, cluster, atol=tol):
break
cluster = new_cluster
# Optional: Fortschritt anzeigen
# J = objective_function(U, samples, cluster, m)
# print(f"Iteration {iteration}: J = {J:.6f}")
return cluster