Topic 08 / 8

End-to-End Data Science Project

~25 min read  //  Data Science Series  //  Coding India

The Capstone Project

Learning syntax is only half the battle. Real data science requires combining these tools into a pipeline that delivers business value. In this project, we will build a Customer Churn Predictor.

Step 1: The Pipeline

We’ll use scikit-learn’s Pipeline to chain our preprocessing and modeling steps together. This prevents data leakage and makes our code deployable.

from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer

# Define preprocessing for different columns
preprocessor = ColumnTransformer(transformers=[
    ('num', StandardScaler(), numeric_features),
    ('cat', OneHotEncoder(), categorical_features)
])

# Create the full pipeline
pipeline = Pipeline([
    ('preprocessor', preprocessor),
    ('classifier', RandomForestClassifier())
])

pipeline.fit(X_train, y_train)

Step 2: Model Serialization

Once the model is trained, we need to save it to disk so we can use it in our web application without retraining it.

import joblib

# Save the pipeline
joblib.dump(pipeline, 'churn_model.pkl')

Step 3: Deployment with FastAPI

A model on your laptop is useless. We will wrap our model in a FastAPI application so other services can send it data and receive predictions over HTTP.

from fastapi import FastAPI
from pydantic import BaseModel
import joblib

app = FastAPI()
model = joblib.load('churn_model.pkl')

class CustomerData(BaseModel):
    age: int
    tenure: int
    monthly_charges: float
    contract_type: str

@app.post("/predict")
def predict_churn(data: CustomerData):
    # Convert incoming JSON to a DataFrame row
    df = pd.DataFrame([data.dict()])
    
    # Predict using the loaded pipeline
    prediction = model.predict(df)[0]
    probability = model.predict_proba(df)[0][1]
    
    return {"churn_prediction": int(prediction), "probability": probability}