TypeError: 'float' object is not iterable with the evaluation of the clustering quality

Question:

I have an array that looks:

matrix =np.array([[0, 0.0784, 0.032768, 0.097216, 0.131008, 0.025792],
       [0.0784  , 0, 0.142144, 0.16768 , 0.223104, 0.174848],
       [0.032768, 0.142144, 0, 0.069312, 0.126656, 0.053056],
       [0.097216, 0.16768 , 0.069312, 0, 0.212224, 0.095232],
       [0.131008, 0.223104, 0.126656, 0.212224, 0, 0.173312],
       [0.025792, 0.174848, 0.053056, 0.095232, 0.173312, 0]])

I use Markov Clustering to cluster this matrix

    import markov_clustering as mc
    import networkx as nx
    import random
    import pandas as pd
    import numpy as np
    
    # number of nodes to use
    numnodes = 6
    
    # generate random positions as a dictionary where the key is the node id and the value
    # is a tuple containing 2D coordinates
    positions = {i:(random.random() * 2 - 1, random.random() * 2 - 1) for i in range(numnodes)}
    
# use networkx to generate the graph
network = nx.random_geometric_graph(numnodes, 0.3, pos=positions)


result = mc.run_mcl(matrix)           # run MCL with default parameters

clusters = mc.get_clusters(result)    # get clusters

output:
[(0,), (1,), (2,), (3, 4), (5,)]

I determine the modularity for a range of cluster inflation values, allowing us to pick the best cluster inflation value for the given graph by using:

for inflation in [i / 10 for i in range(15, 26)]:
    result = mc.run_mcl(matrix, inflation=inflation)
    clusters = mc.get_clusters(result)
    Q = mc.modularity(matrix=result, clusters=clusters)
    print("inflation:", inflation, "modularity:", Q)

but I got an error says: ‘float’ object is not iterable. How can I fix it?

Asked By: alxander21

||

Answers:

You missed the matrix = nx.to_scipy_sparse_array(network)

import markov_clustering as mc
import networkx as nx
import random

matrix =np.array([[0, 0.0784, 0.032768, 0.097216, 0.131008, 0.025792],
                  [0.0784  , 0, 0.142144, 0.16768 , 0.223104, 0.174848],
                  [0.032768, 0.142144, 0, 0.069312, 0.126656, 0.053056],
                  [0.097216, 0.16768 , 0.069312, 0, 0.212224, 0.095232],
                  [0.131008, 0.223104, 0.126656, 0.212224, 0, 0.173312],
                  [0.025792, 0.174848, 0.053056, 0.095232, 0.173312, 0]])

# number of nodes to use
numnodes = 200

# generate random positions as a dictionary where the key is the node id and the value
# is a tuple containing 2D coordinates
positions = {i:(random.random() * 2 - 1, random.random() * 2 - 1) for i in range(numnodes)}

# use networkx to generate the graph
network = nx.random_geometric_graph(numnodes, 0.3, pos=positions)

matrix = nx.to_scipy_sparse_array(network)


for inflation in [i / 10 for i in range(15, 26)]:
    result = mc.run_mcl(matrix, inflation=inflation)
    clusters = mc.get_clusters(result)
    Q = mc.modularity(matrix=result, clusters=clusters)
    print("inflation:", inflation, "modularity:", Q)
Answered By: georgwalker45

You have to convert your matrix as spare matrix:

from scipy.sparse import csr_matrix

matrix = csr_matrix(matrix)
for inflation in [i / 10 for i in range(15, 26)]:
    result = mc.run_mcl(matrix, inflation=inflation)
    clusters = mc.get_clusters(result)
    Q = mc.modularity(matrix=result, clusters=clusters)
    print("inflation:", inflation, "modularity:", Q)

Output:

inflation: 1.5 modularity: 0.0
inflation: 1.6 modularity: 0.11111111111111109
inflation: 1.7 modularity: 0.16666666666666666
inflation: 1.8 modularity: 0.22222222222222224
inflation: 1.9 modularity: 0.22222222222222224
inflation: 2.0 modularity: 0.11111111111111112
inflation: 2.1 modularity: 0.0
inflation: 2.2 modularity: 0.0
inflation: 2.3 modularity: 0.0
inflation: 2.4 modularity: 0.0
inflation: 2.5 modularity: 0.0
Answered By: Corralien
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.