BACK_TO_LOGS// STEP_6_OF_10
HANDBOOK_GUIDE2026-06-29

Human Development Index Predictor: End-to-End Machine Learning Guide

By Rayan Syed
45 min read

Part 6: Production Script - Model Comparison & Training (train.py)

We now transition from interactive notebook exploration to writing a production-grade Python script named train.py. While Jupyter is excellent for visual exploration (EDA) and prototype cleaning, standard Python scripts are preferred in production environments to run training pipelines from end to end and save the resulting model files.

6.1 Understanding Machine Learning Models & Algorithms

In this script, we train and compare three distinct regression models to find the one that yields the highest R-Squared score and lowest Mean Squared Error:

  • Linear Regression: A classic linear algorithm that fits a straight-line equation to map features to target scores.
  • Ridge Regression: An advanced linear regression variant that adds a small L2 regularization penalty to prevent overfitting on highly correlated features.
  • Random Forest Regressor: A powerful non-linear ensemble algorithm that averages predictions from 100 decision trees to capture complex interactions between inputs.

Model Comparison Training Script (train.py)

Save this complete script as train.py in your project root folder:

import os
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, Ridge
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import r2_score, mean_squared_error
import joblib

def run_training_pipeline():
    print("Step 1: Loading GNI / HDI dataset...")
    df = pd.read_csv('data/hdi_data.csv')

    print("Step 2: Resolving missing values...")
    # Fill empty cells with median
    for col in df.columns:
        if df[col].isnull().sum() > 0:
            df[col] = df[col].fillna(df[col].median())

    print("Step 3: Transforming income via natural log...")
    df['log_gross_inc_percap'] = np.log(df['gross_inc_percap'])

    # Split features and target
    X = df[['life_expectancy', 'expec_yr_school', 'mean_yr_school', 'log_gross_inc_percap']]
    y = df['hdi']

    # Split data into 80% training and 20% validation splits
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

    print("\nStep 4: Training and comparing regression models...")
    models = {
        "Linear Regression": LinearRegression(),
        "Ridge Regression": Ridge(alpha=1.0),
        "Random Forest Regressor": RandomForestRegressor(n_estimators=100, random_state=42)
    }

    best_r2 = -1.0
    best_model = None
    best_model_name = ""

    for name, reg in models.items():
        # Fit regression model
        reg.fit(X_train, y_train)
        y_pred = reg.predict(X_test)
        
        r2 = r2_score(y_test, y_pred)
        mse = mean_squared_error(y_test, y_pred)
        print(f"-> {name} | R2 Score: {r2 * 100:.2f}% | MSE: {mse:.6f}")
        
        if r2 > best_r2:
            best_r2 = r2
            best_model = reg
            best_model_name = name

    print(f"\nWinner Selected: {best_model_name} with R2 Score of {best_r2 * 100:.2f}%.")

    print("Step 5: Serializing and saving best model...")
    os.makedirs('models', exist_ok=True)
    joblib.dump(best_model, 'models/hdi_model.joblib')
    print("✓ Output model: models/hdi_model.joblib")

if __name__ == '__main__':
    run_training_pipeline()

6.2 Executing the Training Script

  • Open your project terminal and run:
    python train.py
    
  • Expected Output: The terminal will print out step-by-step progress, output R2 percentages and MSE scores for each algorithm, print the winner name, and confirm serialization.