Windows10下 AttributeError: module 'community' has no attribute 'best_partition' 問題的解決

今天用到著名的社團檢測算法Louvain時,發現GitHub上有一個非常成熟的包,地址如下:https://github.com/JavyWang/python-louvain

但是,導入包的方式有點奇怪,用的不是包名“python-louvain”而是“community”,在jupyter中運行的時候出現標題中的錯誤:

AttributeError: module ‘community’ has no attribute 'best_partition' 

網上查了一下,有說是虛擬環境的問題,但是我沒有用虛擬環境,於是到我係統中的python目錄下將原來的包文件全部刪除重新安裝。

之前安裝是用下面的命令

pip install python-louvain

這次我直接將GitHub中的包文件下載到python庫中,下載路徑爲:https://github.com/taynaud/python-louvain/archive/master.zip

解壓後在python-louvain-master所在的目錄打開cmd,用下面的命令安裝

python setup.py install

安裝完畢後發現,site-packages中多了一個community包

在這裏插入圖片描述

這應該是到導入的包了,在pycharm裏面運行了一下,沒有問題。

import networkx as nx
import time
import pandas as pd
import community

input_file = 'F:/Data/CI_PLP/p2p-Gnutella24/p2p-Gnutella24_1.txt'
data = pd.read_csv(input_file, sep='	', header=None, names=['A', 'B'], encoding='utf-8')
# 轉換得到邊列表
edgelist = data[['A', 'B']].values.tolist()
# 讀取鄰接列表生成網絡
G = nx.from_edgelist(edgelist)
start_time = time.time()
coms = community.best_partition(G)
end_time = time.time()
print(end_time-start_time)

用時如下

C:\Users\WJW\AppData\Local\Programs\Python\Python36\python.exe F:/PYTHON/CI_PLP/main.py
87.85697078704834

Process finished with exit code 0

然後回到jupyter中跑一下,還是有問題,於是重啓jupyter,好了。

在這裏插入圖片描述

可以運行,但是時間長了8秒左右。再次運行一遍,時間少了很多。

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章