Skip to content

Commit 2e8bd78

Browse files
Merge pull request #118 from x-tabdeveloping/topeax_paper
Topeax paper
2 parents dfd7fac + 848387c commit 2e8bd78

35 files changed

+20860
-18
lines changed

docs/Topeax.md

Lines changed: 116 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,127 @@
11
# Topeax
22

3-
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.
4-
It can recover the number of topics automatically.
3+
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.
4+
5+
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:
6+
7+
```python
8+
# pip install datasets, plotly
9+
from datasets import load_dataset
10+
from turftopic import Topeax
11+
12+
ds = load_dataset("gopalkalpande/bbc-news-summary", split="train")
13+
topeax = Topeax(random_state=42)
14+
doc_topic = topeax.fit_transform(list(ds["Summaries"]))
15+
16+
fig = topeax.plot_steps(hover_text=[text[:200] for text in corpus])
17+
fig.show()
18+
```
19+
20+
<figure>
21+
<iframe src="../images/topeax_steps.html", title="", style="height:620px;width:1050px;padding:0px;border:none;"></iframe>
22+
<figcaption> Figure 1: Steps in a Topeax model fitted on BBC News displayed on an interactive graph. </figcaption>
23+
</figure>
24+
25+
```python
26+
topeax.print_topics()
27+
```
28+
29+
30+
| Topic ID | Highest Ranking |
31+
| - | - |
32+
| 0 | mobile, microsoft, digital, technology, broadband, phones, devices, internet, mobiles, computer |
33+
| 1 | economy, growth, economic, deficit, prices, gdp, inflation, currency, rates, exports |
34+
| 2 | profits, shareholders, shares, takeover, shareholder, company, profit, merger, investors, financial |
35+
| 3 | film, actor, oscar, films, actress, oscars, bafta, movie, awards, actors |
36+
| 4 | band, album, song, singer, concert, rock, songs, rapper, rap, grammy |
37+
| 5 | tory, blair, labour, ukip, mps, minister, election, tories, mr, ministers |
38+
| 6 | olympic, tennis, iaaf, federer, wimbledon, doping, roddick, champion, athletics, olympics |
39+
| 7 | rugby, liverpool, england, mourinho, chelsea, premiership, arsenal, gerrard, hodgson, gareth |
40+
41+
## How does Topeax work?
42+
43+
The Topeax algorithm, similar to clustering topic models consists of two consecutive steps.
44+
One of them discovers the underlying clusters in the data, the other one estimates term importance scores for each topic in the corpus.
545

646
<br>
747
<figure>
848
<img src="../images/peax.png" width="100%" style="margin-left: auto;margin-right: auto;">
9-
<figcaption>Schematic overview of the steps of the Peax clustering algorithm</figcaption>
49+
<figcaption>Figure 2: Schematic overview of the steps of the Peax clustering algorithm</figcaption>
1050
</figure>
1151

12-
:warning: **This part of the documentation is still under construction, as more details and a paper are on their way.** :warning:
52+
### 1. Clustering
53+
54+
55+
Documents embeddings first get projected into two-dimensional space using t-SNE.
56+
In order to identify clusters, we first calculate a Kernel Density Estimate over the embedding space,
57+
then find local maxima in the KDE by grid approximation.
58+
When we discover local maxima (peaks), we assume these to be cluster means.
59+
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)
60+
Documents are then assigned to the component with the highest responsibility:
61+
62+
$$\hat{z_d} = arg max_k (r_{kd}); r_{kd}=p(z_k=1 | \hat{x}_d)$$
63+
64+
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$.
65+
66+
### 2. Term Importance Estimation
67+
68+
Topeax uses a combined semantic-lexical term importance, which is the geometric mean of the NPMI method (see [Clustering Topic Models](clustering.md) for more detail) and a slightly modified centroid-based method.
69+
The modified centroids are calculated like so:
70+
71+
$$t_k = \frac{\sum_d r_{kd} \cdot x_d}{\sum_d r_{kd}}$$
72+
73+
where $t_k$ is the embedding of topic $k$ and $x_d$ is the embedding of document $d$.
74+
75+
## Visualization
76+
77+
Topeax has a number of plots available that can aid you when interpreting your results:
78+
79+
### Density Plots
80+
81+
One can plot the kernel density estimate on both a 2D and a 3D plot.
82+
83+
```python
84+
topeax.plot_density()
85+
```
86+
87+
<figure>
88+
<iframe src="../images/topeax_density.html", title="", style="height:620px;width:1050px;padding:0px;border:none;"></iframe>
89+
<figcaption> Figure 2: Density contour plot of the Topeax model. </figcaption>
90+
</figure>
91+
92+
```python
93+
topeax.plot_density3d()
94+
```
95+
96+
<figure>
97+
<iframe src="../images/topeax_density_3d.html", title="", style="height:620px;width:620px;padding:0px;border:none;"></iframe>
98+
<figcaption> Figure 3: 3D Density Surface of the Topeax model. </figcaption>
99+
</figure>
100+
101+
### Component Plots
102+
103+
You can also create a plot over the mixture components/clusters found by the model.
104+
105+
```python
106+
topeax.plot_components()
107+
```
108+
109+
<figure>
110+
<iframe src="../images/topeax_components.html", title="", style="height:620px;width:1050px;padding:0px;border:none;"></iframe>
111+
<figcaption> Figure 4: Gaussian components estimated for the model. </figcaption>
112+
</figure>
113+
114+
You can also create a datamapplot figure similar to clustering models:
115+
116+
```python
117+
# pip install turftopic[datamapplot]
118+
topeax.plot_components_datamapplot()
119+
```
120+
121+
<figure>
122+
<iframe src="../images/topeax_components_datamapplot.html", title="", style="height:620px;width:1050px;padding:0px;border:none;"></iframe>
123+
<figcaption> Figure 5: Datapoints colored by mixture components on a datamapplot. </figcaption>
124+
</figure>
13125

14126
## API Reference
15127

docs/images/topeax_components.html

Lines changed: 3888 additions & 0 deletions
Large diffs are not rendered by default.

docs/images/topeax_components_datamapplot.html

Lines changed: 676 additions & 0 deletions
Large diffs are not rendered by default.

docs/images/topeax_density.html

Lines changed: 3888 additions & 0 deletions
Large diffs are not rendered by default.

docs/images/topeax_density_3d.html

Lines changed: 3888 additions & 0 deletions
Large diffs are not rendered by default.

docs/images/topeax_steps.html

Lines changed: 3888 additions & 0 deletions
Large diffs are not rendered by default.

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ nav:
2626
- Model Overview: model_overview.md
2727
- Semantic Signal Separation (S³): s3.md
2828
- KeyNMF: KeyNMF.md
29+
- Topeax: Topeax.md
2930
- GMM: GMM.md
3031
- Clustering Models (BERTopic & Top2Vec): clustering.md
3132
- Autoencoding Models (ZeroShotTM & CombinedTM): ctm.md

papers/topeax/Merriweather.ttf

4.39 MB
Binary file not shown.

papers/topeax/figures/.xdp-performance.svg.2025_11_06_21_47_55.0.svg-LKVaas

Lines changed: 112 additions & 0 deletions
Large diffs are not rendered by default.
650 KB
Loading

0 commit comments

Comments
 (0)