python - 如何从主题建模中制作主题百分比条形图?
问题描述
我已经为此绞尽脑汁一个星期了。
我想要
- 运行 NMF 主题建模
- 通过查看权重的最大值为每个文档分配一个主题,
- 使用 matplot 将此分布绘制为 % 条形图。(即:X 轴上的主题,以及 y 轴上属于该主题的 % 文档。)
这是一些玩具数据并完成步骤 1 和 2:
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.decomposition import NMF
import pandas as pd
# Get data
data = {
"Documents": ["I am a document",
"And me too",
"The cat is big",
"The dog is big"
"My headphones are large",
"My monitor has rabies",
"My headphones are loud"
"The street is loud "]
}
df = pd.DataFrame(data)
# Fit a TFIDF vectorizer
tfidf_vectorizer = TfidfVectorizer()
tfidf = tfidf_vectorizer.fit_transform(df['Documents'])
# Run NMF
nmf_model = NMF(n_components=4, random_state=1).fit(tfidf)
# Weights
W = nmf_model.transform(tfidf)
# Topics
H= nmf_model.components_
现在,我可以将文档分配给主题:
# Will return document topics as list like [1, 4, 1...] to
# represent that the first document is topic 1, the second 4, and so on.
topics = pd.DataFrame(W).idxmax(axis=1, skipna=True).tolist()
好吧,现在我应该能够通过这两种结构得到我想要的东西,但我不知所措。
解决方案
推荐阅读
- c# - Sensenet:调用 user.save() 时“节点 ID 已过期”
- angular - 错误 NgFor 仅支持绑定到 Iterables,例如 Arrays
- javascript - 在 asp:ListView 中捕获并重定向从 asp:Panel 到 asp:LinkButton 的点击事件
- r - 如何构建本地安装的 R 包?
- linux - PostgreSQL installing from source - conflicting types for 'copy_file_range' error
- c# - CodeGeneration 到 Json 文件的构建时间类
- c# - POST call with application/octet-stream
- apache-spark - Spark Streaming reduceByKeyAndWindow 用于移动平均计算
- python - 如何将目录中的一组图像输入 python 以用作训练集?
- swift - 由于 SDWebImage 图像高度而调整表格视图高度