Luiz

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.

Sprint 13 - Churn Model Fitness.ipynb — luiz19190/Sprint-13-Churn-model-fitness (4000 customers, K-Means with 5 clusters on standardised features); churn rate per cluster from section 4.6

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.

cluster0Live far away1Annual contract2Referred3Low engagement4Regulars
gender0.500.500.480.490.59
Near_Location0.000.941.001.000.97
Partner0.470.740.820.240.26
Promo_friends0.080.491.000.020.09
Phone0.920.900.900.900.90
Contract_period2.2111.873.161.992.81
Group_visits0.220.550.450.320.48
Age28.4829.9329.2228.2130.14
Avg_additional_charges_total133.63164.87141.20131.19162.01
Month_to_end_contract2.0710.872.921.902.59
Lifetime2.784.703.712.405.13
Avg_class_frequency_total1.662.001.731.272.88
Avg_class_frequency_current_month1.471.991.631.032.88
Mean of each feature per cluster. Green: the highest value in the row; purple: the lowest. Sprint 13 - Churn Model Fitness.ipynb — luiz19190/Sprint-13-Churn-model-fitness, section 4.4: mean of each feature per K-Means cluster (original units; binary columns are shares)

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.

Links

Repository