2016-12-23 77 views
0

我剛開始使用Python和我只是碰到了下面的問題來了:如何創建頻率矩陣?

想象我列出的以下列表:

list = [["Word1","Word2","Word2","Word4566"],["Word2", "Word3", "Word4"], ...] 

結果(矩陣),我想應該是這樣的: screenshot

顯示的列和行都顯示單詞(不管是哪個列表)。

我想要的是一個程序,它計算每個列表中單詞的出現(按列表)。

圖片是第一個列表後的結果。

有沒有簡單的方法來實現這樣的東西或類似的東西?


編輯: 基本上我想有一個列表/矩陣,告訴我2-4566次多少字出現時,字1也是在列表中,等等。

因此,我會得到每個單詞的列表,顯示所有其他4555個單詞與該單詞關係的絕對頻率。


所以我需要一個算法,通過文字的這一切列出了迭代和竣工圖的結果列表

+1

這是不是很清楚你在問什麼。列和行的交點意味着什麼? – Nurjan

+0

列和行的區段間並不重要。我不需要它們,所以它們可以是0或空白或任何 – mgruber

+0

你能澄清一下你想做什麼嗎?你想統計每個單詞在每個列表中出現多少次?如果是,字數和矩陣之間的關係是什麼? – Nurjan

回答

0

我設法想出了我自己的問題的正確答案:

list = [["Word1","Word2","Word2"],["Word2", "Word3", "Word4"],["Word2","Word3"]] 

#Names of all dicts 
all_words = sorted(set([w for sublist in list for w in sublist])) 

#Creating the dicts 
dicts = [] 
for i in all_words: 
    dicts.append([i, dict.fromkeys([w for w in all_words if w != i],0)]) 

#Updating the dicts 
for l in list: 
    for word in sorted(set(l)): 
     tmpL = [w for w in l if w != word] 
     ind = ([w[0] for w in dicts].index(word)) 

     for w in dicts[ind][1]: 
      dicts[ind][1][w] += l.count(w) 

print dicts 

獲取結果:

[ '字1',{ 'Word4':0, 'WORD3':0, '字2':2}],[ '字2',{'Word4 ':1,'Word1':1,'Word3':2}],['Word3',{'Word4':1,'Word1':0,'Word2':2}],['Word4',{ 'Word1':0,'Word3':1,'Word2':1}]]

1

我覺得真的很難理解你真正要求的,但我會通過使試一些假設:

  • (1)您有多個單詞的列表(一個),含其他列表(b)(瓦特)。
  • (2)對於在各b -list甲 -list
    • (3)對於每一個瓦特b
      • (3.1)計數的總數出現w in all of the b -lists
      • (3.2)計算有多少個b - 列出,其中瓦特如果這些假設是正確的只出現一次

,則該表不能正確對應您所提供的列表。如果我的假設是錯誤的,那麼我仍然相信我的解決方案可能會給你靈感或關於如何正確解決它的一些想法。最後,我不認爲我的解決方案在速度或類似方面是最佳的。

OBS !!我使用python的內置字典,如果你打算填充數千個單詞,它可能會變得非常慢!看一看:https://docs.python.org/2/tutorial/datastructures.html#dictionaries

frq_dict = {} # num of appearances/frequency 
    uqe_dict = {} # unique 

    for list_b in list_A: 
      temp_dict = {} 
      for word in list_b: 
        if(word in temp_dict): 
          temp_dict[word]+=1 
        else: 
          temp_dict[word]=1 

      # frq is the number of appearances 
      for word, frq in temp_dict.iteritems(): 
        if(frq > 1): 
          if(word in frq_dict) 
            frq_dict[word] += frq 
          else 
            frq_dict[word] = frq 
        else: 
          if(word in uqe_dict) 
            uqe_dict[word] += 1 
          else 
            uqe_dict[word] = 1 
+0

@mgruber我看到你的編輯有點太晚了。我會盡量做出更新的解決方案。 – magnus

+0

感謝您的回答! 我需要的是每個單詞的字典,其中包含除了一個單詞之外的所有其他單詞。字典然後通過列表逐個列表填充。因此,如果列表1包含word1,word1的詞典中填充了word2,word3等次數出現在同一列表(僅)中的次數。這是爲第一個列表中的每個單詞完成的。如果再次出現單詞1,則會移至list2,word2,word3等出現的次數將添加到已有數字的頂部。 – mgruber

+0

確實。您可以通過填寫其他字典的字典來解決此問題。但是,對於許多單詞來說,這將會非常緩慢。你在這裏要求相當大量的信息。你有沒有想過你想怎麼做,或者你需要更多的幫助? – magnus

2

據我瞭解,你想創建一個矩陣,其中顯示出兩個詞一起位於每對單詞的列表的數量。

首先我們應該解決的一組唯一字:

lst = [["Word1","Word2","Word2","Word4566"],["Word2", "Word3", "Word4"], ...] # list is a reserved word in python, don't use it as a name of variables 

words = set() 
for sublst in lst: 
    words |= set(sublst) 
words = list(words) 

其次,我們應該定義一個矩陣零:

result = [[0] * len(words)] * len(words) # zeros matrix N x N 

最後,我們填補了矩陣通過給定列表吧:

for sublst in lst: 
    sublst = list(set(sublst)) # selecting unique words only 
    for i in xrange(len(sublst)): 
     for j in xrange(i + 1, len(sublst)): 
      index1 = words.index(sublst[i]) 
      index2 = words.index(sublst[j]) 
      result[index1][index2] += 1 
      result[index2][index1] += 1 

print result