我有一个数组看起来:
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)
但我收到一条错误消息:“浮动”对象不可迭代。我该如何解决?
您必须将矩阵转换为备用矩阵:
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
你错过了
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)