Skip to content

Commit c33c41c

Browse files
authored
Merge pull request #82 from carpentries-incubator/fix/topic_modelling
Limit sentences for topic modelling; update outputs accordingly.
2 parents 06e82ed + c4cac95 commit c33c41c

2 files changed

Lines changed: 31 additions & 25 deletions

File tree

episodes/03-word_representations.md

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,10 @@ Before the first training run, it downloads the language model; this and the tra
648648
```python
649649
from bertopic import BERTopic
650650

651+
N_SENTENCES=100000
652+
651653
topic_model = BERTopic(nr_topics=100)
652-
topics, probs = topic_model.fit_transform(sentences)
654+
topics, probs = topic_model.fit_transform(sentences[:N_SENTENCES])
653655
```
654656

655657
```output
@@ -673,18 +675,17 @@ topic_model.get_topic_info()
673675
```
674676

675677
```output
676-
Topic Count Name Representation Representative_Docs
677-
0 -1 21788 -1_eyes_little_time_man [eyes, little, time, man, like, face, life, ro... [good man true seen better man, young man mome...
678-
1 0 2968 0_mary_gloria_edna_miss [mary, gloria, edna, miss, madame, gostrey, ma... [gloria, mary, said mary]
679-
2 1 2124 1_dorian_henry_colin_ralph [dorian, henry, colin, ralph, lord, denham, ro... [lord henry looked table, know answered lord h...
680-
3 2 2077 2_yes_oh_ah_eh [yes, oh, ah, eh, glad, right, better, god, su... [oh yes, oh yes, oh yes]
681-
4 3 1625 3_katharine_carrie_cassandra_said [katharine, carrie, cassandra, said, william, ... [think katharine, katharine, katharine]
682-
... ... ... ... ... ...
683-
95 94 16 94_isabella_prison_quadroon_sold [isabella, prison, quadroon, sold, previous, u... [immediately took daughter aside informed prev...
684-
96 95 12 95_foreman_workshop_ogre_motioning [foreman, workshop, ogre, motioning, gait, bar... [head foreman round stopped machine, foreman, ...
685-
97 96 12 96_ames_superiority_funniest_thorne [ames, superiority, funniest, thorne, dora, sa... [going asked ames, said ames, ames]
686-
98 97 11 97_clatter_bang_scatter_crash [clatter, bang, scatter, crash, lads, dogs, fi... [clatter clatter clatter bang, clatter clatter...
687-
99 98 11 98_entanglement_ensue_faithlessness_repugnant [entanglement, ensue, faithlessness, repugnant... [entanglement carrie anxious change sort, carr...
678+
0 -1 34182 -1_little_like_eyes_face [little, like, eyes, face, time, man, day, roo... [said thought day, rate little gentleman early...
679+
1 0 10433 0_oh_yes_know_asked [oh, yes, know, asked, mean, sir, come, ah, te... [oh, oh, oh come]
680+
2 1 4930 1_helen_gloria_miss_mary [helen, gloria, miss, mary, edna, jane, martha... [helen, helen, helen]
681+
3 2 2471 2_hurstwood_drouet_rochester_boldwood [hurstwood, drouet, rochester, boldwood, bilha... [said hurstwood, said hurstwood, said hurstwood]
682+
4 3 2411 3_heart_exclaimed_shook_cried [heart, exclaimed, shook, cried, tears, laugh,... [shook head, shook head, laughed shook head]
683+
... ... ... ... ... ...
684+
95 94 11 94_isle_grand_summer_drove [isle, grand, summer, drove, damsel, vacation,... [let mind wander stay grand isle tried discove...
685+
96 95 11 95_clatter_bang_scatter_lads [clatter, bang, scatter, lads, crash, find, sh... [clatter clatter clatter bang, clatter clatter...
686+
97 96 11 96_desert_desertion_deserting_tolerance [desert, desertion, deserting, tolerance, mise... [found safety desert, like desert, desert]
687+
98 97 10 97_briggs_solicitor_letter_vested [briggs, solicitor, letter, vested, urgency, l... [briggs solicitor street london, briggs london...
688+
99 98 10 98_proportion_likes_proportions_theirs [proportion, likes, proportions, theirs, large... [proportion shifting trouble likes, proportion...
688689
```
689690

690691
Because the topic modelling algorithm has no information about the texts or topics to start with, it randomly assigns words to topics initially.
@@ -720,13 +721,13 @@ topic_model.visualize_hierarchy()
720721
To find topics that are associated to a particular term, use the `.find_topics()` method:
721722

722723
```python
723-
topics, probs = topic_model.find_topics("sea")
724-
print(topics)
724+
sea_topics, probs = topic_model.find_topics("sea")
725+
print(sea_topics)
725726
```
726727

727728
Visualize the terms of the topics found:
728729
```python
729-
topic_model.visualize_barchart(topics)
730+
topic_model.visualize_barchart(sea_topics)
730731
```
731732

732733
![](fig/02-topics-sea.png)
@@ -735,17 +736,18 @@ During the model training, all texts in the data have been assigned to a topic d
735736
You can also use the model to infer the topics of previously unseen texts based on the same model:
736737

737738
```python
738-
topics, probs = topic_model.transform(["A short text about scientific discoveries"])
739+
topics, probs = topic_model.transform(["a short text about water"])
739740
print(topics, probs)
740741
```
741742

742743
```output
743-
[31] [0.47929027]
744+
[20] [1.]
744745
```
745746

746-
This example was assigned to topic 31, with a probability of 0.479.
747-
Again, these numbers will be slightly different per run.
748-
The model can even assign multiple topics to a text, the `topics` and `probs` lists contain the according number of values.
747+
This example was assigned to topic 20, with a score of 1.0.
748+
Again, these numbers can vary per run.
749+
For less trivial sentences, the model can assign multiple topics to a text.
750+
In that case, the `topics` and `probs` lists contain the according number of values.
749751

750752
::: challenge
751753

@@ -758,11 +760,15 @@ The model can even assign multiple topics to a text, the `topics` and `probs` li
758760
::: solution
759761

760762
1. There is no general 'true' label for a topic, but looking at the top word of a topic should give an idea of the concept(s) it refers to, for instance:
761-
`['mary', 'gloria', 'edna', 'miss', 'madame']` -> 'female names and titles'
762-
`['mean', 'oh', 'yes', 'ah', 'eh']` -> 'acknowledging reactions''
763-
`['know', 'tell', 'said', 'told']` -> 'telling'
763+
`[helen, gloria, miss, mary, edna, jane, martha, ...` -> 'female names and titles'
764+
`[hurstwood, drouet, rochester, boldwood, ...` -> 'family names'
765+
`[heart, exclaimed, shook, cried, tears, laugh, ...` -> 'emotions'
766+
767+
2.
764768

765-
2. TODO
769+
- Omitting the number of topics (`nr_topics`) results in an 'optimal' number, according to the clustering algorithm.
770+
- Limiting the number of topics forces the algorithm to merge similar topics.
771+
- Minimum topic sizes prevents the algorithm to form clusters of very few documents.
766772

767773
3. Evaluation methods:
768774

-1.06 KB
Loading

0 commit comments

Comments
 (0)