python
24 lines · 1 tab
Dr. Elena Vasquez
Apr 2026
1 tab
import joblib
import pandas as pd
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI(title='Churn Prediction API')
model = joblib.load('artifacts/churn_pipeline.joblib')
class PredictionRequest(BaseModel):
age: int
income: float
country: str
plan_tier: str
days_since_last_login: int
@app.get('/health')
def health():
return {'status': 'ok'}
@app.post('/predict')
def predict(payload: PredictionRequest):
frame = pd.DataFrame([payload.model_dump()])
probability = float(model.predict_proba(frame)[:, 1][0])
return {'churn_probability': probability, 'model_version': '2026-04-07'}
1 file · python
Explain with highlit
Deployment should not rewrite the feature logic from scratch. I expose trained pipelines behind FastAPI so the exact preprocessing and estimator objects travel together. Strong request schemas and explicit model versioning keep this boring in the right way.
Related snips
python
import mlflow
import mlflow.sklearn
from sklearn.metrics import roc_auc_score
mlflow.set_experiment('customer-churn')
Experiment tracking and model registry workflows with MLflow
mlflow
experiment-tracking
model-registry
by Dr. Elena Vasquez
1 tab
python
from sklearn.linear_model import LinearRegression, Ridge, Lasso, ElasticNet
from sklearn.metrics import mean_absolute_error, root_mean_squared_error
models = {
'linear': LinearRegression(),
'ridge': Ridge(alpha=1.0),
Regression workflows with linear, ridge, lasso, and elastic net
scikit-learn
regression
ridge
by Dr. Elena Vasquez
1 tab
python
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
Encoding categorical variables without creating leakage
categorical-encoding
preprocessing
scikit-learn
by Dr. Elena Vasquez
1 tab
python
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier
grid_search = GridSearchCV(
estimator=RandomForestClassifier(random_state=42, n_jobs=-1),
param_grid={
Hyperparameter tuning with GridSearchCV and randomized search
hyperparameter-tuning
gridsearch
scikit-learn
by Dr. Elena Vasquez
1 tab
python
from typing import Any, Optional
from fastapi.responses import JSONResponse
from pydantic import BaseModel
Consistent JSON Error Responses in FastAPI With a Custom Exception Handler
fastapi
error-handling
exceptions
by codesnips
3 tabs
python
from typing import Optional
from pydantic import BaseModel, EmailStr, Field, field_validator, model_validator
class CreateUserRequest(BaseModel):
model_config = {"extra": "forbid"}
Validating JSON Payloads with Pydantic v2 and Returning Field-Level Errors in FastAPI
fastapi
pydantic
validation
by codesnips
3 tabs
Share this code
Here's the card — post it anywhere.