我有一个数组看起来像:
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”对象不可迭代。如何修复它?
2条答案
按热度按时间cwtwac6a1#
您必须将矩阵转换为备用矩阵:
输出:
qkf9rpyu2#
你错过了
matrix = nx.to_scipy_sparse_array(network)