Skip to main content

双项主题模型

项目描述

双项主题模型

圈子CI 文件状态 Codacy 徽章 问题 下载 下载 派皮

Bitermplus为闫晓辉、郭家峰、蓝燕燕和程雪琪介绍的短文实现了 Biterm 主题模型。实际上,它是BTM的 cythonized 版本。该软件包还能够计算困惑度和语义连贯性指标。

发展

请注意,bitermplus 正在积极改进。请参阅文档以保持最新状态。

要求

  • 赛通
  • 麻木的
  • 熊猫
  • scipy
  • scikit-学习
  • tqdm

设置

Linux 和 Windows

在这些操作系统下安装bitermplus应该没有问题。您可以直接从 PyPi 安装该软件包。

pip install bitermplus

或者从这个回购:

pip install git+https://github.com/maximtrp/bitermplus.git

苹果系统

首先,您需要安装 XCode CLT 和Homebrew。然后,libomp使用安装brew

xcode-select --install
brew install libomp
pip3 install bitermplus

例子

模型拟合

import bitermplus as btm
import numpy as np
import pandas as pd

# IMPORTING DATA
df = pd.read_csv(
    'dataset/SearchSnippets.txt.gz', header=None, names=['texts'])
texts = df['texts'].str.strip().tolist()

# PREPROCESSING
# Obtaining terms frequency in a sparse matrix and corpus vocabulary
X, vocabulary, vocab_dict = btm.get_words_freqs(texts)
tf = np.array(X.sum(axis=0)).ravel()
# Vectorizing documents
docs_vec = btm.get_vectorized_docs(texts, vocabulary)
docs_lens = list(map(len, docs_vec))
# Generating biterms
biterms = btm.get_biterms(docs_vec)

# INITIALIZING AND RUNNING MODEL
model = btm.BTM(
    X, vocabulary, seed=12321, T=8, M=20, alpha=50/8, beta=0.01)
model.fit(biterms, iterations=20)
p_zd = model.transform(docs_vec)

# METRICS
perplexity = btm.perplexity(model.matrix_topics_words_, p_zd, X, 8)
coherence = btm.coherence(model.matrix_topics_words_, X, M=20)
# or
perplexity = model.perplexity_
coherence = model.coherence_

# LABELS
model.labels_
# or
btm.get_docs_top_topic(texts, model.matrix_docs_topics_)

结果可视化

您需要先安装tmplot

import tmplot as tmp
tmp.report(model=model, docs=texts)

报告界面

教程

文档中有一个教程 ,涵盖了主题建模的重要步骤(包括稳定性测量和结果可视化)。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

bitermplus-0.6.12.tar.gz (615.1 kB 查看哈希

已上传 source