Topeax
Topeax is a probabilistic topic model based on the Peax clustering model, which finds topics based on peaks in point density in the embedding space. The model can recover the number of topics automatically.
In the following example I run a Topeax model on the BBC News corpus, and plot the steps of the algorithm to inspect how our documents have been clustered and why:
# pip install datasets, plotly
from datasets import load_dataset
from turftopic import Topeax
ds = load_dataset("gopalkalpande/bbc-news-summary", split="train")
topeax = Topeax(random_state=42)
doc_topic = topeax.fit_transform(list(ds["Summaries"]))
fig = topeax.plot_steps(hover_text=[text[:200] for text in corpus])
fig.show()
topeax.print_topics()
| Topic ID | Highest Ranking |
|---|---|
| 0 | mobile, microsoft, digital, technology, broadband, phones, devices, internet, mobiles, computer |
| 1 | economy, growth, economic, deficit, prices, gdp, inflation, currency, rates, exports |
| 2 | profits, shareholders, shares, takeover, shareholder, company, profit, merger, investors, financial |
| 3 | film, actor, oscar, films, actress, oscars, bafta, movie, awards, actors |
| 4 | band, album, song, singer, concert, rock, songs, rapper, rap, grammy |
| 5 | tory, blair, labour, ukip, mps, minister, election, tories, mr, ministers |
| 6 | olympic, tennis, iaaf, federer, wimbledon, doping, roddick, champion, athletics, olympics |
| 7 | rugby, liverpool, england, mourinho, chelsea, premiership, arsenal, gerrard, hodgson, gareth |
How does Topeax work?
The Topeax algorithm, similar to clustering topic models consists of two consecutive steps. One of them discovers the underlying clusters in the data, the other one estimates term importance scores for each topic in the corpus.
1. Clustering
Documents embeddings first get projected into two-dimensional space using t-SNE. In order to identify clusters, we first calculate a Kernel Density Estimate over the embedding space, then find local maxima in the KDE by grid approximation. When we discover local maxima (peaks), we assume these to be cluster means. Cluster density is then approximated with a Gaussian Mixture, where we fix means to the density peaks and then use expectation-maximization to fit the rest of the parameters. (see Figure 2) Documents are then assigned to the component with the highest responsibility:
where \(z_d\) is the cluster label for document \(d\), \(r_{kd}\) is the responsibility of component \(k\) for document \(d\) and \(\hat{x}_d\) is the 2D embedding of document \(d\).
2. Term Importance Estimation
Topeax uses a combined semantic-lexical term importance, which is the geometric mean of the NPMI method (see Clustering Topic Models for more detail) and a slightly modified centroid-based method. The modified centroids are calculated like so:
where \(t_k\) is the embedding of topic \(k\) and \(x_d\) is the embedding of document \(d\).
Visualization
Topeax has a number of plots available that can aid you when interpreting your results:
Density Plots
One can plot the kernel density estimate on both a 2D and a 3D plot.
topeax.plot_density()
topeax.plot_density3d()
Component Plots
You can also create a plot over the mixture components/clusters found by the model.
topeax.plot_components()
You can also create a datamapplot figure similar to clustering models:
# pip install turftopic[datamapplot]
topeax.plot_components_datamapplot()
API Reference
turftopic.models.topeax.Topeax
Bases: GMM
Topic model based on the Peax clustering algorithm. The algorithm discovers the number of topics automatically, and is based on GMM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoder |
Union[Encoder, str, MultimodalEncoder]
|
Model to encode documents/terms, all-MiniLM-L6-v2 is the default. |
'sentence-transformers/all-MiniLM-L6-v2'
|
vectorizer |
Optional[CountVectorizer]
|
Vectorizer used for term extraction. Can be used to prune or filter the vocabulary. |
None
|
perplexity |
int
|
Number of neighbours to take into account when running TSNE. |
50
|
random_state |
Optional[int]
|
Random state to use so that results are exactly reproducible. |
None
|
Source code in turftopic/models/topeax.py
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 | |
turftopic.models.topeax.Peax
Bases: ClusterMixin, BaseEstimator
Clustering model based on density peaks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
random_state |
Optional[int]
|
Random seed to use for fitting gaussian mixture to peaks. |
None
|
Source code in turftopic/models/topeax.py
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 | |