Topic Model¤
The topic_model
module is used to train and visualize topic models. Currently, it works MALLET, which must be installed separately, to train models and generates visualizations with dfr-browser.
lexos.topic_model.mallet.Mallet
¤
A wrapper for the MALLET command line tool.
Source code in lexos\topic_model\mallet\__init__.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
|
__init__(model_dir, mallet_path='mallet')
¤
Initialize the MALLET object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_dir |
str
|
The directory to store the model. |
required |
mallet_path |
str
|
The path to the MALLET binary. |
'mallet'
|
Source code in lexos\topic_model\mallet\__init__.py
19 20 21 22 23 24 25 26 27 |
|
import_data(docs, allowed=None, remove_stops=True, remove_punct=True, use_lemmas=False, **kwargs)
¤
Import data into MALLET.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docs |
List[object]
|
A list of spaCy documents. |
required |
allowed |
List[str]
|
A list of POS tags that are allowed. |
None
|
remove_stops |
bool
|
Whether to remove stop words. |
True
|
remove_punct |
bool
|
Whether to remove punctuation. |
True
|
use_lemmas |
bool
|
Whether to replace tokens with lemmas. |
False
|
Notes
Creates a file containing one doc per line with each doc consisting of space-separated terms repeated however many times they occurred in the source doc. This file is then over-written by the MALLET import-file command, potentially using any MALLET command flags that are passed in (although most of the work is done by the first step in the process).
Source code in lexos\topic_model\mallet\__init__.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
scale(model_state_file=None, output_file=None)
¤
Scale a model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_state_file |
str
|
The path to a state_file. |
None
|
output_file |
str
|
The path to an output file. |
None
|
Source code in lexos\topic_model\mallet\__init__.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
|
train(mallet_file=None, num_topics=20, num_iterations=1000, optimize_interval=10, random_seed=None, **kwargs)
¤
Train a model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_topics |
int
|
The number of topics to train. |
20
|
num_iterations |
int
|
The number of iterations to train. |
1000
|
optimize_interval |
int
|
The number of iterations between optimization. |
10
|
random_seed |
int
|
The random seed to use. |
None
|
Source code in lexos\topic_model\mallet\__init__.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
|
lexos.topic_model.mallet.scale_model.__num_dist_rows__(array, ndigits=2)
¤
Check that all rows in a matrix sum to 1.
Source code in lexos\topic_model\mallet\scale_model.py
22 23 24 |
|
lexos.topic_model.mallet.scale_model.ValidationError
¤
Bases: ValueError
Handle validation errors.
Source code in lexos\topic_model\mallet\scale_model.py
27 28 29 30 |
|
lexos.topic_model.mallet.scale_model._input_check(topic_term_dists, doc_topic_dists, doc_lengths, vocab, term_frequency)
¤
Check input for scale_model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
topic_term_dists |
pd.DataFrame
|
Matrix of topic-term probabilities. |
required |
doc_topic_dists |
pd.DataFrame
|
Matrix of document-topic probabilities. |
required |
doc_lengths |
list
|
List of document lengths. |
required |
vocab |
list
|
List of vocabulary. |
required |
term_frequency |
int
|
Minimum number of times a term must appear in a document. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
List of errors. |
Source code in lexos\topic_model\mallet\scale_model.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
lexos.topic_model.mallet.scale_model._input_validate(*args)
¤
Check input for scale_model.
Source code in lexos\topic_model\mallet\scale_model.py
90 91 92 93 94 |
|
lexos.topic_model.mallet.scale_model._jensen_shannon(_P, _Q)
¤
Calculate Jensen-Shannon Divergence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
_P |
np.array
|
Probability distribution. |
required |
_Q |
np.array
|
Probability distribution. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
Jensen-Shannon Divergence. |
Source code in lexos\topic_model\mallet\scale_model.py
97 98 99 100 101 102 103 104 105 106 107 108 |
|
lexos.topic_model.mallet.scale_model._pcoa(pair_dists, n_components=2)
¤
Perform Principal Coordinate Analysis.
AKA Classical Multidimensional Scaling Code referenced from skbio.stats.ordination.pcoa
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pair_dists |
np.array
|
Pairwise distances. |
required |
n_components |
int
|
Number of dimensions to reduce to. |
2
|
Returns:
Type | Description |
---|---|
np.array
|
np.array: PCoA matrix. |
Source code in lexos\topic_model\mallet\scale_model.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
lexos.topic_model.mallet.scale_model.js_PCoA(distributions)
¤
Perform dimension reduction.
Works via Jensen-Shannon Divergence & Principal Coordinate Analysis (aka Classical Multidimensional Scaling)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distributions |
np.array
|
(array-like, shape ( |
required |
Returns:
Name | Type | Description |
---|---|---|
pcoa |
np.array
|
(array, shape ( |
Source code in lexos\topic_model\mallet\scale_model.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
|
lexos.topic_model.mallet.scale_model.js_MMDS(distributions, **kwargs)
¤
Perform dimension reduction.
Works via Jensen-Shannon Divergence & Metric Multidimensional Scaling
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distributions |
np.array
|
Matrix of distributions probabilities (array-like, shape ( |
required |
**kwargs |
dict
|
Keyword argument to be passed to |
{}
|
Returns:
Name | Type | Description |
---|---|---|
mmds |
np.array
|
(array, shape ( |
Source code in lexos\topic_model\mallet\scale_model.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
lexos.topic_model.mallet.scale_model.js_TSNE(distributions, **kwargs)
¤
Perform dimension reduction.
Works via Jensen-Shannon Divergence & t-distributed Stochastic Neighbor Embedding
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distributions |
np.array
|
Matrix of distributions probabilities (array-like, shape ( |
required |
**kwargs |
dict
|
Keyword argument to be passed to |
{}
|
Returns:
Name | Type | Description |
---|---|---|
tsne |
np.array
|
(array, shape ( |
Source code in lexos\topic_model\mallet\scale_model.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
lexos.topic_model.mallet.scale_model._df_with_names(data, index_name, columns_name)
¤
Get a dataframe with names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
pd.DataFrame
|
Dataframe. |
required |
index_name |
str
|
Name of index. |
required |
columns_name |
str
|
Name of columns. |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pd.DataFrame: Dataframe with names. |
Source code in lexos\topic_model\mallet\scale_model.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
|
lexos.topic_model.mallet.scale_model._series_with_name(data, name)
¤
Get a series with name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
pd.Series
|
Series. |
required |
name |
str
|
Name of series. |
required |
Returns:
Type | Description |
---|---|
pd.Series
|
pd.Series: Series with name. |
Source code in lexos\topic_model\mallet\scale_model.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
|
lexos.topic_model.mallet.scale_model._topic_coordinates(mds, topic_term_dists, topic_proportion)
¤
Get coordinates for topics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mds |
array, shape (`n_dists`, 2
|
MDS coordinates. |
required |
topic_term_dists |
array, shape (`n_topics`, `n_terms`
|
Topic-term distributions. |
required |
topic_proportion |
array, shape (`n_topics`
|
Topic proportions. |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pd.DataFrame: Topic coordinates. |
Source code in lexos\topic_model\mallet\scale_model.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
|
lexos.topic_model.mallet.scale_model.get_topic_coordinates(topic_term_dists, doc_topic_dists, doc_lengths, vocab, term_frequency, mds=js_PCoA, sort_topics=True)
¤
Transform the topic model distributions and related corpus.
Creates the data structures needed for topic bubbles.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
topic_term_dists |
array-like, shape (`n_topics`, `n_terms`
|
Matrix of topic-term probabilities where
|
required |
doc_topic_dists |
array-like, shape (`n_docs`, `n_topics`
|
Matrix of document-topic probabilities. |
required |
doc_lengths |
(array-like, shape |
required | |
vocab |
array-like, shape `n_terms`
|
List of all the words in the corpus used to train the model. |
required |
term_frequency |
array-like, shape `n_terms`
|
The count of each particular term over the entire corpus.
The ordering of these counts should correspond with |
required |
mds |
Callable
|
A function that takes |
js_PCoA
|
sort_topics |
bool
|
Whether to sort topics by topic proportion (percentage of tokens covered). Set to
|
True
|
Returns:
Name | Type | Description |
---|---|---|
scaled_coordinates |
pd.DataFrame
|
A pandas dataframe containing scaled x and y coordinates. |
Source code in lexos\topic_model\mallet\scale_model.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
|
lexos.topic_model.mallet.scale_model.extract_params(statefile)
¤
Extract the alpha and beta values from the statefile.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
statefile |
str
|
Path to statefile produced by MALLET. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
tuple
|
A tuple of (alpha (list), beta) |
Source code in lexos\topic_model\mallet\scale_model.py
344 345 346 347 348 349 350 351 352 353 354 355 |
|
lexos.topic_model.mallet.scale_model.state_to_df(statefile)
¤
Transform state file into pandas dataframe.
The MALLET statefile is tab-separated, and the first two rows contain the alpha and beta hypterparamters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
statefile |
str
|
Path to statefile produced by MALLET. |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pd.DataFrame: The topic assignment for each token in each document of the model. |
Source code in lexos\topic_model\mallet\scale_model.py
358 359 360 361 362 363 364 365 366 367 368 369 |
|
lexos.topic_model.mallet.scale_model.pivot_and_smooth(df, smooth_value, rows_variable, cols_variable, values_variable)
¤
Turn the pandas dataframe into a data matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
pd.DataFrame
|
The aggregated dataframe. |
required |
smooth_value |
float
|
Value to add to the matrix to account for the priors. |
required |
rows_variable |
str
|
The name of the dataframe column to use as the rows in the matrix. |
required |
cols_variable |
str
|
The name of the dataframe column to use as the columns in the matrix. |
required |
values_variable |
str
|
The name of the dataframe column to use as the values in the matrix. |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pd.DataFrame: A pandas matrix that has been normalized on the rows. |
Source code in lexos\topic_model\mallet\scale_model.py
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
|
lexos.topic_model.mallet.scale_model.convert_mallet_data(state_file)
¤
Convert Mallet data to a structure compatible with pyLDAvis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state_file |
string
|
Mallet state file |
required |
Returns:
Name | Type | Description |
---|---|---|
data |
dict
|
A dict containing pandas dataframes for the pyLDAvis prepare method. |
Source code in lexos\topic_model\mallet\scale_model.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
|
lexos.topic_model.dfr_browser.DfrBrowser
¤
DfrBrowser class.
Source code in lexos\topic_model\dfr_browser\__init__.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
|
__init__(model_dir='.', model_state_file='state.gz', model_scaled_file='topic_scaled.csv', template_dir=TEMPLATE_DIR)
¤
Initialize DfrBrowser object.
Source code in lexos\topic_model\dfr_browser\__init__.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
run(port=8080)
¤
Run the dfr-browser.
This might work on the Jupyter port, but it might not.
Source code in lexos\topic_model\dfr_browser\__init__.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
|