Monash University · FACULTY OF INFORMATION TECHNOLOGY

FIT5202 · Data Processing for Big Data

- one subject, every graph, every model, every mark
Information Technology14 Chapters6-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 5 of 11 · FIT5202

Machine Learning: Featurization

Week 5 opens the Complexity theme of Monash FIT5202: machine learning at scale in Spark MLlib. It distinguishes supervised from unsupervised learning, defines features and feature vectors, and covers featurization = extraction + transformation + selection — CountVectorizer and TF-IDF, tokenization, stop-word removal, string indexing and one-hot encoding, and the Vector Assembler. It also introduces the Spark ML Pipeline (transformers and estimators) and train/test evaluation. Featurization steps and TF-IDF reasoning are common written-response and multiple-choice material, and underpin Assignment 2 Part A.

In this chapter

What this chapter covers

  • 01Supervised (classification, regression) vs unsupervised (clustering, association) learning
  • 02Features, feature vectors and the ML pipeline: featurization → training → evaluation → best model
  • 03Spark MLlib and the ML Pipeline abstraction (transformers vs estimators)
  • 04Featurization = Extraction + Transformation + Selection
  • 05Extractors: CountVectorizer (token-count vectors), TF-IDF, Word2Vec
  • 06TF-IDF: IDF(t,D) = log[(|D|+1)/(DF(t,D)+1)], TFIDF = TF·IDF (rare-but-frequent = important)
  • 07Transformers: tokenization, stop-word removal, string indexing, one-hot encoding, Vector Assembler
  • 08One-hot encoding and the ordinality problem; train/test split and evaluation basics
Worked example · free

TF-IDF: scoring how useful a term is as a feature

Q [5 marks]. A corpus has |D| = 3 documents. The term "spark" appears in 1 of them; the term "the" appears in all 3. In document d1 the term "spark" occurs TF = 2 times. Using the taught IDF(t,D) = log[(|D|+1)/(DF(t,D)+1)] and TFIDF = TF·IDF (natural log, as Spark MLlib uses), find IDF("the"), IDF("spark") and TFIDF("spark", d1). Which term is the more useful feature? (5 marks)
  • +1"the" appears in every document, so its document frequency DF = 3 = |D|. IDF("the") = ln[(3+1)/(3+1)] = ln(1) = 0 — a term in every document carries no discriminating information.
  • +1"spark" appears in DF = 1 document. IDF("spark") = ln[(3+1)/(1+1)] = ln(4/2) = ln(2) ≈ 0.693.
  • +1TFIDF("spark", d1) = TF · IDF = 2 × 0.693 ≈ 1.386.
  • +1TFIDF("the", d1) = TF × 0 = 0, no matter how often "the" occurs, because its IDF is zero.
  • +1"spark" is the more useful feature: a high TF-IDF means frequent in this document but rare across the corpus, exactly the pattern that discriminates documents. "the" behaves like a stop word with zero weight.
IDF("the") = 0, IDF("spark") ≈ 0.693, TFIDF("spark", d1) ≈ 1.386. "spark" is the discriminating feature; "the" gets zero weight because it appears in every document.
Sia tip — The intuition beats the arithmetic: a term in every document has IDF = 0 and is useless as a feature, while a term that is frequent in one document but rare overall scores high. Spark MLlib uses natural log for IDF, so state your base — a different base rescales every score.
Glossary

Key terms

Featurization
Turning raw data into numeric feature vectors a model can learn from: Extraction + Transformation + Selection. In Spark it is built from pipeline stages.
Spark ML Pipeline
A sequence of stages that are either Transformers (transform a DataFrame, e.g. Tokenizer, OneHotEncoder) or Estimators (fit to data to produce a model, e.g. a classifier). Pipelines make featurization + training reproducible.
CountVectorizer
An extractor that builds a vocabulary of the most frequent tokens and represents each document as a vector of token counts over that vocabulary.
TF-IDF
Term frequency × inverse document frequency: TFIDF = TF·IDF with IDF(t,D) = log[(|D|+1)/(DF(t,D)+1)]. It scores terms that are frequent in a document but rare across the corpus; a term in every document gets IDF = 0.
One-hot encoding
Converting a categorical index into a binary vector so that no false ordering is implied. It solves the ordinality problem; Spark uses an N−1-dimensional vector for N categories.
String indexing
Mapping string category labels to numeric indices ordered by frequency (most frequent = 0.0). It precedes one-hot encoding, which removes the accidental ordering the indices introduce.
FAQ

Machine Learning: Featurization FAQ

Why does TF-IDF down-weight common words automatically?

Because a word appearing in every document has DF = |D|, making IDF(t,D) = log[(|D|+1)/(|D|+1)] = log(1) = 0, so its TF-IDF is zero regardless of frequency. Words that appear in few documents get a positive IDF and higher weight. This is why TF-IDF favours discriminating terms without a hand-built stop-word list.

What is the ordinality problem and how does one-hot encoding fix it?

String indexing turns categories into numbers (e.g. Python→0, Scala→1, JavaScript→2), and many algorithms wrongly read a larger index as 'more', which is meaningless for unordered categories. One-hot encoding replaces the index with a binary vector so no category is numerically bigger than another, removing the false order.

What is the difference between a Transformer and an Estimator in Spark MLlib?

A Transformer maps a DataFrame to a DataFrame with no learning (a Tokenizer or OneHotEncoder), while an Estimator has a fit() method that learns from data to produce a model or a fitted transformer (a classifier, or CountVectorizer learning its vocabulary). A Pipeline chains both so the same steps apply to training and new data.

Can AI help me with featurization for Assignment 2?

Yes, as a study aid. Sia can explain each featurization stage, walk a TF-IDF or one-hot calculation, and check your understanding of the Pipeline API step by step. It will not write or debug your graded assignment for you, and Monash academic-integrity rules — including selective, documented GenAI use where a task allows it — apply.

Study strategy

Exam move

Learn featurization as a pipeline of named stages and be able to say what each one does and why. Rehearse a TF-IDF calculation until the appears-everywhere-so-IDF-zero property is instant, and be ready to explain the ordinality problem that one-hot encoding fixes. Because this material powers Assignment 2 Part A, practise it in a PySpark notebook, not just on paper, and keep the Week-5 quiz warm on Moodle. Ask Sia to generate fresh featurization drills at your level and to check your reasoning through SWOTVAC.

Working through Machine Learning: Featurization in FIT5202? Sia is AskSia’s AI Information Technology tutor — ask any FIT5202 Machine Learning: Featurization question and get a clear, step-by-step explanation grounded in how FIT5202 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 47 of your Monash University subjects - and 1,000+ Bibles across every Australian university.
Sia - your FIT5202 tutor, unlimited, worked the way the exam marks it
The full 6-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$25/ month
30-day money-back · cancel in one tap · how it works
Unlock the full FIT5202 Bible + 47 Monash University subjects解锁完整 FIT5202 Bible + Monash University 47 门科目
$25/mo