Machine learning · 2026 · by Luiz Trajano
Gym Churn Prediction and Customer Segments
Who will leave the gym next month, what kinds of customers are there, and what keeps each of them? At a gym, customers rarely cancel: they come a few times, show up less and less, and one day they are gone.
Python · Pandas · scikit-learn · SciPy · Seaborn
Origin: TripleTen Data Analyst track (sprint 13). Brief and data provided by the course; analysis, code and conclusions are mine.
The question
Model Fitness, a gym chain, wanted to act before losing a customer. The catch is that almost nobody at a gym cancels explicitly: people come less and less and one day they stop. By the time the gym notices, they are gone. The work had three parts: predict who leaves next month, find out what kinds of customers the base holds, and turn that into concrete retention actions.
The data
Profiles of 4,000 customers across 14 columns: contract (length, months left, tenure), attendance
(weekly average overall and in the current month), spend on extra services, profile (age, gender,
whether they live nearby, came through a referral or work at a partner company) and the Churn
column, which marks who left in the month. No missing values, no duplicates. 1,061 customers left,
26.5% of the base.
Before any model, I found three things that change how the data reads:
- The 3-month contract mentioned in the brief does not exist. There are only 1, 6 and 12-month contracts, so any average contract like "3.16 months" is a mix, not anyone's actual contract.
- Two pairs of columns are near twins: contract length and months left (r = 0.97), and overall versus current-month attendance (r = 0.95). That distorts how a linear model's coefficients read.
- One column is a leakage suspect. Of the 122 customers who stopped coming in the current month, 116 left (95.1%). If the "current month" is the month they left, that column hands the model the answer.
Method, and why this method
I split training and validation with a stratified split, so the share of customers who left stays
the same (26.5%) in both, and fixed random_state everywhere. The yardstick came before any model: a
"nobody leaves" guess scores 73.5% accuracy and finds none of the customers who left. With rare
churn, accuracy alone misleads, so I also compared precision, recall, F1 and ROC-AUC.
I trained a logistic regression (scaling fitted on the training set only) and a random forest. I declared up front that differences up to 0.02 count as noise. The forest came out nominally ahead on four metrics, but by at most 0.010: a tie. To test the leakage, I retrained both models without the suspect column. I chose the logistic regression because, without that column, it finds more of the customers who leave (78.8% against 75.9%), it is simpler, and its coefficients can be read.
For the segments, I dropped Churn before clustering, so the groups come from the profile and not
from the answer, standardised everything and ran K-Means with 5 clusters. The dendrogram suggested 4
and the silhouette was weak (0.128), so I read each group as a tendency, not a hard category. K-Means
also gave different results across scikit-learn versions; I fixed the starting point and documented
the comparison by inertia, so the groups are the same in any environment.
Findings
The customer who leaves is new, on a short contract, with falling attendance. Those who left had 0.99 months of tenure on average, against 4.71 for those who stayed. 42.3% of month-to-month customers left, against 2.4% of annual-contract customers.
The model finds who is about to leave. The logistic regression is right about 92.5% of the validation customers and finds 176 of the 212 who left (83.0%), with 88.0% of its flags correct. Without the suspect column, recall drops to 78.8%: the column matters, but it does not carry the model alone. In both models, the factors that weigh most are tenure and current-month attendance.
Five kinds of customer, with very different risks, as the chart above shows. The low-engagement group (1,108 customers) lost 52.6% and those who live far away (544) lost 45.0%: together, the two groups hold 828 of the 1,061 people who left. Annual-contract customers lost only 2.2% and regulars 6.9%. Customers referred by a friend sit near the average (24.8%): a referral helps, but on its own it does not keep them.
The mean profile of each group, the same table as section 4.4 of the notebook, shows where each name comes from: group 0 is the only one where nobody lives nearby, group 1 holds the annual contracts, group 2 is 100% referrals, group 3 is the lowest on almost everything and group 4 attends the most.
| cluster | 0Live far away | 1Annual contract | 2Referred | 3Low engagement | 4Regulars |
|---|---|---|---|---|---|
| gender | 0.50 | 0.50 | 0.48 | 0.49 | 0.59 |
| Near_Location | 0.00 | 0.94 | 1.00 | 1.00 | 0.97 |
| Partner | 0.47 | 0.74 | 0.82 | 0.24 | 0.26 |
| Promo_friends | 0.08 | 0.49 | 1.00 | 0.02 | 0.09 |
| Phone | 0.92 | 0.90 | 0.90 | 0.90 | 0.90 |
| Contract_period | 2.21 | 11.87 | 3.16 | 1.99 | 2.81 |
| Group_visits | 0.22 | 0.55 | 0.45 | 0.32 | 0.48 |
| Age | 28.48 | 29.93 | 29.22 | 28.21 | 30.14 |
| Avg_additional_charges_total | 133.63 | 164.87 | 141.20 | 131.19 | 162.01 |
| Month_to_end_contract | 2.07 | 10.87 | 2.92 | 1.90 | 2.59 |
| Lifetime | 2.78 | 4.70 | 3.71 | 2.40 | 5.13 |
| Avg_class_frequency_total | 1.66 | 2.00 | 1.73 | 1.27 | 2.88 |
| Avg_class_frequency_current_month | 1.47 | 1.99 | 1.63 | 1.03 | 2.88 |
The decision it enables
Four retention principles, each with a practical step. The first month decides: a welcome with a fitness assessment and a call from the instructor at the end of the first month. Long contracts hold: offer the 6 or 12-month plan in the month before a monthly contract ends, starting with the highest-churn groups. Falling attendance is the alarm: run the model trained without the suspect column every month and contact the customers most likely to leave. Social ties help people stay: group classes and a perk for referring a friend.
These principles come from association, not causation, so each action should first be tested against a randomised control group. The project was approved by the TripleTen reviewer.