TECHNOLOGIESTieInn Technologies

Turning data into features: numbers, categories and text

ML Foundations · 10 questions

Short notes

Key idea

Models only understand numbers. Feature engineering turns raw columns (dates, categories, text) into useful numeric inputs, and often matters more than which model you pick.

Syntax

from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.feature_extraction.text import TfidfVectorizer

pre = ColumnTransformer([
    ("num", StandardScaler(), ["orders_90d", "avg_amount"]),
    ("cat", OneHotEncoder(handle_unknown="ignore"), ["city", "plan"]),
    ("txt", TfidfVectorizer(max_features=500), "last_review"),
])
  • Numbers: scale for distance-based and linear models; log-transform skewed amounts.
  • Categories: one-hot encoding; never label-encode unordered categories for linear models.
  • Dates: days since, day of week, month.
  • Text: TF-IDF or embeddings.

Example

Instead of the raw signup date, "days since signup" and "orders per active month" make churn far easier to predict, with the same model.

Traps

  • City as 1, 2, 3 tells a linear model that city 3 is "bigger" than city 1.
  • New categories at prediction time crash a strict encoder; use handle_unknown="ignore".
  • Too many one-hot columns from IDs (customer_id) cause overfitting; drop or group them.
  • Fit encoders on training data only, inside a pipeline.

Want the full lesson with step-by-step examples, a project and a certificate? ML Foundations course, ₹180 →

Questions

Open in Code Lab5 free questions
5 questions

    Turning data into features: numbers, categories and text

  1. Question 1easy

    Why one-hot encode the device column instead of mobile = 1, desktop = 2, tablet = 3?

  2. Turning data into features: numbers, categories and text

  3. Question 2medium

    How many columns does this create?

    import pandas as pd
    df = pd.DataFrame({"device": ["mobile", "desktop", "tablet", "mobile"], "source": ["ads", "email", "ads", "direct"]})
    print(pd.get_dummies(df, dtype=int).shape[1])
  4. Turning data into features: numbers, categories and text

  5. Question 3medium

    What does this print?

    from sklearn.feature_extraction.text import CountVectorizer
    vec = CountVectorizer().fit(["good good bad"])
    print(vec.transform(["good good bad"]).toarray().tolist())
  6. Turning data into features: numbers, categories and text

  7. Question 4hard

    Drop duplicate review_texts from product_reviews, label positive as rating >= 4, split with test_size=0.3, random_state=0, stratify=y, train make_pipeline(CountVectorizer(), LogisticRegression(max_iter=1000)), and store the test accuracy, rounded to 2 decimals, in result.

    The tables are ready as DataFrames with the same names. Store your answer in result.

    Tables
  8. Turning data into features: numbers, categories and text

  9. Question 5medium

    In a logistic regression on word counts, a word has a large negative weight. What does that mean?

5 more questions on this topic, with hints, and tests

Sign up free for 21 days of every question and test.