-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod_7_hierarchical.py
More file actions
57 lines (43 loc) · 1.64 KB
/
Copy pathmod_7_hierarchical.py
File metadata and controls
57 lines (43 loc) · 1.64 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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import linkage, fcluster
from mpl_toolkits.mplot3d import Axes3D
CLUSTER_COLORS = {
1: "#FF7700",
2: "#AA00FF",
3: "#00C8C8",
4: "#A4DE02",
5: "#FF1493"
}
def plot_3d_clusters(data, labels):
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
unique_labels = np.unique(labels)
for label in unique_labels:
cluster_points = data[labels == label]
color = CLUSTER_COLORS.get(label, "#000000")
cluster_size = len(cluster_points)
label_name = f"Cluster {label} ({cluster_size} pkt)"
ax.scatter(cluster_points[:, 0], cluster_points[:, 1], cluster_points[:, 2],
label=label_name, color=color, alpha=0.8, edgecolors='k')
ax.set_xlabel("Mediana Hue (Hmed)")
ax.set_ylabel("Mediana Saturation (Smed)")
ax.set_zlabel("Mediana Value (Vmed)")
ax.legend(loc="upper left")
ax.set_title("Klastrowanie metodą Warda (5 klastrów)")
plt.tight_layout()
plt.show()
filename = "HSVonly_vectors.csv"
print(f"\n🔍 Przetwarzanie pliku: {filename}")
df = pd.read_csv(filename)
image_names = df.iloc[:, 0]
data = df.iloc[:, 1:].values
linkage_matrix = linkage(data, method='ward')
n_clusters = 5
labels = fcluster(linkage_matrix, n_clusters, criterion='maxclust')
df["Cluster"] = labels
plot_3d_clusters(data, labels)
output_filename = f"ward_{n_clusters}_{filename}"
df.to_csv(output_filename, index=False)
print(f"✅ Plik z klastrami zapisany jako '{output_filename}'.")