indusai.co

The machine learning workflow

A machine learning project is mostly not about the algorithm. It is about asking a clear question, getting the data into shape, checking the model honestly and getting it into use. This lesson walks through the steps in the order you will actually do them.

1. Define the problem

Write one sentence: "Given X, predict Y, so that Z." For example, "Given a customer's last six months of activity, predict whether they will cancel next month, so that support can reach out first."

Decide how success is measured before touching data. For churn, is it accuracy? Catching most churners even at the cost of some false alarms? The metric shapes every later decision.

2. Collect and load data

Data comes from databases, CSV exports, APIs or logs. In Python it nearly always ends up in a pandas DataFrame.

python
import pandas as pd

df = pd.DataFrame({
    "tenure_months": [3, 24, 12, 1, 36, 6],
    "monthly_bill":  [900, 450, 700, 1200, 400, 950],
    "support_calls": [4, 0, 1, 6, 0, 3],
    "churned":       [1, 0, 0, 1, 0, 1],
})
print(df)

3. Explore the data

Look before you model. Shapes, types, missing values, ranges, and how each feature relates to the target.

python
import pandas as pd

df = pd.DataFrame({
    "tenure_months": [3, 24, 12, 1, 36, 6],
    "monthly_bill":  [900, 450, 700, 1200, 400, 950],
    "support_calls": [4, 0, 1, 6, 0, 3],
    "churned":       [1, 0, 0, 1, 0, 1],
})
print(df.describe().round(1))
print(df.groupby("churned").mean().round(1))

Already a pattern: churners have short tenure, high bills and more support calls.

4. Prepare the data

Fix missing values, convert categories to numbers, scale features that are on very different ranges, and create new features that make the pattern easier to see. This is usually the most time consuming step. It has its own lesson.

5. Split the data

Hold back part of the data that the model never trains on. That held out set is the only honest estimate of how the model will do in the real world.

python
from sklearn.model_selection import train_test_split

X = [[3, 900, 4], [24, 450, 0], [12, 700, 1], [1, 1200, 6], [36, 400, 0], [6, 950, 3]]
y = [1, 0, 0, 1, 0, 1]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
print(len(X_train), "train rows,", len(X_test), "test rows")

6. Train a model

Start simple. A linear model or a small tree tells you quickly whether the features carry any signal at all.

python
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier

X = [[3, 900, 4], [24, 450, 0], [12, 700, 1], [1, 1200, 6], [36, 400, 0], [6, 950, 3]]
y = [1, 0, 0, 1, 0, 1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

model = DecisionTreeClassifier(random_state=0).fit(X_train, y_train)
print("test predictions:", model.predict(X_test))
print("actual:          ", y_test)

7. Evaluate

Score the model on the test set with the metric you chose in step 1. Compare against a baseline such as "always predict the most common class". If you cannot beat the baseline, the features or the data need work, not the algorithm.

8. Improve

Iterate: engineer better features, try other algorithms, tune hyperparameters with cross-validation. Keep the test set untouched until the very end so it stays honest.

9. Deploy and monitor

Save the trained model, wrap it in an API or a batch job, and watch its predictions over time. Data drifts: customer behaviour in a year will not match today's training set, so plan to retrain.

python
import pickle
from sklearn.linear_model import LogisticRegression

model = LogisticRegression().fit([[0], [1], [2], [3]], [0, 0, 1, 1])
blob = pickle.dumps(model)
restored = pickle.loads(blob)
print(restored.predict([[2.5]]))

The loop, not the line

In practice you go around steps 3 to 8 many times. Exploration reveals a data problem; fixing it changes the features; the new features suggest a different model. Budget most of your time for data work and evaluation, not for choosing algorithms.

Practice

  1. Write the one sentence problem statement for a project you would like to build, and name the metric you would use.
  2. In the churn example, which of the three features do you expect to matter most? Check by looking at the grouped means.