MST Clustering using Python

时间:2015-06-30 13:27:44

标签: python graph

I want to make clusters of nodes based on edges of a minimum spanning tree. I used scipy to create minimum spanning tree. The following code:

file = open('/home/deep/Desktop/Lalit_jee/spanning.txt', 'w')
import euc_dist
import stan_devia
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import minimum_spanning_tree
matrix = euc_dist.euc_matrix
matrix1 = matrix
new_mat = [[]]
for i in range(len(matrix1)):
    for j in range(len(matrix1)):
        if j <= i:
            matrix1[i][j] = 0
X = csr_matrix(matrix1)
min_span = minimum_spanning_tree(X)
print >> file, min_span

produced the output as follows:

(0, 2)  3.0
(0, 9)  2.0
(1, 8)  3.0
(1, 9)  4.0
(2, 3)  15.0
(3, 7)  2.0
(4, 5)  6.0
(4, 8)  3.0
(6, 7)  36.0

which indicates a graph as shown in the figure:

! spanning tree

I took threshold as 4. So now I must have the following output:

! Clusters

In the case of data, I must have the following:

(0,9)  2.0
(0,2)  3.0
(3,7)  2.0
(8,1)  3.0
(8,4)  3.0
(5,5)  0.0
(6,6)  0.0

Please tell me the most pythonic way to achieve this output. Thanks in advance...

1 个答案:

答案 0 :(得分:0)

If loops are not required you can use something like this:

[[i for i in row if i < threshold else 0]
  for row in min_span.toarray().astype(int)
]

This will give you resulting matrix as an array