{"id":1083771,"date":"2025-01-08T13:01:15","date_gmt":"2025-01-08T05:01:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1083771.html"},"modified":"2025-01-08T13:01:17","modified_gmt":"2025-01-08T05:01:17","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%bf%9b%e8%a1%8c%e7%a4%be%e5%8c%ba%e5%8f%91%e7%8e%b0%e7%ae%97%e6%b3%95-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1083771.html","title":{"rendered":"\u5982\u4f55\u7528python\u8fdb\u884c\u793e\u533a\u53d1\u73b0\u7b97\u6cd5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194148\/32eb4e59-99a7-40ce-b68c-cd00ea3d74c1.webp\" alt=\"\u5982\u4f55\u7528python\u8fdb\u884c\u793e\u533a\u53d1\u73b0\u7b97\u6cd5\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u8fdb\u884c\u793e\u533a\u53d1\u73b0\u7b97\u6cd5\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5982Louv<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n\u7b97\u6cd5\u3001Girvan-Newman\u7b97\u6cd5\u3001Label Propagation\u7b97\u6cd5\u7b49\u3002<\/strong> \u5176\u4e2d<strong>Louvain\u7b97\u6cd5<\/strong>\u56e0\u5176\u9ad8\u6548\u6027\u548c\u6613\u7528\u6027\u53d7\u5230\u5e7f\u6cdb\u5e94\u7528\u3002\u793e\u533a\u53d1\u73b0\u7b97\u6cd5\u901a\u8fc7\u8bc6\u522b\u56fe\u4e2d\u8282\u70b9\u4e4b\u95f4\u7684\u7d27\u5bc6\u8fde\u63a5\u7fa4\u4f53\uff0c\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u590d\u6742\u7f51\u7edc\u7684\u7ed3\u6784\u3002Python\u7684NetworkX\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u793e\u533a\u53d1\u73b0\u7b97\u6cd5\u7684\u5b9e\u73b0\uff0c\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u7b97\u6cd5\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Louvain\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>Louvain\u7b97\u6cd5\u662f\u4e00\u79cd\u57fa\u4e8e\u6a21\u5757\u5ea6\u4f18\u5316\u7684\u793e\u533a\u53d1\u73b0\u7b97\u6cd5\u3002\u6a21\u5757\u5ea6\u662f\u8861\u91cf\u7f51\u7edc\u5212\u5206\u8d28\u91cf\u7684\u6307\u6807\uff0c\u503c\u8d8a\u5927\u8868\u793a\u5212\u5206\u6548\u679c\u8d8a\u597d\u3002Louvain\u7b97\u6cd5\u901a\u8fc7\u6700\u5927\u5316\u6a21\u5757\u5ea6\u6765\u53d1\u73b0\u793e\u533a\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>1\u3001Louvain\u7b97\u6cd5\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>Louvain\u7b97\u6cd5\u7684\u57fa\u672c\u601d\u60f3\u662f\u901a\u8fc7\u4e0d\u65ad\u5730\u5408\u5e76\u8282\u70b9\u548c\u793e\u533a\u6765\u6700\u5927\u5316\u6a21\u5757\u5ea6\u3002\u9996\u5148\uff0c\u5c06\u6bcf\u4e2a\u8282\u70b9\u521d\u59cb\u5316\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684\u793e\u533a\uff0c\u7136\u540e\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\uff0c\u5c1d\u8bd5\u5c06\u6bcf\u4e2a\u8282\u70b9\u79fb\u52a8\u5230\u5176\u90bb\u5c45\u8282\u70b9\u6240\u5728\u7684\u793e\u533a\uff0c\u4ee5\u589e\u52a0\u6a21\u5757\u5ea6\u3002\u8fed\u4ee3\u7ed3\u675f\u540e\uff0c\u5c06\u6240\u6709\u8282\u70b9\u5408\u5e76\u4e3a\u4e00\u4e2a\u65b0\u8282\u70b9\uff0c\u5f62\u6210\u4e00\u4e2a\u65b0\u7684\u7f51\u7edc\u7ed3\u6784\uff0c\u91cd\u590d\u4e0a\u8ff0\u8fc7\u7a0b\uff0c\u76f4\u5230\u6a21\u5757\u5ea6\u4e0d\u518d\u589e\u52a0\u3002<\/p>\n<\/p>\n<p><h4>2\u3001Louvain\u7b97\u6cd5\u5b9e\u73b0<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528Python\u7684NetworkX\u5e93\u548cCommunity\u5e93\u5b9e\u73b0Louvain\u7b97\u6cd5\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import networkx as nx<\/p>\n<p>import community as community_louvain<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u56fe<\/strong><\/h2>\n<p>G = nx.karate_club_graph()<\/p>\n<h2><strong>\u8ba1\u7b97\u6700\u4f73\u5206\u533a<\/strong><\/h2>\n<p>partition = community_louvain.best_partition(G)<\/p>\n<h2><strong>\u6839\u636e\u793e\u533a\u5206\u914d\u8282\u70b9\u989c\u8272<\/strong><\/h2>\n<p>pos = nx.spring_layout(G)<\/p>\n<p>cmap = plt.get_cmap(&#39;viridis&#39;)<\/p>\n<p>colors = [cmap(partition[node]) for node in G.nodes()]<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>nx.draw_networkx(G, pos, node_color=colors, with_labels=True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u793a\u4f8b\u56fe\uff08\u7a7a\u624b\u9053\u4ff1\u4e50\u90e8\u56fe\uff09\uff0c\u7136\u540e\u4f7f\u7528<code>community_louvain.best_partition<\/code>\u51fd\u6570\u8ba1\u7b97\u6700\u4f73\u5206\u533a\uff0c\u5e76\u5c06\u8282\u70b9\u6309\u793e\u533a\u5206\u914d\u989c\u8272\uff0c\u6700\u540e\u7ed8\u5236\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001Girvan-Newman\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>Girvan-Newman\u7b97\u6cd5\u662f\u4e00\u79cd\u57fa\u4e8e\u8fb9\u4ecb\u6570\u7684\u793e\u533a\u53d1\u73b0\u7b97\u6cd5\u3002\u8fb9\u4ecb\u6570\u662f\u6307\u901a\u8fc7\u4e00\u6761\u8fb9\u7684\u6700\u77ed\u8def\u5f84\u6570\u91cf\uff0c\u7b97\u6cd5\u901a\u8fc7\u79fb\u9664\u9ad8\u4ecb\u6570\u503c\u7684\u8fb9\u6765\u62c6\u5206\u7f51\u7edc\uff0c\u76f4\u5230\u5f62\u6210\u660e\u786e\u7684\u793e\u533a\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>1\u3001Girvan-Newman\u7b97\u6cd5\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>Girvan-Newman\u7b97\u6cd5\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u8ba1\u7b97\u56fe\u4e2d\u6bcf\u6761\u8fb9\u7684\u8fb9\u4ecb\u6570\u3002<\/li>\n<li>\u79fb\u9664\u8fb9\u4ecb\u6570\u6700\u5927\u7684\u8fb9\u3002<\/li>\n<li>\u91cd\u65b0\u8ba1\u7b97\u56fe\u4e2d\u6bcf\u6761\u8fb9\u7684\u8fb9\u4ecb\u6570\u3002<\/li>\n<li>\u91cd\u590d\u6b65\u9aa42\u548c3\uff0c\u76f4\u5230\u56fe\u88ab\u62c6\u5206\u6210\u591a\u4e2a\u793e\u533a\u3002<\/li>\n<\/ol>\n<p><h4>2\u3001Girvan-Newman\u7b97\u6cd5\u5b9e\u73b0<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528Python\u7684NetworkX\u5e93\u5b9e\u73b0Girvan-Newman\u7b97\u6cd5\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import networkx as nx<\/p>\n<p>from networkx.algorithms.community import girvan_newman<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u56fe<\/strong><\/h2>\n<p>G = nx.karate_club_graph()<\/p>\n<h2><strong>\u8ba1\u7b97\u793e\u533a\u7ed3\u6784<\/strong><\/h2>\n<p>communities = girvan_newman(G)<\/p>\n<p>first_level_communities = next(communities)<\/p>\n<p>sorted(map(sorted, first_level_communities))<\/p>\n<h2><strong>\u6839\u636e\u793e\u533a\u5206\u914d\u8282\u70b9\u989c\u8272<\/strong><\/h2>\n<p>pos = nx.spring_layout(G)<\/p>\n<p>cmap = plt.get_cmap(&#39;viridis&#39;)<\/p>\n<p>colors = [cmap(i) for i, community in enumerate(first_level_communities) for node in community]<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>nx.draw_networkx(G, pos, node_color=colors, with_labels=True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u793a\u4f8b\u56fe\uff0c\u5e76\u4f7f\u7528<code>girvan_newman<\/code>\u51fd\u6570\u8ba1\u7b97\u793e\u533a\u7ed3\u6784\uff0c\u63d0\u53d6\u7b2c\u4e00\u6b21\u62c6\u5206\u540e\u7684\u793e\u533a\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u793e\u533a\u5206\u914d\u989c\u8272\uff0c\u6700\u540e\u7ed8\u5236\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001Label Propagation\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>Label Propagation\u7b97\u6cd5\u662f\u4e00\u79cd\u57fa\u4e8e\u6807\u7b7e\u4f20\u64ad\u7684\u793e\u533a\u53d1\u73b0\u7b97\u6cd5\u3002\u6bcf\u4e2a\u8282\u70b9\u6700\u521d\u88ab\u8d4b\u4e88\u4e00\u4e2a\u552f\u4e00\u7684\u6807\u7b7e\uff0c\u7136\u540e\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\uff0c\u6bcf\u4e2a\u8282\u70b9\u9009\u62e9\u5176\u90bb\u5c45\u4e2d\u51fa\u73b0\u6b21\u6570\u6700\u591a\u7684\u6807\u7b7e\u8fdb\u884c\u66f4\u65b0\uff0c\u76f4\u5230\u6807\u7b7e\u4e0d\u518d\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><h4>1\u3001Label Propagation\u7b97\u6cd5\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>Label Propagation\u7b97\u6cd5\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6bcf\u4e2a\u8282\u70b9\u521d\u59cb\u5316\u4e3a\u4e00\u4e2a\u552f\u4e00\u7684\u6807\u7b7e\u3002<\/li>\n<li>\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\uff0c\u6bcf\u4e2a\u8282\u70b9\u9009\u62e9\u5176\u90bb\u5c45\u4e2d\u51fa\u73b0\u6b21\u6570\u6700\u591a\u7684\u6807\u7b7e\u8fdb\u884c\u66f4\u65b0\u3002<\/li>\n<li>\u91cd\u590d\u6b65\u9aa42\uff0c\u76f4\u5230\u6807\u7b7e\u4e0d\u518d\u53d8\u5316\u3002<\/li>\n<\/ol>\n<p><h4>2\u3001Label Propagation\u7b97\u6cd5\u5b9e\u73b0<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528Python\u7684NetworkX\u5e93\u5b9e\u73b0Label Propagation\u7b97\u6cd5\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import networkx as nx<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u56fe<\/strong><\/h2>\n<p>G = nx.karate_club_graph()<\/p>\n<h2><strong>\u8ba1\u7b97\u793e\u533a\u7ed3\u6784<\/strong><\/h2>\n<p>communities = nx.algorithms.community.label_propagation_communities(G)<\/p>\n<p>communities = [list(community) for community in communities]<\/p>\n<h2><strong>\u6839\u636e\u793e\u533a\u5206\u914d\u8282\u70b9\u989c\u8272<\/strong><\/h2>\n<p>pos = nx.spring_layout(G)<\/p>\n<p>cmap = plt.get_cmap(&#39;viridis&#39;)<\/p>\n<p>colors = [cmap(i) for i, community in enumerate(communities) for node in community]<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>nx.draw_networkx(G, pos, node_color=colors, with_labels=True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u793a\u4f8b\u56fe\uff0c\u5e76\u4f7f\u7528<code>label_propagation_communities<\/code>\u51fd\u6570\u8ba1\u7b97\u793e\u533a\u7ed3\u6784\uff0c\u5c06\u793e\u533a\u8f6c\u6362\u4e3a\u5217\u8868\u5f62\u5f0f\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u793e\u533a\u5206\u914d\u989c\u8272\uff0c\u6700\u540e\u7ed8\u5236\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6a21\u5757\u5ea6\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u6a21\u5757\u5ea6\u4f18\u5316\u662f\u793e\u533a\u53d1\u73b0\u7b97\u6cd5\u4e2d\u7684\u91cd\u8981\u6b65\u9aa4\uff0c\u901a\u8fc7\u4f18\u5316\u6a21\u5757\u5ea6\u53ef\u4ee5\u63d0\u9ad8\u793e\u533a\u5212\u5206\u7684\u8d28\u91cf\u3002\u6a21\u5757\u5ea6\uff08Modularity\uff09\u662f\u8861\u91cf\u793e\u533a\u5212\u5206\u8d28\u91cf\u7684\u6307\u6807\uff0c\u503c\u8d8a\u5927\u8868\u793a\u793e\u533a\u5212\u5206\u6548\u679c\u8d8a\u597d\u3002\u6a21\u5757\u5ea6\u4f18\u5316\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u8d2a\u5fc3\u6a21\u5757\u5ea6\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u8d2a\u5fc3\u6a21\u5757\u5ea6\u4f18\u5316\u662f\u4e00\u79cd\u9010\u6b65\u5408\u5e76\u8282\u70b9\u548c\u793e\u533a\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u4e0d\u65ad\u5730\u5408\u5e76\u53ef\u4ee5\u589e\u52a0\u6a21\u5757\u5ea6\u7684\u8282\u70b9\u548c\u793e\u533a\uff0c\u6765\u5b9e\u73b0\u6a21\u5757\u5ea6\u6700\u5927\u5316\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528Python\u7684NetworkX\u5e93\u5b9e\u73b0\u8d2a\u5fc3\u6a21\u5757\u5ea6\u4f18\u5316\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import networkx as nx<\/p>\n<p>from networkx.algorithms.community import greedy_modularity_communities<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u56fe<\/strong><\/h2>\n<p>G = nx.karate_club_graph()<\/p>\n<h2><strong>\u8ba1\u7b97\u793e\u533a\u7ed3\u6784<\/strong><\/h2>\n<p>communities = greedy_modularity_communities(G)<\/p>\n<p>communities = [list(community) for community in communities]<\/p>\n<h2><strong>\u6839\u636e\u793e\u533a\u5206\u914d\u8282\u70b9\u989c\u8272<\/strong><\/h2>\n<p>pos = nx.spring_layout(G)<\/p>\n<p>cmap = plt.get_cmap(&#39;viridis&#39;)<\/p>\n<p>colors = [cmap(i) for i, community in enumerate(communities) for node in community]<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>nx.draw_networkx(G, pos, node_color=colors, with_labels=True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u793a\u4f8b\u56fe\uff0c\u5e76\u4f7f\u7528<code>greedy_modularity_communities<\/code>\u51fd\u6570\u8ba1\u7b97\u793e\u533a\u7ed3\u6784\uff0c\u5c06\u793e\u533a\u8f6c\u6362\u4e3a\u5217\u8868\u5f62\u5f0f\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u793e\u533a\u5206\u914d\u989c\u8272\uff0c\u6700\u540e\u7ed8\u5236\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8c31\u805a\u7c7b<\/h4>\n<\/p>\n<p><p>\u8c31\u805a\u7c7b\u662f\u4e00\u79cd\u57fa\u4e8e\u56fe\u7684\u62c9\u666e\u62c9\u65af\u77e9\u9635\u7684\u793e\u533a\u53d1\u73b0\u7b97\u6cd5\uff0c\u901a\u8fc7\u8ba1\u7b97\u56fe\u7684\u62c9\u666e\u62c9\u65af\u77e9\u9635\u7684\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf\uff0c\u5c06\u8282\u70b9\u6620\u5c04\u5230\u4f4e\u7ef4\u7a7a\u95f4\uff0c\u7136\u540e\u4f7f\u7528\u805a\u7c7b\u7b97\u6cd5\uff08\u5982K-means\uff09\u8fdb\u884c\u793e\u533a\u5212\u5206\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528Python\u7684NetworkX\u5e93\u548cScikit-learn\u5e93\u5b9e\u73b0\u8c31\u805a\u7c7b\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import networkx as nx<\/p>\n<p>import numpy as np<\/p>\n<p>from sklearn.cluster import KMeans<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u56fe<\/strong><\/h2>\n<p>G = nx.karate_club_graph()<\/p>\n<h2><strong>\u8ba1\u7b97\u62c9\u666e\u62c9\u65af\u77e9\u9635<\/strong><\/h2>\n<p>L = nx.normalized_laplacian_matrix(G).todense()<\/p>\n<h2><strong>\u8ba1\u7b97\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf<\/strong><\/h2>\n<p>eigvals, eigvecs = np.linalg.eig(L)<\/p>\n<h2><strong>\u9009\u62e9\u524dk\u4e2a\u7279\u5f81\u5411\u91cf<\/strong><\/h2>\n<p>k = 4<\/p>\n<p>X = eigvecs[:, :k]<\/p>\n<h2><strong>\u4f7f\u7528K-means\u8fdb\u884c\u805a\u7c7b<\/strong><\/h2>\n<p>kmeans = KMeans(n_clusters=k)<\/p>\n<p>kmeans.fit(X)<\/p>\n<p>labels = kmeans.labels_<\/p>\n<h2><strong>\u6839\u636e\u793e\u533a\u5206\u914d\u8282\u70b9\u989c\u8272<\/strong><\/h2>\n<p>pos = nx.spring_layout(G)<\/p>\n<p>cmap = plt.get_cmap(&#39;viridis&#39;)<\/p>\n<p>colors = [cmap(label) for label in labels]<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>nx.draw_networkx(G, pos, node_color=colors, with_labels=True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u793a\u4f8b\u56fe\uff0c\u8ba1\u7b97\u62c9\u666e\u62c9\u65af\u77e9\u9635\u7684\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf\uff0c\u9009\u62e9\u524dk\u4e2a\u7279\u5f81\u5411\u91cf\uff0c\u4f7f\u7528K-means\u8fdb\u884c\u805a\u7c7b\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u793e\u533a\u5206\u914d\u989c\u8272\uff0c\u6700\u540e\u7ed8\u5236\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u4ecb\u7ecd\u4e86\u51e0\u79cd\u5e38\u7528\u7684\u793e\u533a\u53d1\u73b0\u7b97\u6cd5\uff0c\u5305\u62ecLouvain\u7b97\u6cd5\u3001Girvan-Newman\u7b97\u6cd5\u3001Label Propagation\u7b97\u6cd5\u3001\u8d2a\u5fc3\u6a21\u5757\u5ea6\u4f18\u5316\u548c\u8c31\u805a\u7c7b\u3002\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Python\u7684NetworkX\u5e93\u548c\u5176\u4ed6\u76f8\u5173\u5e93\u5b9e\u73b0\u8fd9\u4e9b\u7b97\u6cd5\u3002\u793e\u533a\u53d1\u73b0\u662f\u7f51\u7edc\u5206\u6790\u4e2d\u7684\u91cd\u8981\u95ee\u9898\uff0c\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u5e94\u7528\u793e\u533a\u53d1\u73b0\u7b97\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u590d\u6742\u7f51\u7edc\u7684\u7ed3\u6784\u548c\u529f\u80fd\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5bf9\u4f60\u5728\u4f7f\u7528Python\u8fdb\u884c\u793e\u533a\u53d1\u73b0\u7b97\u6cd5\u65f6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u793e\u533a\u53d1\u73b0\u7b97\u6cd5\uff1f<\/strong><br \/>\u5728\u9009\u62e9\u793e\u533a\u53d1\u73b0\u7b97\u6cd5\u65f6\uff0c\u9700\u8981\u8003\u8651\u6570\u636e\u7684\u7279\u70b9\u548c\u76ee\u6807\u3002\u4e0d\u540c\u7684\u7b97\u6cd5\u9002\u5408\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002\u4f8b\u5982\uff0cLouvain\u7b97\u6cd5\u9002\u5408\u4e8e\u5927\u89c4\u6a21\u7f51\u7edc\uff0c\u800cGirvan-Newman\u7b97\u6cd5\u5219\u9002\u7528\u4e8e\u8f83\u5c0f\u7684\u7f51\u7edc\u3002\u4e86\u89e3\u6bcf\u79cd\u7b97\u6cd5\u7684\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u505a\u51fa\u66f4\u660e\u667a\u7684\u9009\u62e9\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5b9e\u73b0\u793e\u533a\u53d1\u73b0\u7b97\u6cd5\u7684\u5e93\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>Python\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u4e8e\u793e\u533a\u53d1\u73b0\uff0c\u4f8b\u5982NetworkX\u3001igraph\u548cPyTorch Geometric\u7b49\u3002NetworkX\u63d0\u4f9b\u4e86\u591a\u79cd\u793e\u533a\u53d1\u73b0\u7b97\u6cd5\u7684\u5b9e\u73b0\uff0cigraph\u5219\u5728\u5904\u7406\u5927\u578b\u56fe\u65f6\u8868\u73b0\u66f4\u4f73\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u7684\u5e93\u8fdb\u884c\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u793e\u533a\u53d1\u73b0\u7b97\u6cd5\u7684\u7ed3\u679c\u5982\u4f55\u8bc4\u4f30\uff1f<\/strong><br \/>\u8bc4\u4f30\u793e\u533a\u53d1\u73b0\u7684\u6548\u679c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u6307\u6807\uff0c\u4f8b\u5982\u6a21\u5757\u5ea6\uff08Modularity\uff09\u3001NMI\uff08Normalized Mutual Information\uff09\u548c\u7cbe\u786e\u5ea6\u7b49\u3002\u8fd9\u4e9b\u6307\u6807\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5224\u65ad\u6240\u4f7f\u7528\u7b97\u6cd5\u7684\u6027\u80fd\u4ee5\u53ca\u8bc6\u522b\u7684\u793e\u533a\u7ed3\u6784\u7684\u8d28\u91cf\u3002\u901a\u8fc7\u5bf9\u6bd4\u4e0d\u540c\u7b97\u6cd5\u7684\u8bc4\u4f30\u7ed3\u679c\uff0c\u53ef\u4ee5\u9009\u62e9\u51fa\u6700\u4f73\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u8fdb\u884c\u793e\u533a\u53d1\u73b0\u7b97\u6cd5\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5982LouvAIn\u7b97\u6cd5\u3001Girvan-Newman\u7b97\u6cd5\u3001Label [&hellip;]","protected":false},"author":3,"featured_media":1083781,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1083771"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1083771"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1083771\/revisions"}],"predecessor-version":[{"id":1083783,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1083771\/revisions\/1083783"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1083781"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1083771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1083771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1083771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}