Dynamic Topic Modeling
If you want to examine the evolution of topics over time, you will need a dynamic topic model.
Note that regular static models can also be used to study the evolution of topics and information dynamics, but they can't capture changes in the topics themselves.
Models
In Turftopic you can currently use three different topic models for modeling topics over time:
- ClusteringTopicModel, where an overall model is fitted on the whole corpus, and then term importances are estimated over time slices.
- GMM, similarly to clustering models, term importances are reestimated per time slice
- KeyNMF, an overall decomposition is done, then using coordinate descent, topic-term-matrices are recalculated based on document-topic importances in the given time slice.
- SemanticSignalSeparation, a global model is fitted and then local models are inferred using linear regression from embeddings and document-topic signals in a given time-slice.
Usage
Dynamic topic models in Turftopic have a unified interface.
To fit a dynamic topic model you will need a corpus, that has been annotated with timestamps.
The timestamps need to be Python datetime
objects, but pandas Timestamp
object are also supported.
Models that have dynamic modeling capabilities (KeyNMF
, GMM
, SemanticSignalSeparation
and ClusteringTopicModel
) have a fit_transform_dynamic()
method, that fits the model on the corpus over time.
from datetime import datetime
from turftopic import KeyNMF
corpus: list[str] = []
timestamps: list[datetime] = []
model = KeyNMF(5, top_n=5, random_state=42)
document_topic_matrix = model.fit_transform_dynamic(
corpus, timestamps=timestamps, bins=10
)
You can use the print_topics_over_time()
method for producing a table of the topics over the generated time slices.
This example uses CNN news data.
model.print_topics_over_time()
Time Slice | 0_olympics_tokyo_athletes_beijing | 1_covid_vaccine_pandemic_coronavirus | 2_olympic_athletes_ioc_athlete | 3_djokovic_novak_tennis_federer | 4_ronaldo_cristiano_messi_manchester |
---|---|---|---|---|---|
2012 12 06 - 2013 11 10 | genocide, yugoslavia, karadzic, facts, cnn | cnn, russia, chechnya, prince, merkel | france, cnn, francois, hollande, bike | tennis, tournament, wimbledon, grass, courts | beckham, soccer, retired, david, learn |
2013 11 10 - 2014 10 14 | keith, stones, richards, musician, author | georgia, russia, conflict, 2008, cnn | civil, rights, hear, why, should | cnn, kidneys, traffickers, organ, nepal | ronaldo, cristiano, goalscorer, soccer, player |
2014 10 14 - 2015 09 18 | ethiopia, brew, coffee, birthplace, anderson | climate, sutter, countries, snapchat, injustice | women, guatemala, murder, country, worst | cnn, climate, oklahoma, women, topics | sweden, parental, dads, advantage, leave |
2015 09 18 - 2016 08 22 | snow, ice, winter, storm, pets | climate, crisis, drought, outbreaks, syrian | women, vulnerabilities, frontlines, countries, marcelas | cnn, warming, climate, sutter, theresa | sutter, band, paris, fans, crowd |
2016 08 22 - 2017 07 26 | derby, epsom, sporting, race, spectacle | overdoses, heroin, deaths, macron, emmanuel | fear, died, indigenous, people, arthur | siblings, amnesia, palombo, racial, mh370 | bobbi, measles, raped, camp, rape |
2017 07 26 - 2018 06 30 | her, percussionist, drums, she, deported | novichok, hurricane, hospital, deaths, breathing | women, day, celebrate, taliban, international | abuse, harassment, cnn, women, pilgrimage | maradona, argentina, history, jadon, rape |
2018 06 30 - 2019 06 03 | athletes, teammates, celtics, white, racism | pope, archbishop, francis, vigano, resignation | racism, athletes, teammates, celtics, white | golf, iceland, volcanoes, atlantic, ocean | rape, sudanese, racist, women, soldiers |
2019 06 03 - 2020 05 07 | esports, climate, ice, racers, culver | esports, coronavirus, pandemic, football, teams | racers, women, compete, zone, bery | serena, stadium, sasha, final, naomi | kobe, bryant, greatest, basketball, influence |
2020 05 07 - 2021 04 10 | olympics, beijing, xinjiang, ioc, boycott | covid, vaccine, coronavirus, pandemic, vaccination | olympic, japan, medalist, canceled, tokyo | djokovic, novak, tennis, federer, masterclass | ronaldo, cristiano, messi, juventus, barcelona |
2021 04 10 - 2022 03 16 | olympics, tokyo, athletes, beijing, medal | covid, pandemic, vaccine, vaccinated, coronavirus | olympic, athletes, ioc, medal, athlete | djokovic, novak, tennis, wimbledon, federer | ronaldo, cristiano, messi, manchester, scored |
You can also display the topics over time on an interactive HTML figure. The most important words for topics get revealed by hovering over them.
You will need to install Plotly for this to work.
pip install plotly
model.plot_topics_over_time()
API reference
All dynamic topic models have a temporal_components_
attribute, which contains the topic-term matrices for each time slice, along with a temporal_importance_
attribute, which contains the importance of each topic in each time slice.
turftopic.dynamic.DynamicTopicModel
Bases: ABC
Source code in turftopic/dynamic.py
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 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 268 269 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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
|
bin_timestamps(timestamps, bins=10)
staticmethod
Bins timestamps based on given bins.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timestamps
|
list[datetime]
|
List of timestamps for documents. |
required |
bins
|
Union[int, list[datetime]]
|
Time bins to use. If the bins are an int (N), N equally sized bins are used. Otherwise they should be bin edges, including the last and first edge. Bins are inclusive at the lower end and exclusive at the upper (lower <= timestamp < upper). |
10
|
Returns:
Name | Type | Description |
---|---|---|
time_labels |
ndarray of int
|
Labels for time slice in each document. |
bin_edges |
list[datetime]
|
List of edges for time bins. |
Source code in turftopic/dynamic.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
export_topics_over_time(top_k=5, show_scores=False, date_format='%Y %m %d', format='csv')
Pretty prints topics in the model in a table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
top_k
|
int
|
Number of top words to return for each topic. |
5
|
show_scores
|
bool
|
Indicates whether to show importance scores for each word. |
False
|
format
|
Specifies which format should be used. 'csv', 'latex' and 'markdown' are supported. |
'csv'
|
Source code in turftopic/dynamic.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
|
fit_dynamic(raw_documents, timestamps, embeddings=None, bins=10)
Fits a dynamic topic model on the corpus and returns document-topic-importances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raw_documents
|
Documents to fit the model on. |
required | |
timestamps
|
list[datetime]
|
Timestamp for each document in |
required |
embeddings
|
Optional[ndarray]
|
Document embeddings produced by an embedding model. |
None
|
bins
|
Union[int, list[datetime]]
|
Specifies how to bin timestamps in to time slices.
When an Note: The final edge is not included. You might want to add one day to the last bin edge if it equals the last timestamp. |
10
|
Source code in turftopic/dynamic.py
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 |
|
fit_transform_dynamic(raw_documents, timestamps, embeddings=None, bins=10)
abstractmethod
Fits a dynamic topic model on the corpus and returns document-topic-importances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raw_documents
|
Documents to fit the model on. |
required | |
timestamps
|
list[datetime]
|
Timestamp for each document in |
required |
embeddings
|
Optional[ndarray]
|
Document embeddings produced by an embedding model. |
None
|
bins
|
Union[int, list[datetime]]
|
Specifies how to bin timestamps in to time slices.
When an |
10
|
Returns:
Type | Description |
---|---|
ndarray of shape (n_documents, n_topics)
|
Document-topic importance matrix. |
Source code in turftopic/dynamic.py
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 |
|
get_time_slices()
Returns starting and ending datetime of each timeslice in the model.
Source code in turftopic/dynamic.py
128 129 130 131 132 133 134 135 |
|
get_topics_over_time(top_k=10)
Returns high-level topic representations in form of the top K words in each topic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
top_k
|
int
|
Number of top words to return for each topic. |
10
|
Returns:
Type | Description |
---|---|
list[list[tuple]]
|
List of topics over each time slice in the dynamic model. Each time slice is a list of topics. Each topic is a tuple of topic ID and the top k words. Top k words are a list of (word, word_importance) pairs. |
Source code in turftopic/dynamic.py
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 |
|
plot_topics_over_time(top_k=6, color_discrete_sequence=None, color_discrete_map=None)
Displays topics over time in the fitted dynamic model on a dynamic HTML figure.
You will need to
pip install plotly
to use this method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
top_k
|
int
|
Number of top words per topic to display on the figure. |
6
|
color_discrete_sequence
|
Optional[Iterable[str]]
|
Color palette to use in the plot. Example:
|
None
|
color_discrete_map
|
Optional[dict[str, str]]
|
Topic names mapped to the colors that should be associated with them. |
None
|
Returns:
Type | Description |
---|---|
Figure
|
Plotly graph objects Figure, that can be displayed or exported as HTML or static image. |
Source code in turftopic/dynamic.py
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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
|
print_topics_over_time(top_k=5, show_scores=False, date_format='%Y %m %d')
Pretty prints topics in the model in a table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
top_k
|
int
|
Number of top words to return for each topic. |
5
|
show_scores
|
bool
|
Indicates whether to show importance scores for each word. |
False
|
Source code in turftopic/dynamic.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
|