An End-to-End ML Workflow

Chain every step — load, split, train, evaluate — into one complete example.

Here is the whole journey in one script: load data, split it, train a model, and evaluate it. This is the skeleton of almost every ML project.

The steps

  1. Load and clean the data.
  2. Split into train and test.
  3. Fit a model on the training set.
  4. Evaluate on the test set.

Example

Example · python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42)

model = RandomForestClassifier(random_state=42)
model.fit(X_train, y_train)

preds = model.predict(X_test)
print('accuracy:', accuracy_score(y_test, preds))  # ~0.97

When to use it

  • A data scientist assembles a reproducible end-to-end notebook that any team member can run from raw CSV to final model accuracy report.
  • An ML engineer wraps the full pipeline in a scikit-learn Pipeline object so that preprocessing and model inference always execute together in production.
  • A junior analyst uses an end-to-end template to structure their first customer churn project, ensuring no step (scaling, splitting, evaluation) is accidentally skipped.

More examples

Full pipeline in one script

Chains all ML steps — load, split, scale, train, evaluate — into a single sequential script covering the complete end-to-end workflow.

Example · python
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report

df = pd.read_csv('churn.csv')
X = df.drop(columns=['churned'])
y = df['churned']

X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_tr = scaler.fit_transform(X_tr)
X_te = scaler.transform(X_te)

model = RandomForestClassifier(n_estimators=100, random_state=42).fit(X_tr, y_tr)
print(classification_report(y_te, model.predict(X_te)))

Encapsulate with scikit-learn Pipeline

Wraps preprocessing and the model in a Pipeline so that fit, predict, and score always apply scaling automatically, making deployment safe and reproducible.

Example · python
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

X, y = load_breast_cancer(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2)

pipe = Pipeline([
    ('scale', StandardScaler()),
    ('clf',   GradientBoostingClassifier(n_estimators=100))
])
pipe.fit(X_tr, y_tr)
print('Test accuracy:', pipe.score(X_te, y_te).round(3))

Automated report with classification_report

Prints per-class precision, recall, F1, and support in one call, generating a concise model performance report ready for stakeholder review.

Example · python
from sklearn.metrics import classification_report
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split

X, y = load_digits(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2)
model = RandomForestClassifier().fit(X_tr, y_tr)

print(classification_report(y_te, model.predict(X_te),
      target_names=[str(i) for i in range(10)]))

Discussion

  • Be the first to comment on this lesson.