2015-09-12 29 views
1

我使用Python 2.7創建一個項目,該項目將使用Twitter數據並對其進行分析。主要概念是收集推文,並獲取該推文集中最常用的主題標籤,然後我需要創建一個圖表,其中主題標籤將成爲節點。如果這些井號標籤恰好出現在相同的推文中,而該推文會是圖中的一條邊,並且該邊的權重將成爲同現號。所以,我想創建一個使用defaultdict(lambda : defaultdict(int))詞典的詞典,並使用networkx.from_dict_of_dictsPython:從共生矩陣創建無向加權圖

我的代碼用於創建共生矩陣的曲線

def coocurrence (common_entities): 


com = defaultdict(lambda : defaultdict(int)) 

# Build co-occurrence matrix 
for i in range(len(common_entities)-1):    
    for j in range(i+1, len(common_entities)): 
     w1, w2 = sorted([common_entities[i], common_entities[j]])     
     if w1 != w2: 
      com[w1][w2] += 1 


return com 

但爲了使用networkx.from_dict_of_dicts我需要它在這種格式:com= {0: {1:{'weight':1}}}

你有什麼想法我可以解決這個問題嗎?或者以不同的方式創建這樣的圖表?

回答

1

首先,我會先排序實體,所以你不會在循環內部不斷地進行排序。然後我會使用itertools.combinations來獲取組合。你需要這些變化什麼直接的翻譯是這樣的:

from itertools import combinations 
from collections import defaultdict 


def coocurrence (common_entities): 

    com = defaultdict(lambda : defaultdict(lambda: {'weight':0})) 

    # Build co-occurrence matrix 
    for w1, w2 in combinations(sorted(common_entities), 2): 
     if w1 != w2: 
      com[w1][w2]['weight'] += 1 

    return com 

print coocurrence('abcaqwvv') 

它可能更有效(較少的索引,創造較少的對象)來構建別的東西,然後再產生第二個循環的最終答案。第二個循環不會像第一個循環那樣運行多個循環,因爲所有計數都已經計算完畢。此外,由於第二個循環沒有運行多個循環,因此可能會將if statement推遲到第二個循環可以節省更多時間。像往常一樣,在多個變體運行timeit如果你關心,但這裏是兩個循環解決方案的一個可能的例子:

def coocurrence (common_entities): 

    com = defaultdict(int) 

    # Build co-occurrence matrix 
    for w1, w2 in combinations(sorted(common_entities), 2): 
     com[w1, w2] += 1 

    result = defaultdict(dict) 
    for (w1, w2), count in com.items(): 
     if w1 != w2: 
      result[w1][w2] = {'weight': count} 
    return result 

print coocurrence('abcaqwvv') 
+0

雖然在我的項目的進一步工作,我創建了一個從我得到了使用矩陣創建一個圖形功能我的代碼 'DEF create_graph(cooccurrence_matrix): 克= nx.Graph() 對於E,共在cooccurrence_matrix.iteritems(): 如果共> = 3: g.add_edge(E [0],E [ 1],weight = co) return g' 但是,當我運行它說'TypeError:'collections.defaultdict'對象不可調用',我不能figu回答原因。你有什麼想法? – banana

+0

@banana - 該代碼中沒有任何內容突出顯示爲有問題,但我不是一位NX專家。通常情況下,整個診斷回溯是有用的,並且在評論中不太適合,所以如果您可以創建[mvce](http://stackoverflow.com/help/mcve),則可能值得發佈一個新問題。 –