dockonsurf / modules / clustering.py @ 86112fec
Historique | Voir | Annoter | Télécharger (962 octet)
1 |
#!/usr/bin/env python
|
---|---|
2 |
# -*- coding: utf-8 -*-
|
3 |
|
4 |
import sys |
5 |
import numpy as np |
6 |
import sklearn |
7 |
from sklearn import cluster |
8 |
from sklearn.cluster import AffinityPropagation |
9 |
|
10 |
matrice = sys.argv[1]
|
11 |
|
12 |
X = np.loadtxt(matrice) # lecture de la matrice à partir d'un fichier
|
13 |
|
14 |
if len(X.shape)==0: |
15 |
print 'Number of clusters: 1' |
16 |
print 'Cluster centers:' |
17 |
print '0' |
18 |
print 'Cluster groups:' |
19 |
print '0' |
20 |
|
21 |
else:
|
22 |
af = AffinityPropagation(affinity='precomputed').fit(X)
|
23 |
|
24 |
cluster_centers_indices = af.cluster_centers_indices_ |
25 |
labels = af.labels_ |
26 |
n_clusters_ = len(cluster_centers_indices)
|
27 |
|
28 |
|
29 |
print('Number of clusters: %d' % n_clusters_)
|
30 |
print 'Cluster centers:' |
31 |
print cluster_centers_indices
|
32 |
|
33 |
def ClusterIndicesNumpy(clustNum, labels_array): #numpy |
34 |
return np.where(labels_array == clustNum)[0] |
35 |
|
36 |
print 'Cluster groups:' |
37 |
|
38 |
for i in range(0,n_clusters_) : |
39 |
a = ClusterIndicesNumpy(i, labels) |
40 |
print a
|
41 |
|
42 |
|