LDA topic modeling

This example trains a latent Dirichlet allocation model with scikit-learn using abstracts from Neurosynth.

import os

import pandas as pd

from nimare import annotate
from nimare.dataset import Dataset
from nimare.utils import get_resource_path

Load dataset with abstracts

dset = Dataset(os.path.join(get_resource_path(), "neurosynth_laird_studies.json"))

Initialize LDA model

model = annotate.lda.LDAModel(n_topics=5, max_iter=1000, text_column="abstract")

Run model

new_dset = model.fit(dset)

Out:

/home/docs/checkouts/readthedocs.org/user_builds/nimare/envs/0.0.12rc1/lib/python3.7/site-packages/sklearn/utils/deprecation.py:87: FutureWarning: Function get_feature_names is deprecated; get_feature_names is deprecated in 1.0 and will be removed in 1.2. Please use get_feature_names_out instead.
  warnings.warn(msg, category=FutureWarning)

View results

This DataFrame is very large, so we will only show a slice of it.

id study_id contrast_id Neurosynth_TFIDF__001 Neurosynth_TFIDF__01 Neurosynth_TFIDF__05 Neurosynth_TFIDF__10 Neurosynth_TFIDF__100 Neurosynth_TFIDF__11 Neurosynth_TFIDF__12
0 17029760-1 17029760 1 0.0 0.0 0.0 0.000000 0.0 0.000000 0.0
1 18760263-1 18760263 1 0.0 0.0 0.0 0.000000 0.0 0.000000 0.0
2 19162389-1 19162389 1 0.0 0.0 0.0 0.000000 0.0 0.176321 0.0
3 19603407-1 19603407 1 0.0 0.0 0.0 0.000000 0.0 0.000000 0.0
4 20197097-1 20197097 1 0.0 0.0 0.0 0.000000 0.0 0.000000 0.0
5 22569543-1 22569543 1 0.0 0.0 0.0 0.000000 0.0 0.000000 0.0
6 22659444-1 22659444 1 0.0 0.0 0.0 0.000000 0.0 0.000000 0.0
7 23042731-1 23042731 1 0.0 0.0 0.0 0.000000 0.0 0.000000 0.0
8 23702412-1 23702412 1 0.0 0.0 0.0 0.061006 0.0 0.000000 0.0
9 24681401-1 24681401 1 0.0 0.0 0.0 0.000000 0.0 0.000000 0.0


Given that this DataFrame is very wide (many terms), we will transpose it before presenting it.

model.distributions_["p_topic_g_word_df"].T.head(10)
LDA5__1 LDA5__2 LDA5__3 LDA5__4 LDA5__5
10 0.001000 2.001000 0.001 0.001000 0.001000
abstract 1.000108 0.001000 0.001 0.001000 1.001892
action 1.000776 1.001224 0.001 0.001000 0.001000
active 4.001000 0.001000 0.001 0.001000 0.001000
addition 2.284153 1.201311 0.001 1.517536 0.001000
additionally 2.001000 0.001000 0.001 0.001000 0.001000
affective 3.192516 1.809065 0.001 0.001000 1.001420
affective processes 2.001000 0.001000 0.001 0.001000 0.001000
ale 0.001000 0.001000 0.001 2.001000 0.001000
altered 4.001000 0.001000 0.001 0.001000 0.001000


LDA5__1 LDA5__2 LDA5__3 LDA5__4 LDA5__5
Token
0 connectivity cortex functional literature social
1 functional human connectivity talairach motor
2 functional connectivity lateral anterior error functional
3 macm prefrontal networks coordinate task
4 anterior frontal indicate suggest cortex
5 cognitive cognition altered indicated network
6 structural identified functional networks quantitative reflecting
7 analytic functional structural using systems
8 modeling prefrontal cortex function published compare
9 posterior maps connectivity modeling estimation systematic


Total running time of the script: ( 0 minutes 3.371 seconds)

Gallery generated by Sphinx-Gallery