Python 将值从字典转换为矩阵

Python 将值从字典转换为矩阵,python,list,dictionary,matrix,nested-lists,Python,List,Dictionary,Matrix,Nested Lists,我计算了一个单词在文本文档中出现的次数,并将这些值放入字典中。现在,我想将这些数量添加到一个矩阵中,该矩阵由文本文件作为列,不同的单词作为行组成。 这是字典的输出: {'test1.txt': {'peer': 1, 'appel': 1, 'moes': 1}, 'test2.txt': {'peer': 1, 'appel': 1}, 'test3.txt': {'peer': 1, 'moes': 2}, 'test4.txt': {'peer': 1, 'moes': 1, 'an

我计算了一个单词在文本文档中出现的次数,并将这些值放入字典中。现在,我想将这些数量添加到一个矩阵中,该矩阵由文本文件作为列,不同的单词作为行组成。 这是字典的输出:

{'test1.txt': {'peer': 1, 'appel': 1, 'moes': 1}, 
'test2.txt': {'peer': 1, 'appel': 1}, 
'test3.txt': {'peer': 1, 'moes': 2}, 
'test4.txt': {'peer': 1, 'moes': 1, 'ananas': 1}}
矩阵的输出必须如下所示:

[['', 'test1.txt', 'test2.txt', 'test3.txt', 'test4.txt'],
['moes', 1, 0, 2, 1],
['appel', 1, 1, 0, 0],
['peer', 1, 1, 1, 1],
['ananas', 0, 0, 0, 1]]
这是我现在打印矩阵的代码,但是每个文档中出现一个单词的次数还没有实现

term_freq_matrix = []

list_of_files.insert(0," ")
term_freq_matrix.insert(1, list_of_files)

for unique_word in unique_words:
    unique_word = unique_word.split()
    term_freq_matrix.append(unique_word)

print(term_freq_matrix)
谢谢

your_dict = {'test1.txt': {'peer': 1, 'appel': 1, 'moes': 1}, 
'test2.txt': {'peer': 1, 'appel': 1}, 
'test3.txt': {'peer': 1, 'moes': 2}, 
'test4.txt': {'peer': 1, 'moes': 1, 'ananas': 1}}
sklearn有一个整洁的实用程序类用于这种预处理

from sklearn.feature_extraction import DictVectorizer
vectorizer = DictVectorizer()
matrix = vectorizer.fit_transform(list(your_dict.values()))

字典键到矩阵索引的映射可在
矢量器上找到。词汇表\uuuu

要在没有外部库的情况下完成此操作,请执行以下操作:

代码:

d = {'test1.txt': {'peer': 1, 'appel': 1, 'moes': 1}, 
    'test2.txt': {'peer': 1, 'appel': 1}, 
    'test3.txt': {'peer': 1, 'moes': 2}, 
    'test4.txt': {'peer': 1, 'moes': 1, 'ananas': 1}}

res = [[''] + list(d.keys())]
for c in set(k for v in d.values() for k in v.keys()):
    res.append([c] + [d[k].get(c, 0) for k in res[0][1:]])
>>> res
[['', 'test1.txt', 'test2.txt', 'test3.txt', 'test4.txt'],
 ['peer', 1, 1, 1, 1],
 ['ananas', 0, 0, 0, 1],
 ['appel', 1, 1, 0, 0],
 ['moes', 1, 0, 2, 1]]
输出:

d = {'test1.txt': {'peer': 1, 'appel': 1, 'moes': 1}, 
    'test2.txt': {'peer': 1, 'appel': 1}, 
    'test3.txt': {'peer': 1, 'moes': 2}, 
    'test4.txt': {'peer': 1, 'moes': 1, 'ananas': 1}}

res = [[''] + list(d.keys())]
for c in set(k for v in d.values() for k in v.keys()):
    res.append([c] + [d[k].get(c, 0) for k in res[0][1:]])
>>> res
[['', 'test1.txt', 'test2.txt', 'test3.txt', 'test4.txt'],
 ['peer', 1, 1, 1, 1],
 ['ananas', 0, 0, 0, 1],
 ['appel', 1, 1, 0, 0],
 ['moes', 1, 0, 2, 1]]
熊猫:

import pandas as pd
df = pd.DataFrame(d).fillna(0)  #d is your dictionary
result = [[''] + df.columns.to_numpy().tolist()]+ df.reset_index().to_numpy().tolist()
print(result)
输出

[['', 'test1.txt', 'test2.txt', 'test3.txt', 'test4.txt'],
 ['ananas', 0.0, 0.0, 0.0, 1.0], 
 ['appel', 1.0, 1.0, 0.0, 0.0], 
 ['moes', 1.0, 0.0, 2.0, 1.0], 
 ['peer', 1.0, 1.0, 1.0, 1.0]]

您可以使用嵌套列表:

data = {'test1.txt': {'peer': 1, 'appel': 1, 'moes': 1}, 'test2.txt': {'peer': 1, 'appel': 1}, 'test3.txt': {'peer': 1, 'moes': 2}, 'test4.txt': {'peer': 1, 'moes': 1, 'ananas': 1}}
h, v = data.keys(), {i for b in data.values() for i in b}
r = [['', *h], *[[b, *[data[k].get(b, 0) for k in h]] for b in v]]
输出:

[['', 'test1.txt', 'test2.txt', 'test3.txt', 'test4.txt'], 
 ['moes', 1, 0, 2, 1], 
 ['peer', 1, 1, 1, 1], 
 ['appel', 1, 1, 0, 0], 
 ['ananas', 0, 0, 0, 1]]