AI & Machine Learning Topic

Prepare Data for ML: Missing Values, Categories and Useful Features

A model learns from the data you give it. If the rows contain mistakes or information from the future, a good-looking score can mislead you. Data preparation means checking the meaning of the data as well as changing its format.

Understand one row

Write down what one row represents, where each field comes from and when it becomes available. A feature is an input used for prediction. A label is the result you want to predict. An order's final delivery time cannot be an input when predicting that time at checkout.

Split before learning preparation rules

Keep test data separate before fitting an imputer, scaler or feature selector. Fit those steps on training data only. Otherwise, information from the test set can leak into training. A pipeline keeps preparation and the model together, including during cross-validation.

A random split is suitable only when it matches the problem. For future events, split by time. If several rows belong to the same person, keep that person's related rows in one split where needed. Duplicates across splits can also make a score too easy.

Choose a missing-value policy

A blank is not always zero. First ask why it is missing. You might fill a numeric field with a training-set median, use a category such as unknown, add a missing-value flag or remove unusable rows. Explain the choice and check how much data it affects.

For time series, filling from a later value can reveal the future. A method that is reasonable for a historical chart may be wrong for a live prediction.

Categories and scales

One-hot encoding creates indicator columns for categories. It avoids pretending that course names have a numeric order. Standard scaling changes numeric values using a learned centre and spread. It helps many distance-based and linear methods, but is not required by every model.

A complete pipeline

Install numpy, pandas and scikit-learn in your practice environment. Save this as preprocessing.py. The rows are invented and too few to measure real predictive quality.

import numpy as np
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler

# Made-up rows for learning mechanics, not evidence of useful prediction.
frame = pd.DataFrame({
    "hours": [1,2,np.nan,4,5,6,2,3,4,5,6,7],
    "course": ["python","web","python","web","python","web"]*2,
    "completed": [0,0,0,1,1,1,0,0,1,1,1,1]})
X_train, X_test, y_train, y_test = train_test_split(
    frame[["hours", "course"]], frame["completed"],
    test_size=0.25, stratify=frame["completed"], random_state=42)
numeric = make_pipeline(SimpleImputer(strategy="median"), StandardScaler())
transform = ColumnTransformer([
    ("numbers", numeric, ["hours"]),
    ("categories", OneHotEncoder(handle_unknown="ignore"), ["course"])])
model = make_pipeline(transform, LogisticRegression(max_iter=500))
model.fit(X_train, y_train)
print("Predictions:", model.predict(X_test).tolist())
print("Unseen category:", model.predict(pd.DataFrame({"hours":[3],"course":["new"]})).tolist())

Run python preprocessing.py. You should get three test predictions and one prediction for an unseen course category. The pipeline learns the median and category vocabulary from training rows. handle_unknown prevents a new category from crashing this encoder; it does not mean the model understands that course.

Create features carefully

A useful feature might be an account's age at prediction time. Record how it is calculated and use the same logic in training and serving. Do not use a total that includes later purchases. Review identifiers and sensitive fields before including them.

An outlier may be a typo, a rare important case or a valid extreme value. Investigate before deleting it. Keep transformations in code so the work is reviewable and repeatable.

Practice

Add a missing course value and decide how to handle it explicitly. Add another unseen category to prediction. Explain which steps use fit and which use transform. Then identify one feature that would leak future information in a delivery-time model.

Official references

Reviewed on 25 October 2026. Use documentation for your installed versions.

All AI & Machine Learning tutorials

Comments (0)

No comments yet. Be the first to comment!

LecturreAI Assistant

Hello! I'm LecturreAI

What would you like to learn today?