Session 11 · Workshop

Workshop: what is the news about?

Topic modelling, the simple way: last week's sentences and embeddings, with the labels hidden.

Data Science and Advanced Programming · UNIL · 2026
Session 11 · Workshop

Two weeks ago: the market line

Apple's daily returns against the market (VOO), one dot per day, and the least-squares line.

  • Beta 1.30: when the market moves 1 %, Apple moves 1.3 % on average.
  • R² 0.58: the market explains 58 % of Apple's moves; the rest is Apple's own news.
  • The market falls 2 % → the line says Apple falls 2.6 %, on average.
Data Science and Advanced Programming · UNIL · 2026
Session 11 · Workshop

Two weeks ago: one typo, a wrong beta

The same data with one wrong price: the decimal point moved on one day.

  • Two crazy returns: −90 % that day, +900 % the next.
  • The beta goes from +1.3 to −0.75, R² from 0.58 to 0.
  • describe() and one picture find it in seconds. Look at the data first.
Data Science and Advanced Programming · UNIL · 2026
Session 11 · Workshop

Two weeks ago: two ways to the same line

  • By hand: b = Σ(dx·dy) / Σ(dx·dx), the line through the means → 1.296.
  • scikit-learn: LinearRegression().fit(X, y) → 1.296. No magic, the same formula.
  • Gradient descent: start at 0, take small steps downhill. After 60 steps → 1.296 again; the error falls fast, then flattens.
Data Science and Advanced Programming · UNIL · 2026
Session 11 · Workshop

Last week in one example

The sentence: EPS dropped to EUR0.2 from EUR0.3. Which sentences are closest?

Bag of words (same words) Embeddings (same meaning)
0.75 · The EPS improved to EUR0.38 from EUR0.27. 0.94 · Earnings per share (EPS) dropped to EUR 0.21 from EUR 0.31.
0.60 · EPS amounted to EUR0.98, up from the loss… 0.92 · EPS grew to 0.04 eur from 0.02 eur.
0.60 · EPS amounted to EUR0.03, up from the loss… 0.90 · Diluted EPS declined to EUR 0.78 from EUR 1.76.

Same words, opposite meaning. The embedding sees the meaning.

Data Science and Advanced Programming · UNIL · 2026
Session 11 · Workshop

Last week: how many labels do you need?

With 200 labelled sentences, the embeddings beat the bag of words trained on all 1,584: the model already knew the language.

Data Science and Advanced Programming · UNIL · 2026
Session 11 · Workshop

Last week: the four models

Logistic regression is best on both. One tree is weak on embeddings: one of 768 numbers means nothing alone.

Data Science and Advanced Programming · UNIL · 2026
Session 11 · Workshop

Last week: which mistakes?

Logistic regression on the embeddings, 680 test sentences. Rows: the truth; columns: the prediction.

  • Negative: 83 of 91 found (recall 0.91).
  • Most mistakes are positive → neutral (20): a mildly good result read as a plain fact.
  • A negative sentence is almost never read as positive (1 of 91); the other way round happens more (13).
Data Science and Advanced Programming · UNIL · 2026
Session 11 · Workshop

Same data, no answers

  • The same 2,264 sentences from financial news, the same 768 numbers per sentence (EmbeddingGemma).
  • Last week we gave the labels and trained a classifier.
  • Today the methods never see the labels. They group the sentences into topics, and we name the topics ourselves.
  • Only at the very end we look at the labels: which topics are bad news?
Data Science and Advanced Programming · UNIL · 2026
Session 11 · Workshop

Agglomerative clustering

  • Start with every sentence alone. Repeatedly merge the two closest groups, until 10 are left.
  • Ward linkage: merge the pair that adds the least spread.
  • Average linkage: merge by average distance. On our data it builds one giant group (step 4): the linkage decides.
  • The full history of merges is the dendrogram.
Data Science and Advanced Programming · UNIL · 2026
Session 11 · Workshop

Naming a topic

A group is just a number. To understand it, find its characteristic words:

  • frequent in the topic, rare in the others: TF-IDF with one "document" per topic;
  • then read one example sentence.

This is the idea behind modern topic models (such as BERTopic): embeddings to group, words to name.

Data Science and Advanced Programming · UNIL · 2026
Session 11 · Workshop

PCA, only to draw

  • 768 axes cannot be drawn. PCA finds the directions with the most spread.
  • For the stocks in the lecture, PC1 was the market. Here PC1 explains about 7 %: meaning is spread out.
  • So we group in all 768 numbers, and use PCA only for the picture (step 6).
Data Science and Advanced Programming · UNIL · 2026
Session 11 · Workshop

How today's file works

  1. Open only week-11/workshop/workshop.py. The data files phrasebank.csv and embeddings.npy just stay in the same folder: you do not open them, the program reads them.
  2. Find the first ___ and replace it.
  3. Press ▷ and read the terminal: ✓ with what the result means, or ✗ with a hint.
  4. Pictures appear as step-N.png next to the file.

Steps: 1 load · 2 PCA · 3 ten topics (Ward) · 4 average linkage · 5 name the topics · 6 🖼 topic map · 7 🖼 tree of topics · 8 🖼 which topics are bad news?
★ 9 k-means vs Ward · 10 the same with word counts · 11 PCA by hand

Data Science and Advanced Programming · UNIL · 2026