pandas TypeError:“float”对象不能与群集质量的评估一起迭代

nszi6y05  于 2023-02-28  发布在  其他
关注(0)|答案(2)|浏览(133)

我有一个数组看起来像:

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]])

我使用马尔可夫聚类来对这个矩阵进行聚类

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,)]

我确定了一定范围内的集群膨胀值的模块度,这样我们就可以通过下式为给定的图选择最佳的集群膨胀值:

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)

但我得到一个错误说:“float”对象不可迭代。如何修复它?

cwtwac6a

cwtwac6a1#

您必须将矩阵转换为备用矩阵:

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)

输出:

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
qkf9rpyu

qkf9rpyu2#

你错过了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)

相关问题