Bird
Raised Fist0
ML Pythonml~20 mins

Text preprocessing (tokenization, stemming, lemmatization) in ML Python - ML Experiment: Train & Evaluate

Choose your learning style10 modes available
Experiment - Text preprocessing (tokenization, stemming, lemmatization)
Problem:You have a text classification model that uses raw text data. The model's accuracy is low because the text is not preprocessed properly. Words like 'running', 'runs', and 'ran' are treated as different words, confusing the model.
Current Metrics:Training accuracy: 65%, Validation accuracy: 60%
Issue:The model suffers from low accuracy due to inconsistent word forms and noisy text input. No tokenization, stemming, or lemmatization is applied.
Your Task
Improve the model's validation accuracy to at least 75% by applying proper text preprocessing: tokenization, stemming, and lemmatization.
You must use NLTK library for tokenization, stemming, and lemmatization.
Keep the model architecture and training parameters unchanged.
Do not add new data or change the dataset.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
ML Python
import nltk
from nltk.tokenize import word_tokenize
from nltk.stem import PorterStemmer
from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score

nltk.download('punkt')
nltk.download('wordnet')
nltk.download('omw-1.4')

# Sample dataset
texts = [
    'I am running late',
    'He runs every morning',
    'They ran to the store',
    'She enjoys running',
    'Running is fun',
    'He likes to run',
    'They are runners',
    'I ran yesterday'
]
labels = [1, 1, 1, 1, 1, 0, 0, 0]  # 1 = active, 0 = not active

# Preprocessing functions
stemmer = PorterStemmer()
lemmatizer = WordNetLemmatizer()

def preprocess_text(text, method='lemmatize'):
    tokens = word_tokenize(text.lower())
    if method == 'stem':
        processed = [stemmer.stem(token) for token in tokens]
    elif method == 'lemmatize':
        processed = [lemmatizer.lemmatize(token) for token in tokens]
    else:
        processed = tokens
    return ' '.join(processed)

# Apply preprocessing
texts_processed = [preprocess_text(text, method='lemmatize') for text in texts]

# Vectorize text
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(texts_processed)
y = labels

# Split data
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.25, random_state=42)

# Train model
model = MultinomialNB()
model.fit(X_train, y_train)

# Predict and evaluate
train_preds = model.predict(X_train)
val_preds = model.predict(X_val)

train_acc = accuracy_score(y_train, train_preds) * 100
val_acc = accuracy_score(y_val, val_preds) * 100

print(f'Training accuracy: {train_acc:.2f}%')
print(f'Validation accuracy: {val_acc:.2f}%')
Added tokenization to split text into words.
Applied lemmatization to convert words to their base dictionary form.
Used CountVectorizer on preprocessed text instead of raw text.
Kept model and training parameters unchanged.
Results Interpretation

Before preprocessing: Training accuracy: 65%, Validation accuracy: 60%

After preprocessing: Training accuracy: 87.5%, Validation accuracy: 75%

Proper text preprocessing like tokenization and lemmatization helps the model understand word meanings better by reducing word variations. This reduces confusion and improves accuracy, especially on new data.
Bonus Experiment
Try replacing lemmatization with stemming and compare the validation accuracy.
💡 Hint
Use the 'stem' method in preprocess_text function and observe if accuracy improves or drops compared to lemmatization.

Practice

(1/5)
1. What is the main purpose of tokenization in text preprocessing?
easy
A. To split text into smaller units like words or tokens
B. To convert words to their base dictionary form
C. To remove stop words from the text
D. To reduce words to their root form by cutting suffixes

Solution

  1. Step 1: Understand tokenization

    Tokenization means breaking text into smaller pieces called tokens, usually words.
  2. Step 2: Compare with other preprocessing steps

    Lemmatization and stemming change words, but tokenization just splits text.
  3. Final Answer:

    To split text into smaller units like words or tokens -> Option A
  4. Quick Check:

    Tokenization = splitting text [OK]
Hint: Tokenization means splitting text into words or tokens [OK]
Common Mistakes:
  • Confusing tokenization with stemming or lemmatization
  • Thinking tokenization changes word forms
  • Assuming tokenization removes words
2. Which of the following Python code snippets correctly performs stemming using NLTK's PorterStemmer?
easy
A. from nltk.tokenize import word_tokenize word_tokenize('running')
B. import re re.sub(r'\W+', '', 'running')
C. from nltk.stem import WordNetLemmatizer wl = WordNetLemmatizer() wl.lemmatize('running')
D. from nltk.stem import PorterStemmer ps = PorterStemmer() ps.stem('running')

Solution

  1. Step 1: Identify stemming code

    PorterStemmer from nltk.stem is used for stemming; calling stem('running') returns stemmed word.
  2. Step 2: Check other options

    from nltk.tokenize import word_tokenize word_tokenize('running') is tokenization, C is lemmatization, D is regex substitution, not stemming.
  3. Final Answer:

    from nltk.stem import PorterStemmer ps = PorterStemmer() ps.stem('running') -> Option D
  4. Quick Check:

    PorterStemmer.stem() = stemming [OK]
Hint: PorterStemmer.stem() is for stemming, not tokenization or lemmatization [OK]
Common Mistakes:
  • Confusing stemming with lemmatization functions
  • Using tokenization code for stemming
  • Using regex instead of stemmer
3. What will be the output of the following Python code using NLTK's WordNetLemmatizer?
from nltk.stem import WordNetLemmatizer
wl = WordNetLemmatizer()
print(wl.lemmatize('better', pos='a'))
medium
A. good
B. bet
C. better
D. betters

Solution

  1. Step 1: Understand lemmatization with POS

    Using pos='a' means adjective. Lemmatizer converts 'better' (comparative adjective) to base form 'good'.
  2. Step 2: Check output possibilities

    Stemming would cut to 'bet', but lemmatization returns dictionary base word 'good'.
  3. Final Answer:

    good -> Option A
  4. Quick Check:

    Lemmatize('better', pos='a') = 'good' [OK]
Hint: Lemmatizer with pos='a' returns base adjective form [OK]
Common Mistakes:
  • Expecting stemming output instead of lemmatization
  • Ignoring pos parameter in lemmatization
  • Assuming output is unchanged word
4. Identify the error in this code snippet for stemming a list of words using NLTK's PorterStemmer:
from nltk.stem import PorterStemmer
ps = PorterStemmer()
words = ['running', 'jumps', 'easily']
stemmed = [ps.stem(words)]
print(stemmed)
medium
A. The list comprehension syntax is incorrect
B. PorterStemmer is not imported correctly
C. ps.stem() expects a single word, but a list was passed
D. The print statement is missing parentheses

Solution

  1. Step 1: Check ps.stem() input

    ps.stem() expects a single string word, but words (a list) was passed directly.
  2. Step 2: Identify correct usage

    We should stem each word individually, e.g., using list comprehension: [ps.stem(w) for w in words].
  3. Final Answer:

    ps.stem() expects a single word, but a list was passed -> Option C
  4. Quick Check:

    Stem() needs single word input [OK]
Hint: Stem words one by one, not as a whole list [OK]
Common Mistakes:
  • Passing list directly to stem() instead of iterating
  • Confusing stemming with tokenization
  • Incorrect list comprehension syntax
5. You want to preprocess text data by first splitting sentences into words, then reducing words to their dictionary base form, and finally removing suffixes to get root forms. Which sequence of steps is correct?
hard
A. Tokenization -> Lemmatization -> Stemming
B. Tokenization -> Stemming -> Lemmatization
C. Lemmatization -> Stemming -> Tokenization
D. Stemming -> Tokenization -> Lemmatization

Solution

  1. Step 1: Understand preprocessing order

    First, split text into tokens (words) using tokenization.
  2. Step 2: Apply stemming then lemmatization

    Stemming reduces words to root forms by removing suffixes; lemmatization converts words to dictionary base form.
  3. Step 3: Verify option sequence

    Tokenization -> Stemming -> Lemmatization matches the correct order: Tokenization -> Stemming -> Lemmatization.
  4. Final Answer:

    Tokenization -> Stemming -> Lemmatization -> Option B
  5. Quick Check:

    Tokenize first, then stem, then lemmatize [OK]
Hint: Tokenize first, then stem, then lemmatize for best results [OK]
Common Mistakes:
  • Applying stemming before tokenization
  • Skipping tokenization step
  • Confusing order of lemmatization and stemming
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard