Skip to main content

Biterm Topic Model

Project description

Biterm Topic Model

CircleCI Documentation Status Codacy Badge Downloads PyPI

Bitermplus implements Biterm topic model for short texts introduced by Xiaohui Yan, Jiafeng Guo, Yanyan Lan, and Xueqi Cheng. Actually, it is a cythonized version of BTM. This package is also capable of computing perplexity and semantic coherence metrics.

Development

Please note that bitermplus is actively improved. Refer to documentation to stay up to date.

Requirements

  • Cython
  • NumPy
  • Pandas
  • SciPy
  • Scikit-learn
  • pyLDAvis (optional)

Setup

Linux and Windows

There should be no issues with installing bitermplus under these OSes. You can install the package directly from PyPi.

pip install bitermplus

Or from this repo:

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

Mac OS

First, you need to install XCode CLT and Homebrew. Then, install libomp using brew:

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

Example

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

# 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, W=vocabulary.size, 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_

# RESULTS VISUALIZATION
# Turning on displaying in Jupyter notebook
plv.enable_notebook()
# Preparing our results for visualization
vis = btm.vis_prepare_model(
    model.matrix_topics_words_,
    p_zd,
    docs_lens,
    model.vocabulary_,
    tf
)
# Displaying the results
plv.display(vis)

Tutorial

There is a tutorial in documentation that covers the important steps of topic modeling (including stability measures and results visualization).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bitermplus-0.6.4.tar.gz (608.9 kB view details)

Uploaded Source

File details

Details for the file bitermplus-0.6.4.tar.gz.

File metadata

  • Download URL: bitermplus-0.6.4.tar.gz
  • Upload date:
  • Size: 608.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for bitermplus-0.6.4.tar.gz
Algorithm Hash digest
SHA256 0387e10197a393f672142d83ac05704231c81cb1ad423635eb5f9f48ad5d1e3f
MD5 1873104f95beffb2b69d90be953a094d
BLAKE2b-256 c2094cb01b366f72177cc2d158fc89931ca0c0d03144e8d2b1ce3608b13d17f6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page