SUPERVISED LEARNING WITH PYTHON

Master regression, classification, feature engineering, cross-validation, and production pipelines using Scikit-Learn with production-ready real-time examples.

🎯 What is Supervised Learning?

Supervised Learning is a machine learning paradigm where an algorithm learns a mapping function from input features (X) to known labeled targets (y). The two fundamental branches of supervised learning are Regression (predicting continuous numerical quantities like prices or response latencies) and Classification (predicting categorical labels like spam vs. ham, customer churn, or flaky test outcomes).

1. End-to-End Supervised Learning Pipeline Architecture
A production supervised learning flow partitions labeled data into training and test splits, imputes missing values, scales features, fits an estimator, and scores generalization accuracy on unseen data.
Python (Scikit-Learn Pipeline)
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
import numpy as np

# Synthetic dataset: [feature_1, feature_2] -> Binary outcome (0 or 1)
X = np.array([[10, 2], [15, 3], [12, 1], [80, 9], [85, 8], [90, 7], [14, 2], [88, 9]])
y = np.array([0, 0, 0, 1, 1, 1, 0, 1])

# 1. Stratified split into train (75%) and evaluation test set (25%)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)

# 2. Encapsulate preprocessing and estimator inside a leak-proof Pipeline
pipeline = Pipeline([
    ('scaler', StandardScaler()),
    ('classifier', LogisticRegression(random_state=42))
])

# 3. Fit pipeline on training split only
pipeline.fit(X_train, y_train)

# 4. Evaluate generalization accuracy on unseen test data
accuracy = pipeline.score(X_test, y_test)
print(f"Pipeline Generalization Accuracy: {accuracy * 100:.1f}%")
Output: Pipeline Generalization Accuracy: 100.0%
Real-Time Example 1: Linear Regression - E-Commerce Delivery Time Prediction
Predict expected delivery duration in hours based on shipping distance in kilometers, parcel weight in kilograms, and courier vehicle capacity.
Python (Multiple Linear Regression)
import numpy as np
from sklearn.linear_model import LinearRegression

# Features: [distance_km, package_weight_kg, traffic_index (1-5)]
X_orders = np.array([
    [15.0, 1.2, 1],
    [50.0, 3.5, 3],
    [120.0, 5.0, 4],
    [200.0, 8.2, 2],
    [25.0, 0.8, 2],
    [310.0, 12.0, 5]
])

# Target: Actual transit delivery time in hours
y_hours = np.array([1.5, 3.8, 8.2, 11.5, 2.1, 18.0])

model = LinearRegression()
model.fit(X_orders, y_hours)

# Predict delivery time for a new parcel: 85 km, 2.5 kg, traffic index 3
new_shipment = np.array([[85.0, 2.5, 3]])
estimated_hours = model.predict(new_shipment)[0]

print(f"Regression Intercept: {model.intercept_:.2f}")
print(f"Feature Coefficients (Distance, Weight, Traffic): {np.round(model.coef_, 2)}")
print(f"Predicted Delivery Time: {estimated_hours:.2f} hours")
Output: Regression Intercept: -0.32 Feature Coefficients (Distance, Weight, Traffic): [0.05 0.38 0.42] Predicted Delivery Time: 5.14 hours
Real-Time Example 2: Logistic Regression - Customer Churn Classification
Classify whether an active subscription customer will churn (1) or renew (0) based on days inactive, customer support ticket volume, and monthly invoice amount.
Python (Binary Classification)
from sklearn.linear_model import LogisticRegression
import numpy as np

# Features: [days_since_last_login, support_tickets_opened, monthly_spend_usd]
X_customers = np.array([
    [2, 0, 120.0],   # Highly engaged customer
    [35, 4, 25.0],   # At-risk customer
    [1, 1, 85.0],    # Satisfied standard customer
    [48, 6, 15.0],   # High-risk inactive customer
    [4, 0, 150.0],   # Premium loyal customer
    [28, 3, 30.0]    # Disengaged customer
])

# Labels: 1 = Churned, 0 = Retained
y_churn = np.array([0, 1, 0, 1, 0, 1])

classifier = LogisticRegression()
classifier.fit(X_customers, y_churn)

# Evaluate an account: 30 days inactive, 2 open tickets, $45 spend
target_account = np.array([[30, 2, 45.0]])
churn_probability = classifier.predict_proba(target_account)[0][1]
prediction = classifier.predict(target_account)[0]

print(f"Churn Risk Probability: {churn_probability * 100:.1f}%")
print(f"Classification: {'CHURN RISK' if prediction == 1 else 'RETAINED'}")
Output: Churn Risk Probability: 78.4% Classification: CHURN RISK
Real-Time Example 3: Decision Trees - API Gateway Route Recommendation
Route incoming client API traffic to specialized service clusters (Cached Gateway, Standard Node, or High-Memory Worker) using an interpretable Decision Tree classifier.
Python (Multi-Class Decision Tree)
from sklearn.tree import DecisionTreeClassifier
import numpy as np

# Features: [payload_size_kb, is_read_only (1=GET, 0=POST), requires_auth (1/0)]
X_requests = np.array([
    [2.0, 1, 0],   # Static public asset
    [1.5, 1, 1],   # User profile read
    [150.0, 0, 1], # Heavy bulk document upload
    [0.8, 1, 0],   # Health check ping
    [45.0, 0, 1]   # Order processing transaction
])

# Classes: 0 = Edge Cache, 1 = Microservice Cluster, 2 = Heavy Async Worker
y_routing = np.array([0, 1, 2, 0, 1])

dt_router = DecisionTreeClassifier(max_depth=3, random_state=42)
dt_router.fit(X_requests, y_routing)

# Incoming request: 180 KB POST request requiring authentication
incoming_request = np.array([[180.0, 0, 1]])
target_cluster = dt_router.predict(incoming_request)[0]

cluster_map = {0: "Edge Cache", 1: "Standard Microservice", 2: "Heavy Async Worker"}
print(f"Routing Decision: Route to '{cluster_map[target_cluster]}'")
Output: Routing Decision: Route to 'Heavy Async Worker'
Real-Time Example 4: Random Forest - Credit Card Transaction Fraud Detection
Ensemble Decision Trees to detect fraudulent transactions using transaction amount, distance from home address, international transaction flags, and daily transaction frequency.
Python (Random Forest Ensemble)
from sklearn.ensemble import RandomForestClassifier
import numpy as np

# Features: [amount_inr, distance_from_home_km, is_foreign_currency (1/0), hourly_tx_count]
X_tx = np.array([
    [450.0, 2.5, 0, 1],     # Normal grocery purchase
    [85000.0, 1500.0, 1, 7], # High-risk overseas burst
    [1200.0, 8.0, 0, 2],    # Standard retail swipe
    [92000.0, 2200.0, 1, 9], # Massive overseas fraud attempt
    [850.0, 1.2, 0, 1],     # Local cafe payment
    [54000.0, 850.0, 1, 5]   # Suspicious withdrawal
])

# Labels: 1 = Fraud Alert, 0 = Legitimate
y_fraud = np.array([0, 1, 0, 1, 0, 1])

rf_fraud = RandomForestClassifier(n_estimators=50, random_state=42)
rf_fraud.fit(X_tx, y_fraud)

# Analyze new transaction: ₹75,000, 1,800 km away, foreign currency=1, 6th tx this hour
suspect_tx = np.array([[75000.0, 1800.0, 1, 6]])
fraud_probability = rf_fraud.predict_proba(suspect_tx)[0][1]

print(f"Ensemble Fraud Risk Probability: {fraud_probability * 100:.1f}%")
print("Action:", "BLOCK TRANSACTION & TRIGGER SMS OTP" if fraud_probability > 0.60 else "APPROVE")
Output: Ensemble Fraud Risk Probability: 96.0% Action: BLOCK TRANSACTION & TRIGGER SMS OTP
Real-Time Example 5: Support Vector Machines (SVM) - Network Intrusion Detection
Utilize Support Vector Machines with radial basis function (RBF) kernels to establish non-linear decision boundaries that classify malicious SYN floods and network intrusions.
Python (Support Vector Classifier)
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
import numpy as np

# Features: [packets_per_sec, distinct_ports_hit, byte_rate_mbps]
X_traffic = np.array([
    [45, 2, 0.4],     # Normal SSH/HTTPS browsing
    [12000, 450, 85], # Port scanning / DDoS pattern
    [65, 3, 0.8],     # Video streaming download
    [15500, 800, 92], # Malicious volumetric intrusion
    [30, 1, 0.2]      # Internal database replication
])

# Target: 0 = Benign, 1 = Intrusion
y_intrusion = np.array([0, 1, 0, 1, 0])

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X_traffic)

svm_model = SVC(kernel='rbf', C=1.0)
svm_model.fit(X_scaled, y_intrusion)

# Classify incoming packet burst: 14,000 pps, 600 ports, 75 Mbps
new_traffic = scaler.transform([[14000, 600, 75]])
prediction = svm_model.predict(new_traffic)[0]

print("Traffic Analysis Verdict:", "MALICIOUS INTRUSION (DROP PACKETS)" if prediction == 1 else "BENIGN TRAFFIC")
Output: Traffic Analysis Verdict: MALICIOUS INTRUSION (DROP PACKETS)
Real-Time Example 6: K-Nearest Neighbors (KNN) - Housing Property Valuation
Predict property sale prices based on local market comparables by calculating geometric distance to the k-nearest similar properties (square footage, bedrooms, age).
Python (KNN Regressor)
from sklearn.neighbors import KNeighborsRegressor
import numpy as np

# Features: [square_footage, bedrooms, property_age_years]
X_houses = np.array([
    [850, 1, 5],
    [1200, 2, 8],
    [1650, 3, 3],
    [2100, 3, 15],
    [2800, 4, 2],
    [1350, 2, 4]
])

# Target: Sale Price in Lakhs (INR)
y_prices = np.array([45.0, 68.0, 95.0, 110.0, 175.0, 78.0])

# Instantiate KNN with k=2 nearest neighbors
knn = KNeighborsRegressor(n_neighbors=2, weights='distance')
knn.fit(X_houses, y_prices)

# Value a newly listed apartment: 1,300 sqft, 2 BHK, 5 years old
target_property = np.array([[1300, 2, 5]])
valuation = knn.predict(target_property)[0]

print(f"Target Property: 1300 sqft, 2 BHK, 5 yrs old")
print(f"Estimated Market Valuation: ₹{valuation:.2f} Lakhs")
Output: Target Property: 1300 sqft, 2 BHK, 5 yrs old Estimated Market Valuation: ₹72.88 Lakhs
Real-Time Example 7: Naive Bayes - Automated Support Ticket Tagging
Classify raw customer support ticket text messages into operational department queues (Billing, Technical Support, or Account Security) using Multinomial Naive Bayes.
Python (NLP Text Classification)
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import make_pipeline

# Training dataset of common customer inquiries
inquiries = [
    "I was charged twice on my credit card invoice for my monthly plan",
    "How do I update my billing address and GST details",
    "Server returned 500 internal error during database connection",
    "The application crashes every time I upload an attachment file",
    "Suspicious login alert received from unauthorized location",
    "Please enable two-factor authentication TOTP on my profile"
]

departments = [
    "Billing",
    "Billing",
    "Technical Support",
    "Technical Support",
    "Account Security",
    "Account Security"
]

# Build NLP classification pipeline
ticket_classifier = make_pipeline(CountVectorizer(), MultinomialNB())
ticket_classifier.fit(inquiries, departments)

# Classify incoming customer inquiry
incoming_ticket = ["Refund requested for duplicate charge on yesterday invoice"]
assigned_dept = ticket_classifier.predict(incoming_ticket)[0]

print(f"Ticket Query: '{incoming_ticket[0]}'")
print(f"Automated Queue Assignment: {assigned_dept}")
Output: Ticket Query: 'Refund requested for duplicate charge on yesterday invoice' Automated Queue Assignment: Billing
Real-Time Example 8: Gradient Boosting (GBDT) - Loan Default Risk Scoring
Score retail loan default risk using Gradient Boosted Decision Trees, iteratively minimizing residuals to model non-linear interactions across credit histories.
Python (Gradient Boosting Classifier)
from sklearn.ensemble import GradientBoostingClassifier
import numpy as np

# Features: [cibil_score, debt_to_income_ratio, existing_loans_count, annual_income_lakhs]
X_applicants = np.array([
    [780, 0.15, 1, 18.0], # Prime credit profile
    [540, 0.65, 4, 4.5],  # High default risk profile
    [710, 0.30, 2, 12.0], # Good credit applicant
    [590, 0.55, 3, 6.0],  # Subprime applicant
    [820, 0.10, 0, 25.0], # Ultra-low risk applicant
    [620, 0.48, 3, 7.5]   # Moderate risk applicant
])

# Labels: 1 = Loan Defaulted, 0 = Fully Repaid
y_default = np.array([0, 1, 0, 1, 0, 1])

gbdt = GradientBoostingClassifier(n_estimators=30, learning_rate=0.1, random_state=42)
gbdt.fit(X_applicants, y_default)

# New Applicant: CIBIL 605, DTI 0.52, 2 loans, Income 6.8 Lakhs
applicant = np.array([[605, 0.52, 2, 6.8]])
default_prob = gbdt.predict_proba(applicant)[0][1]

print(f"Underwriting Risk Score: {default_prob * 100:.2f}%")
print("Underwriting Decision:", "REJECT LOAN APPLICATION" if default_prob >= 0.50 else "APPROVE WITH STANDARD TERMS")
Output: Underwriting Risk Score: 87.45% Underwriting Decision: REJECT LOAN APPLICATION
Real-Time Example 9: Stratified K-Fold Cross-Validation & Model Evaluation
Prevent overfitting and validate model stability across imbalanced datasets using Stratified 5-Fold Cross-Validation, computing Precision, Recall, and ROC-AUC metrics.
Python (Model Evaluation)
from sklearn.model_selection import cross_val_score, StratifiedKFold
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
import numpy as np

# Generate realistic imbalanced classification dataset (90% class 0, 10% class 1)
X_synth, y_synth = make_classification(
    n_samples=200, n_features=5, n_informative=3,
    weights=[0.9, 0.1], random_state=42
)

# 5-Fold Stratified Split preserves minority class distribution across all folds
stratified_cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
estimator = LogisticRegression(random_state=42)

# Compute ROC-AUC scores across all folds
auc_scores = cross_val_score(estimator, X_synth, y_synth, cv=stratified_cv, scoring='roc_auc')

print("Stratified 5-Fold ROC-AUC Scores:", np.round(auc_scores, 3))
print(f"Mean Generalization AUC: {np.mean(auc_scores):.3f} (± {np.std(auc_scores):.3f})")
Output: Stratified 5-Fold ROC-AUC Scores: [0.944 0.889 0.917 0.972 0.917] Mean Generalization AUC: 0.928 (± 0.028)