自动检测 LDA 和 NMF 的稳健参数化。与 scikit-learn 和 gensim 兼容。
项目描述
机器人
rob ustTop ics是一个针对对构建健壮主题模型感兴趣的非机器学习专家的库。主要目标是提供一个简单易用的框架来检查主题模型是否达到每次运行相同或至少相似的结果。
特征
- 支持 sklearn (LatentDirichletAllocation, NMF) 和 gensim (LdaModel, ldamulticore, nmf) 主题模型
- 基于sobol 序列创建样本,这需要比网格搜索更少的样本,并确保使用整个参数空间,这在随机抽样中是不确定的。
- 使用基于词向量的连贯性分数在每个样本的不同重新初始化之间进行简单的主题匹配。
- 所有模型的排名基于四个指标:
- 每个主题的前 n 个词的Jaccard 距离
- 基于Jensen Shannon Divergence的主题分布相似性
- 基于Kendall 的 Tau的前 n 个词的排名相关性
- 基于词向量的连贯性分数(TC-W2V 的简单版本)
- 样本和主题模型实例的基于词的分析。
安装
- Python版本: 3.5+
- 包管理器: pip
点子
使用 pip,robics 版本可作为源包和二进制轮子获得:
pip install robics
例子
来自 sklearn 的测试数据集
from sklearn.datasets import fetch_20newsgroups
# PREPROCESSING
dataset = fetch_20newsgroups(
shuffle=True, random_state=1, remove=('headers', 'footers', 'quotes'))
# Only 1000 dokuments for performance reasons
documents = dataset.data[:1000]
加载用于连贯性计算的词向量
import spacy
nlp = spacy.load("en_core_web_md")
检测健壮的 sklearn 主题模型
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
from sklearn.decomposition import LatentDirichletAllocation, NMF
from robics import RobustTopics
# Document vectorization using TFIDF
tf_vectorizer = CountVectorizer(
max_df=0.95, min_df=2, stop_words='english')
tf = tf_vectorizer.fit_transform(documents)
tf_feature_names = tf_vectorizer.get_feature_names()
# TOPIC MODELLING
robustTopics = RobustTopics(nlp)
# Load NMF and LDA models
robustTopics.load_sklearn_model(
LatentDirichletAllocation, tf, tf_vectorizer, dimension_range=[5, 50], n_samples=4, n_initializations=3)
robustTopics.load_sklearn_model(NMF, tf, tf_vectorizer, dimension_range=[5, 50], n_samples=4, n_initializations=3)
# Fit the models - Warning, this might take a lot of time based on the number of samples (n_models*n_sample*n_initializations)
robustTopics.fit_models()
分析我们首先看所有模型的排名
robustTopics.rank_models()
# Output:
[{'model': sklearn.decomposition._nmf.NMF,
'model_id': 1,
'sample_id': 0,
'n_topics': 27,
'params': {'n_components': 27},
'topic_coherence': 0.3184709231908624,
'jaccard': 0.9976484420928865,
'kendalltau': 0.9987839560655095,
'jensenshannon': 0.9999348301145501},
{'model': sklearn.decomposition._nmf.NMF,
'model_id': 1,
'sample_id': 3,
'n_topics': 21,
'params': {'n_components': 21},
'topic_coherence': 0.31157823571739196,
'jaccard': 1.0,
'kendalltau': 1.0,
'jensenshannon': 0.9999784246484099},
...
{'model': sklearn.decomposition._lda.LatentDirichletAllocation,
'model_id': 0,
'sample_id': 1,
'n_topics': 38,
'params': {'n_components': 38},
'topic_coherence': 0.30808284548733383,
'jaccard': 0.07074176815158277,
'kendalltau': 0.1023597955928783,
'jensenshannon': 0.22596536037699871}]
顶级模型是具有 27 个主题(model_id 1 和 sample_id 0)的 NMF 模型。下一步是在单词级别上查看模型。
robustTopics.display_sample_topic_consistency(model_id=1, sample_id=0)
# Output:
Words in 3 out of 3 runs:
['edu', 'graphics', 'pub', 'mail', 'ray', '128', 'send', '3d', 'ftp', 'com', 'objects', 'server', 'amiga', 'image', 'archie', 'files', 'file', 'images', 'archive', 'package', 'section', 'firearm', 'weapon', 'military', 'license', 'shall', 'dangerous', 'person', 'division', 'application', 'means', 'device', 'use', 'following', 'issued', 'state', 'act',...
Words in 2 out of 3 runs:
['know']
Words in 1 out of 3 runs:
['goals']
大多数单词都在三个重新初始化中。只有“知道”和“目标”不一致。
相比之下,让我们看看表现最差的模型:
robustTopics.display_sample_topic_consistency(model_id=0, sample_id=1)
# Output:
Words in 3 out of 3 runs:
['know', 'know', 'people', 'use', 'drive', 'think', 'just', 'like', 'just', 'people', 'just', 'know', 'think', 'good', 'think', 'like', 'people', 'know', 'don', 'think', 'like', 'just', 'don', 'know', 'just', 'don', 'think', 'like', 'windows', 'like', 'don']
Words in 2 out of 3 runs:
['just', 'think', 'people', 'dc', 'just', 'like', 'want', 'said', 'use', 'said', 'local', 'say', 'god', 'shall', 'rights', 'know', 'like', 'window', 'like', 'just', 'time', 'new', 'like', 'just', 'good', 'don', 'used', 'does', 'think', 'like', 'new', 'state', 'like', 'contact', 'know', 'bike', 'just', 'like', 'year', 'data', 'use', 'way', 've', 'people', 'don', 'know', 'didn', 'years', 'little', 'rocket', 'like', 'generation', 'build', 'll', 'max', 'ssrt', 'just', 'time', 'using', 'edu', 'ftp', 'file', 'available', 'server', '10', 'good', 'new', 'know', 'people', 'way', 'know', 'good', 'don', 'just', 'like', 'time', 'like', 'don', 'insurance', 'year', 'time', 'car', 'years', 'people', 'say', 'new', 'new', 'need', 'just', 'think', 'like', 'good', 'just', 'don', 'work', 'time', 'government', 'rights', 'make', 'people', 'edu', 'com', 'graphics', 'time', 'good', 'people', 'need', 'like', 'don', 'know', 'just', 'people', 'server', 'edu', 'file', 'video', 'support', 'mit', 'ftp', 'linux', 'time', 'binaries', 'information', 'available', 'new', 'greek', '10', 'just', 'just', 'does', 'know', 'use', 'need', 'like', 'don', 'use', 'know', 'think', 'like', 'new', 'like', 'just', 'edu', 've', 'does', 'problem', 'think', 'just', 'way', 'power', 'wrong', 'edu', 'points', 'point', 'just', 'hp', 'don', 'good', 'people', 'phone', 'food', 'just', 'bit', 'good', 'know', 'just', 'like', 'use', 'people', 'used', 'going', 'people', 'know', 'time', 'say', 'use', 'drive', '10', 'like', 'observations', 'think', 'don', 'want', 'thing', 'know', 'll', 'good', 'like', 'mm', 'used', 'say']
Words in 1 out of 3 runs:
['jews', 'israel', 'true', 'state', 'year',
...
最差的模型在 3 次运行部分中的 1 次中包含最多的单词,并且只有填充词在运行之间是一致的。“知道”出现了五次,这意味着它在五个不同的主题中都出现了三个排名靠前的单词。
现在让我们看一下顶部和底部模型中的主题。
# Top model
robustTopics.display_sample_topics(1, 0)
# Output
Topic 0
In 3 runs: edu graphics pub mail ray 128 send 3d ftp com objects server amiga image archie files file images archive package
Topic 1
In 3 runs: section firearm weapon military license shall dangerous person division application means device use following issued state act designed code automatic
Topic 2
In 3 runs: aids health care said children medical infected new patients disease 1993 10 information research april national study trials service number
Topic 3
In 3 runs: god good people just suppose brothers makes fisher like does jews joseph did worship judaism right instead jesus end read
Topic 4
In 3 runs: server support edu file 386bsd ftp mit binaries vga information supported linux svga readme available os new faq files video
Topic 5
In 3 runs: edu com mil navy cs vote misc votes health ca hp nrl gov email cc creation au john thomas uk
Topic 6
In 3 runs: probe space titan earth orbiter launch mission jupiter orbit atmosphere 93 saturn gravity 10 surface satellite ray 12 possible 97
# Bottom model
robustTopics.display_sample_topics(0, 1)
# Output
Topic 0
In 3 runs: know
Topic 1
In 3 runs: know
Topic 2
Topic 3
Topic 4
In 3 runs: people
Topic 5
Topic 6
In 3 runs: use
Topic 7
Topic 8
In 3 runs: drive think just
顶级模型在所有运行中产生与连接的单词一致的主题。与底部模型形成强烈对比,底部模型几乎没有一致的词,这些提供的信息很少。
我们想看看顶级模型中的一个实例的主题(模型 1、样本 0、实例 0)。
robustTopics.display_run_topics(1, 0, 0)
# Output
Topic 0:
edu graphics pub mail ray 128 send 3d ftp com objects server amiga image archie files file images archive package
Topic 1:
section firearm weapon military license shall dangerous person division application means device use following issued state act designed code automatic
Topic 2:
aids health care said children medical infected new patients disease 1993 10 information research april national study trials service number
Topic 3:
god good people just suppose brothers makes fisher like does jews joseph did worship judaism right instead jesus end read
Topic 4:
server support edu file 386bsd ftp mit binaries vga information supported linux svga readme available os new faq files video
Topic 5:
edu com mil navy cs vote misc votes health ca hp nrl gov email cc creation au john thomas uk
Topic 6:
probe space titan earth orbiter launch mission jupiter orbit atmosphere 93 saturn gravity 10 surface satellite ray 12 possible 97
如果输出有意义,我们现在可以使用此模型进一步使用或导出更多独立信息以进行额外分析。
# Use the results in your own workflow
# Export specific model based on the ranked models and the analysis (model_id, sample_id, run_id)
sklearn_model = robustTopics.export_model(1,0,0)
# Look at the full reports inclusing separate values for each initialization
robustTopics.models[1].report_full
# Convert the full report to a pandas dataframe for further use or export
import pandas as pd
report = pd.DataFrame.from_records(robustTopics.models[model_id].report) # or report_full
还支持 Gensim 主题模型。以下是如何设置简单管道的示例。分析步骤与上面完全相同。
from gensim.models import LdaModel, nmf, ldamulticore
from gensim.utils import simple_preprocess
from gensim import corpora
from robics import RobustTopics
def docs_to_words(docs):
for doc in docs:
yield(simple_preprocess(str(doc), deacc=True))
tokenized_data = list(docs_to_words(documents))
dictionary = corpora.Dictionary(tokenized_data)
corpus = [dictionary.doc2bow(text) for text in tokenized_data]
# TOPIC MODELLING
robustTopics = RobustTopics(nlp)
# Load 4 different models
robustTopics.load_gensim_model(
ldamulticore.LdaModel, corpus, dictionary, dimension_range=[5, 50], n_samples=4, n_initializations=3)
robustTopics.load_gensim_model(
nmf.Nmf, corpus, dictionary, dimension_range=[5, 50], n_samples=4, n_initializations=3)
robustTopics.fit_models()
# Same analysis steps as in the sklearn example
下一步
- 可视化界面
- 如果需要,添加对更多模型的支持。
- 添加日志记录
- 编写单元测试。
- 提高整体性能。
- 实施本文中的 Cv 连贯性度量
贡献
我很高兴在上述任何事情或其他有趣的功能请求方面获得帮助。