-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathK Measns.py
More file actions
31 lines (23 loc) · 944 Bytes
/
K Measns.py
File metadata and controls
31 lines (23 loc) · 944 Bytes
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
import numpy as np
import sklearn
from sklearn.preprocessing import scale
from sklearn.datasets import load_digits
from sklearn.cluster import KMeans
from sklearn import metrics
digits = load_digits()
data = scale(digits)
y = data.targets
# k = len(np.unique(y)) - if dynamic clusters needed
k = 10
samples, features = data.shape
def bench_k_means(estimator, name, data):
estimator.fit(data)
print('%-9s\t%i\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f'
% (name, estimator.inertia_,
metrics.homogeneity_score(y, estimator.labels_),
metrics.completeness_score(y, estimator.labels_),
metrics.v_measure_score(y, estimator.labels_),
metrics.adjusted_rand_score(y, estimator.labels_),
metrics.adjusted_mutual_info_score(y, estimator.labels_),
metrics.silhouette_score(y, estimator.labels_, metric='euclidean'))
)