Beginner

Data Preprocessing

Master the essential data preparation techniques: scaling numeric features, encoding categorical variables, handling missing values, and extracting features from text and images.

Feature Scaling

Many ML algorithms perform better when features are on similar scales. Scikit-learn provides several scalers:

Python
from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler

# StandardScaler: zero mean, unit variance (best for most cases)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X_train)

# MinMaxScaler: scale to [0, 1] range
scaler = MinMaxScaler()
X_normed = scaler.fit_transform(X_train)

# RobustScaler: uses median/IQR, robust to outliers
scaler = RobustScaler()
X_robust = scaler.fit_transform(X_train)

# IMPORTANT: fit on train, transform on test
X_test_scaled = scaler.transform(X_test)  # No fit!
Common Mistake: Always fit scalers on training data only, then use transform() on test data. Using fit_transform() on test data causes data leakage and inflated metrics.

Encoding Categorical Variables

Python
from sklearn.preprocessing import OneHotEncoder, OrdinalEncoder, LabelEncoder

# OneHotEncoder: for nominal categories (no order)
ohe = OneHotEncoder(sparse_output=False, handle_unknown="ignore")
X_encoded = ohe.fit_transform(X_categorical)

# OrdinalEncoder: for ordered categories
oe = OrdinalEncoder(categories=[["low", "medium", "high"]])
X_ordinal = oe.fit_transform(X_categorical)

# LabelEncoder: for target variable encoding
le = LabelEncoder()
y_encoded = le.fit_transform(y_labels)

Handling Missing Values

Python
from sklearn.impute import SimpleImputer, KNNImputer

# Simple strategies: mean, median, most_frequent, constant
imputer = SimpleImputer(strategy="median")
X_filled = imputer.fit_transform(X_with_nans)

# KNN-based imputation (uses similar samples)
knn_imp = KNNImputer(n_neighbors=5)
X_knn = knn_imp.fit_transform(X_with_nans)

# For categorical columns
cat_imp = SimpleImputer(strategy="most_frequent")
X_cat_filled = cat_imp.fit_transform(X_categorical)

Feature Extraction

Python
from sklearn.feature_extraction.text import TfidfVectorizer

# Text feature extraction with TF-IDF
tfidf = TfidfVectorizer(max_features=5000, stop_words="english")
X_text = tfidf.fit_transform(documents)

# Polynomial feature generation
from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(degree=2, include_bias=False)
X_poly = poly.fit_transform(X_numeric)

Scaler Comparison

ScalerMethodBest For
StandardScalerz = (x - mean) / stdGeneral purpose, SVM, logistic regression
MinMaxScalerx' = (x - min) / (max - min)Neural networks, bounded features
RobustScalerUses median and IQRData with outliers
MaxAbsScalerx' = x / max(|x|)Sparse data

Next: Model Selection

Learn how to choose the right algorithm, tune hyperparameters, and properly evaluate model performance.

Next: Model Selection →

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.