Files
miner/frontend/clusters.py
Bastien OLLIER 9da6e2d594 Add cluster stats (#13)
Co-authored-by: bastien ollier <bastien.ollier@etu.uca.fr>
Reviewed-on: https://codefirst.iut.uca.fr/git/clement.freville2/miner/pulls/13
Reviewed-by: Hugo PRADIER <hugo.pradier2@etu.uca.fr>
Reviewed-by: Clément FRÉVILLE <clement.freville2@etu.uca.fr>
Co-authored-by: Bastien OLLIER <bastien.ollier@noreply.codefirst.iut.uca.fr>
Co-committed-by: Bastien OLLIER <bastien.ollier@noreply.codefirst.iut.uca.fr>
2024-06-25 08:37:38 +02:00

64 lines
1.9 KiB
Python

from sklearn.cluster import DBSCAN, KMeans
import numpy as np
class DBSCAN_cluster():
def __init__(self, eps, min_samples,data):
self.eps = eps
self.min_samples = min_samples
self.data = data
self.labels = np.array([])
def run(self):
dbscan = DBSCAN(eps=self.eps, min_samples=self.min_samples)
self.labels = dbscan.fit_predict(self.data)
return self.labels
def get_stats(self):
unique_labels = np.unique(self.labels)
stats = []
for label in unique_labels:
if label == -1:
continue
cluster_points = self.data[self.labels == label]
num_points = len(cluster_points)
density = num_points / (np.max(cluster_points, axis=0) - np.min(cluster_points, axis=0)).prod()
stats.append({
"cluster": label,
"num_points": num_points,
"density": density
})
return stats
class KMeans_cluster():
def __init__(self, n_clusters, n_init, max_iter, data):
self.n_clusters = n_clusters
self.n_init = n_init
self.max_iter = max_iter
self.data = data
self.labels = np.array([])
self.centers = []
def run(self):
kmeans = KMeans(n_clusters=self.n_clusters, init="random", n_init=self.n_init, max_iter=self.max_iter, random_state=111)
self.labels = kmeans.fit_predict(self.data)
self.centers = kmeans.cluster_centers_
return self.labels
def get_stats(self):
unique_labels = np.unique(self.labels)
stats = []
for label in unique_labels:
cluster_points = self.data[self.labels == label]
num_points = len(cluster_points)
center = self.centers[label]
stats.append({
'cluster': label,
'num_points': num_points,
'center': center
})
return stats