Session 10 · Workshop

Workshop: good news, bad news, or neither?

The classifiers from the lecture, on real sentences from financial news.

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

The data: Financial PhraseBank

2,264 sentences from financial news, each labelled by finance experts (all of them agreed):

Pretax profit totaled EUR 9.0 mn, down from EUR 36.3 mn in 2007. → negative
Operating profit improved by 27% to EUR 579.8 mn. → positive
The share subscription period will commence on 1 September 2008. → neutral

61 % are neutral: a model that always says "neutral" is right 61 % of the time. That is the bar to beat.

Malo, Sinha, Korhonen, Wallenius & Takala (2014), Good debt or bad debt, JASIST. Licence CC BY-NC-SA 3.0.

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

Way 1: the bag of words

One column per word of the vocabulary. Each cell: how often the word appears in the sentence.

profit down fell improved …
Pretax profit … down from … 1 1 0 0 …
Operating profit improved … 1 0 0 1 …

CountVectorizer().fit_transform(sentences): 2,264 rows × 6,057 columns, 99.7 % zeros.

It knows words, not meaning: fell and dropped are two unrelated columns.

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

More columns than rows: high-dimensional data

  • Each sentence is a point in a space with one axis per word: 6,057 axes.
  • Two sentences that use different words are "far apart", even if they say the same thing.
  • With more columns than rows, a model can always find some word that separates the training sentences, by chance.

That is why we always judge a model on sentences it has not seen.

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

Way 2: embeddings

A sentence model (EmbeddingGemma, by Google) turns each sentence into 768 numbers, so that sentences with a similar meaning get similar numbers.

  • It learned this from huge amounts of text, before it ever saw ours: it knows that fell, dropped and declined go together.
  • We ran it once for you: embeddings.npy. Nothing to install.
  • Same table shape as before: one row per sentence, now 768 columns, none of them zero.
Data Science and Advanced Programming · UNIL · 2026
Session 10 · Workshop

How close are two vectors? Cosine similarity

  • 1: same direction. 0: nothing in common.
  • The lengths cancel: a long sentence is not "more similar" just because it is long.
  • In code: cosine_similarity(A, B) from scikit-learn. In step 3 you also compute it by hand.
Data Science and Advanced Programming · UNIL · 2026
Session 10 · Workshop

The four models from the lecture, on text

Model Idea Word counts Embeddings
kNN copy the label of the closest examples weak good
Logistic regression a weight per column, summed, turned into a probability strong strongest
Decision tree yes/no questions, one column at a time fair weak
Random forest many trees, majority vote fair fair
  • kNN needs distances that mean something: close word counts are not close meanings.
  • One tree can split on one word (decreased), but one of 768 embedding numbers means nothing alone.
  • On text, a linear model is the classic first choice. You check this table yourself in step 9.
Data Science and Advanced Programming · UNIL · 2026
Session 10 · Workshop

Labels are expensive

  • Every label in this data was written by a person with a finance degree.
  • So the question is not only "how accurate?" but "how many labels do I need?"
  • In step 8 you train on 50, 100, 200, 500 sentences, and draw the curve for both ways.

The embedding model learned the language on its own; our few labels only have to teach it good and bad.

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

How today's file works

  1. Open only week-10/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. Some steps ask for a whole line.
  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 first look · 2 bag of words · 3 cosine by hand · 4 embeddings · 5 closest sentences, two ways · 6 classifier on words · 7 classifier on embeddings · 8 🖼 how many labels? · 9 the four models
★ 10 the "negative" words · 11 the confusion matrix · 12 kNN with cosine, by hand

Data Science and Advanced Programming · UNIL · 2026