iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Feature Engineering

Feature engineering is the single biggest lever you have. Better features beat fancier algorithms almost every time. The job: turn raw columns into signals the model can use.

Common transformations by column type

EXAMPLE
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose       import ColumnTransformer

# Numeric — scale + log
df['log_amount']   = np.log1p(df['amount'])
df['amount_vs_median'] = df['amount'] / df.groupby('category')['amount'].transform('median')

# Datetime — extract features
ts = pd.to_datetime(df['ts'])
df['dow']         = ts.dt.dayofweek
df['hour']        = ts.dt.hour
df['is_weekend']  = ts.dt.dayofweek.isin([5, 6]).astype(int)
df['days_since']  = (pd.Timestamp.now() - ts).dt.days

# Text — basic
df['name_len']    = df['name'].str.len()
df['n_words']     = df['title'].str.split().str.len()
df['has_url']     = df['body'].str.contains(r'https?://', regex=True)

# Categorical — one-hot via sklearn
encoder = OneHotEncoder(handle_unknown='ignore', sparse_output=False)
ctry = encoder.fit_transform(df[['country']])

# Put it together — ColumnTransformer
pre = ColumnTransformer([
    ('num', StandardScaler(),                        ['age', 'income', 'log_amount']),
    ('cat', OneHotEncoder(handle_unknown='ignore'),  ['country', 'plan']),
], remainder='drop')

X = pre.fit_transform(train_df)

Why it matters

Beware target leakage. Anything computed using the label (or future info you wouldn’t have at prediction time) inflates training scores and crashes in production.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# Generate features by domain knowledge:
#   datetime → day_of_week, is_weekend
#   text     → length, n_words, tf-idf
#   amount   → log_amount, amount_vs_median
# Use sklearn's ColumnTransformer to keep it tidy.
Try it Yourself »

Discussion

Loading…