Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 13 — Assignment Project: Discovering Topics in a News Archive

Open in Colab

Overview

A news website has thousands of old posts and no tags. The editor asks: what topics are in the archive, and could the site suggest tags automatically? You will cluster the posts without any labels, find out what each cluster is about, diagnose what goes wrong, improve the pipeline, and report honestly on what works.

Data. Posts from six discussion groups of the 20 Newsgroups collection (computer graphics, cars, hockey, medicine, space, Middle East politics), with headers, signatures and quoted replies removed as far as possible. The group of each post is hidden from your methods: we use it only at the end of each experiment, as the teacher’s check, through the adjusted Rand index (ARI).

PartTaskPoints
ARepresent the posts as vectors20
BCluster them and read the clusters25
CDiagnose and improve; test on fresh posts30
DReport for the editor25

Rules. Use random_state=42. Make all choices on the training posts; the test posts are used once, in C3.


Part A — Represent the Posts (20 points)

A1 (checked). Many posts are one-liners (“Thanks!”) with nothing to cluster. Complete keep_long(docs, min_words), returning the indices of the posts with at least min_words words (split on whitespace).

A2 (checked). Complete make_vectorizer(**options): a TfidfVectorizer with English stop words removed, words appearing in fewer than 5 posts ignored (min_df=5), words appearing in more than half of the posts ignored (max_df=0.5), and any keyword arguments passed on (so that Part C can change them). Each row of the TF-IDF matrix has length 1.

A3. Print the 10 terms with the highest TF-IDF weight in the first training post, next to the start of the post. Do they capture what it is about?


Part B — Cluster and Read the Clusters (25 points)

B1. Reduce the TF-IDF vectors to 100 dimensions with TruncatedSVD (this is latent semantic analysis, LSA: PCA for sparse text) and normalise each row to length 1. Then run k-means for k=2,…,10k = 2, \dots, 10 and plot the inertia and the mean silhouette. Do they suggest a clear kk?

Your answer:

B2 (checked). Complete top_terms(centers, terms, n): centers is a matrix with one row per cluster in TF-IDF space (one column per term); return, for each row, the list of the n terms with the largest values, largest first.

B3. Run k-means with k=6k = 6. For each cluster print its size and its top 8 terms (map the centroids back to TF-IDF space with svd.inverse_transform). Give each cluster a name if you can.

Your names for the clusters:


Part C — Diagnose and Improve (30 points)

C1. Now use the hidden groups as a check. Show the table of clusters against groups (pd.crosstab) and the ARI. Which of your observations in B3 does it confirm?

Your answer:

C2. Try at least three changes to the pipeline, one at a time, measuring the training ARI with k=6k = 6 (n_init=10, random_state=42). Ideas: sublinear_tf=True (use 1+log⁡(count)1 + \log(\text{count}), so that one word repeated many times counts less), fewer or more SVD components, a lower max_df, a different kk. Complete evaluate, show a table, and choose a final pipeline. (Remember the warning of Section 13.7: choosing by ARI uses the labels, which a real project would not have. We do it here to learn which choices matter, and protect ourselves by testing once on fresh posts.)

C3. Run your final pipeline once on the test posts (fit the vectorizer, SVD and k-means on them: clustering has no training phase to reuse). Report the ARI, the cluster-by-group table and the top terms of each cluster.


Part D — Report for the Editor (25 points)

Write 300–400 words for the editor, who is not a data scientist:

  1. What we did, in plain words (no formulas).

  2. What topics we found, with their key words, and which ones the method could not separate.

  3. What went wrong and how we fixed it (one-liners, signatures, generic discussion posts; the changes of C2).

  4. How reliable it is (your test result, in plain words) and how the site should use it: for example, suggesting tags for a human to confirm, not tagging automatically.

Your report:


Grading Rubric

PartFull marks if…
A (20)filtering and vectorizer correct (checked); top terms of a post shown and discussed
B (25)inertia and silhouette plotted and honestly interpreted; top_terms correct (checked); clusters named
C (30)crosstab and ARI; at least three changes tested one at a time; test posts used once; the final pipeline compared with the baseline
D (25)clear for a non-specialist; numbers match the notebook; problems named; sensible advice on use