Back to Blog
Lesson 20 of the Intermediate Machine Learning: Real-World Pipelines course
AI/MLJune 25, 20263 min read

Project Milestone: Building the Baseline Pipeline

Master the art of building a robust baseline pipeline. Learn to integrate preprocessing and modeling into a single, reproducible workflow for your project.

machine learningscikit-learnpipelinesbaselinemodel developmentdata engineeringaimachine-learningpython

Previously in this course, we covered the individual components of a professional ML system, from ColumnTransformer for heterogeneous data to handling missing values strategically. In this lesson, we synthesize those pieces into an end-to-end baseline pipeline—the most critical project milestone in your journey toward a production-ready model.

Why You Need a Baseline

A baseline is not just a "first attempt." It is the yardstick against which every future improvement is measured. Without a rigorous, simple baseline, you cannot quantify the value of complex feature engineering or sophisticated hyperparameter tuning. In professional settings, a baseline serves three purposes:

  1. Validation of Data Flow: It ensures your data cleaning, encoding, and scaling logic work together without errors.
  2. Performance Floor: It provides a "worst-case" performance metric. If your complex model doesn't beat the baseline, you likely have a design flaw or overfitting issue.
  3. Documentation: It establishes the starting point for your project’s evolution, which is essential for stakeholder communication.

Building Your End-to-End Pipeline

To build our baseline, we will combine our preprocessing stages with a simple, robust estimator (like a Logistic Regression or Random Forest). We use sklearn.pipeline.Pipeline to encapsulate the entire flow, ensuring that every transformation is applied consistently during training and inference.

Worked Example: The Baseline Implementation

Assume we are working on a binary classification task. We need to impute missing values, scale numeric features, and encode categorical variables before fitting a model.

PYTHON
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

# 1. Define feature groups
numeric_features = [CE9178">'age', CE9178">'income', CE9178">'tenure']
categorical_features = [CE9178">'region', CE9178">'plan_type']

# 2. Define preprocessing steps
numeric_transformer = Pipeline(steps=[
    (CE9178">'imputer', SimpleImputer(strategy=CE9178">'median')),
    (CE9178">'scaler', StandardScaler())
])

categorical_transformer = Pipeline(steps=[
    (CE9178">'imputer', SimpleImputer(strategy=CE9178">'constant', fill_value=CE9178">'missing')),
    (CE9178">'encoder', OneHotEncoder(handle_unknown=CE9178">'ignore'))
])

preprocessor = ColumnTransformer(transformers=[
    (CE9178">'num', numeric_transformer, numeric_features),
    (CE9178">'cat', categorical_transformer, categorical_features)
])

# 3. Create the end-to-end pipeline
baseline_pipeline = Pipeline(steps=[
    (CE9178">'preprocessor', preprocessor),
    (CE9178">'classifier', RandomForestClassifier(random_state=42))
])

# 4. Train and compute baseline
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
baseline_pipeline.fit(X_train, y_train)
score = baseline_pipeline.score(X_test, y_test)

print(f"Baseline Accuracy: {score:.4f}")

Hands-on Exercise: Establishing the Baseline

  1. Refactor: Take the code from your previous project work and wrap it into a single Pipeline object as shown above.
  2. Evaluate: Use Introduction to Cross-Validation: Robust Model Evaluation to compute the mean cross-validated score for this pipeline.
  3. Log: Create a simple text file or Markdown document in your repository named BASELINE_REPORT.md. Record the mean accuracy, the standard deviation across folds, and a brief description of the preprocessing steps used.
  4. Compare: If you have an existing model, compare this new pipeline's score against it.

Common Pitfalls in Baseline Development

  • Data Leakage: Even in a baseline, you must ensure that your StandardScaler or SimpleImputer is only fitted on the training folds. Using fit_transform on the entire dataset is a silent killer of model performance.
  • Over-Engineering: Do not perform complex feature engineering yet. A baseline should be simple. If you start with a 50-step pipeline, you won't know which component is causing issues if the model underperforms.
  • Ignoring Metrics: Don't just rely on accuracy. If your data is imbalanced, refer back to Advanced Metrics for Imbalanced Datasets: MCC and Kappa and compute those as part of your baseline assessment.
  • Hard-coding Paths: Avoid absolute file paths. Use relative paths or configuration files so your pipeline runs on your teammate's machine just as easily as it does on yours.

Recap

We have successfully moved from disconnected preprocessing steps to a unified model development workflow. By establishing this baseline, you now have a reproducible foundation that allows for safe experimentation. You have verified that your data pipeline is sound and have a metric to beat in the coming lessons.

Up next: We will begin the process of systematic improvement by introducing GridSearchCV to optimize our model parameters.

Similar Posts