Customer Churn Classification
You want to predict which customers will leave from their tenure, monthly charges and support calls, and you want an honest measure of how well the prediction works. This flow holds back a quarter of the rows, trains a Random Forest on the rest, scores the held-back rows and computes accuracy, precision, recall and F1. Every trained model is logged to MLflow, and the flow exports as a runnable scikit-learn script.
ML nodes are in the Machine Learning group of the node palette. See the ML Quick Start for setup.
You'll use: File Input → Train / Test Split → Train Classifier → Predict → Evaluate, with two File Outputs.
- InputFile Inputcustomers.csv
- MLTrain /
Test Split test 0.25 · stratify churn · seed 42 - MLTrain ClassifierRandom Forest on the train output
- MLPredicttest output + model wire
- MLEvaluateaccuracy · precision · recall · f1
- OutputFile Outputmetrics.csv
- OutputFile Outputtest_predictions.csv, a second branch from Predict
Sample data
Save this as customers.csv, or download customers.csv,
and upload it on the Datasets page:
customer_id,tenure,monthly_charges,support_calls,churn
1,2,79.9,4,1
2,34,55.1,0,0
3,5,99.0,3,1
4,48,42.3,1,0
5,9,88.5,5,1
6,60,38.0,0,0
7,3,72.4,2,1
8,41,49.9,1,0
9,4,85.0,3,1
10,55,40.5,0,0
11,7,91.2,4,1
12,38,47.8,1,0
13,6,77.5,2,1
14,50,44.0,0,0
15,2,95.0,5,1
16,45,39.9,0,0churn is the target: 1 means the customer left. Half the customers churned.
Why 16 rows
Training needs at least 10 rows after the test rows are set aside. With fewer,
Train / Test Split stops the run with the training set has only N rows; at least 10 are required. Sixteen rows with a test size of 0.25 leave 12 for
training.
Build the flow
- File Input: File type CSV, dataset
customers.csv. - Train / Test Split: Test size
0.25, Stratify bychurn, Random seed42. The node has two outputs: the top handle is train, the bottom handle is test. - Train Classifier: connect the train output. Set Model to
Random Forest, Target column
churn, Feature columnstenure,monthly_charges,support_calls, and Random seed42. The node emits a model wire. - Predict: connect the test output to its data input and the model
wire to its model input. Prediction column stays
prediction. - Evaluate: connect Predict. Task type Classification,
True value column
churn, Prediction columnprediction. - File Output: connect Evaluate, Dataset name
metrics. - File Output: connect Predict as well, Dataset name
test_predictions. This lets you read each test prediction next to the true value.
Click Run. The Models page shows the trained model logged in MLflow
with its training-set scores, train_accuracy and train_f1_weighted. Those
measure the rows the model learned from. The held-out scores from Evaluate are
in metrics.csv; Evaluate does not log them to MLflow.
Advanced options on the Train Classifier node holds the hyperparameters, the in-model preprocessing and the MLflow experiment name. Use Classifier Model with Cross-Validate when you want fold scores before you train a final model.
Prefer to describe it?
With a provider key set up, upload the sample data, open a blank flow in the same project and ask the Flow assistant, for example: "train a classifier on the churn column with a train/test split and evaluate it on the test rows." Review the proposed nodes and settings, then choose Apply and save. We ran this prompt on the sample data; see what the assistant built. Its flow can differ from the steps above in node order and names, so check each setting. The assistant never runs a flow, so click Run yourself.
What the split protects against
A model scored on rows it trained on looks better than it is: it can memorize
them. Train / Test Split puts 4 of the 16 customers aside before training, and
only Predict and Evaluate see them. Stratify by churn keeps the 50/50
balance in both parts: 6 churned and 6 stayed in training, 2 and 2 in test.
The Random seed fixes which rows go where, so every run uses the same split and the metrics are comparable from run to run.
Why the feature columns are set
If you leave Feature columns empty, the model trains on every column except
the target, and that includes customer_id. In this sample every odd ID
churned and every even ID stayed. A model could score perfectly by learning
"odd ID means churn", which says nothing about real customers.
Listing tenure, monthly_charges and support_calls keeps the ID out.
Predict ignores columns the model did not train on, so customer_id still comes
through to the output for reference.
Result
Predict adds a prediction column to the four test rows:
| customer_id | tenure | monthly_charges | support_calls | churn | prediction |
|---|---|---|---|---|---|
| 8 | 41 | 49.9 | 1 | 0 | 0 |
| 3 | 5 | 99.0 | 3 | 1 | 1 |
| 14 | 50 | 44.0 | 0 | 0 | 0 |
| 13 | 6 | 77.5 | 2 | 1 | 1 |
Evaluate turns them into a metric / value table:
| customer_id | churn | prediction |
|---|---|---|
| 8 | 0 | 0 |
| 3 | 1 | 1 |
| 14 | 0 | 0 |
| 13 | 1 | 1 |
| metric | value |
|---|---|
| accuracy | 1 |
| precision | 1 |
| recall | 1 |
| f1 | 1 |
These are the values we got with seed 42 (scikit-learn 1.9). The sample is built to separate cleanly: churners have short tenure, high charges and more support calls. Read the perfect score as "the flow is wired correctly", not as model quality. With four test rows, one wrong prediction drops accuracy to 0.75. Precision, recall and F1 are weighted averages over both classes.
Check the result
test_predictions.csvhas 4 rows, customers 8, 3, 14 and 13: two withchurn1 and two with 0. None of them appears in the training rows.metrics.csvhas four rows:accuracy,precision,recall,f1.- On the Models page, the run lists
train_accuracyandtrain_f1_weighted, scored on the 12 training rows, not the test rows.
Exported Python
Click Export in the editor toolbar and open the pandas tab. The generated script covers the whole flow:
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.ensemble import RandomForestClassifier
from sklearn.impute import SimpleImputer
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import StandardScaler
df_customers = pd.read_csv('customers.csv')
df_train, df_test = train_test_split(df_customers, test_size=0.25, random_state=42, stratify=df_customers['churn'])
df_train = df_train.reset_index(drop=True)
df_test = df_test.reset_index(drop=True)
features = ['tenure', 'monthly_charges', 'support_calls']
_numeric = [c for c in features if pd.api.types.is_numeric_dtype(df_train[c])]
_categorical = [c for c in features if c not in _numeric]
_preprocessor = ColumnTransformer([
('num', Pipeline([
('impute', SimpleImputer(strategy='median')),
('scale', StandardScaler()),
]), _numeric),
('cat', Pipeline([
('impute', SimpleImputer(strategy='most_frequent')),
('encode', OneHotEncoder(handle_unknown='ignore')),
]), _categorical),
])
model = Pipeline([
('preprocessor', _preprocessor),
('model', RandomForestClassifier(random_state=42)),
])
model.fit(df_train[features], df_train['churn'])
df_predictions = df_test.copy()
df_predictions['prediction'] = model.predict(df_test[features])
_metrics = {
'accuracy': accuracy_score(df_predictions['churn'], df_predictions['prediction']),
'precision': precision_score(df_predictions['churn'], df_predictions['prediction'], average='weighted', zero_division=0),
'recall': recall_score(df_predictions['churn'], df_predictions['prediction'], average='weighted', zero_division=0),
'f1': f1_score(df_predictions['churn'], df_predictions['prediction'], average='weighted', zero_division=0),
}
df_metrics = pd.DataFrame([{'metric': k, 'value': v} for k, v in _metrics.items()])
df_predictions.to_csv('test_predictions.csv', index=False)
df_metrics.to_csv('metrics.csv', index=False)Run it next to customers.csv and both output files match the tables above.
The script does not need Ciaren, but it needs pandas and scikit-learn
installed. The preprocessing (median imputation and standard
scaling) is part of the Pipeline, so it is reapplied with the training values
when predict runs. Variables follow Ciaren's naming: the input frame is named
after the dataset, the split outputs are df_train and df_test, the fitted
pipeline is model with its feature list in features, the scored frame is
df_predictions and the metrics are df_metrics. _-prefixed names hold
intermediate values.
Variations
- Another model: pick Logistic Regression, Support Vector Machine,
K-Nearest Neighbors or another option in Model; the rest of the flow
stays the same. XGBoost and LightGBM need the
mlextra. - Regression: use Train Regressor with a numeric target and set Evaluate's Task type to Regression (RMSE, MAE, R²).
- Which features matter: connect a Feature Importance node to the model wire.
- Retrain regularly: attach a schedule.
Next steps
- Feature Engineering: scale, encode and reduce features
- ML Nodes Reference
- ML Quick Start