HOW I BUILT A CHURN PREDICTION API
Someone said churn?
HOW I BUILT A CHURN PREDICTION API
Someone said churn?
Yea churn, churn have a wide range of meaning based on context, but in business, churn is simply the lost of customers over time.
Every businesses survive with customers being involved, someone who needs to use the service, products etc.
I will be putting this into 2 sections:
1. Model Building
2. API building using FastAPI
To get started, here is the github link: https://github.com/Codaks-py/Churn-prediction-Api
Model Building
So I built a churn prediction model for a telecommunication business using a data I sourced from Kaggle. This model built on classification technique, predicts if a customer will churn or not using features which include gender, phone services of the customer, if they have access to multiple cells etc.
To the building, I used pyforest, sklearn and joblib.
Pyforest for using my pandas function, sklearn for the preprocessing, pipeline building and modelling, and joblib to pickle my model so I can use it when building the API later on.

Let’s grab a brief of pyforest:
Pyforest serves like the mother library that houses the pandas, numpy and matplotlib library in it.
A quick guide to set that up:
1. pip install pyforest
*2. from pyforest import **
(you can go ahead and call the pandas and numpy and matplotlib functtion whenever you need to).
With that outta picture
I faced a common problem when dealing with classification models which is something relating with majority and minority categories of the target feature.
In my case, the target feature has 2 values(Yes/No), the data has a shape of (7043, 21), with the majority value having 5173(No) and minority values of 1869(Yes). Building on this data can only allow an high recall of the majority value leading to a biased model.

Which lead me to using Resampling Technique.
Resampling either by upsampling or downsampling.
Upsampling in the sense where the minority values is added to, to be on the same range as the majority value OR Downsampling in the sense where the majority is reduced to be on the same range as the minority value.
Selecting what to use is dependent on what you are building, your conviction, how much data you have etc.
In my case, having a little dataset, downsampling wasn’t a good choice, so I upsampled, and here is how I did the upsampling.

To break it all down, I called the resample function after import it from the onset using
from sklearn.utils import resample
churn_yes = a dataframe for just the ‘Yes’ values from the churn feature and also the data to be resampled.
n_samples = the numbers of samples you want, we want it to be in the same range as the majority values from the churn feature
Replace = fixed at True since we are upsampling, if we are downsampling, you set it to False
Random_state = can be any value you see fit.
Upsampling the dataset added more values, from 7043 values to 10000+ values and also helps tackle the biased effect which might have occurred if I trained the model without resampling.
I then went ahead to splitting the sampled data-set, building a pipeline for easy usage later and training a classification model using RandomForest technique

The model went on to have an accuracy of 0.89% using random forest, 0.85% using logistic regression and based on the classification report, it was able to less biased recall and precision.
I also went ahead to checking the confusion matrix and I got a reasonable high TP and TN based on the test size.

APi Building Using FastAPI
FastAPI is a web framework for building APIs with Python 3.7+ based on standard Python type hints.
Before we get started, a virtual environment is important(and optional but I will advise you create one whenever you are working on a new project).
How you create one:
Open your model folder on bash
cd foldername
Once you are in, run the commands
Python -m venv venvname
Source venvname/scripts/activate
You will have something like this

Once that’s settled,I made sure to install the same version of the libraries i used for building the model.
Actually, I faced quite a few problems on this part due to fact I used sklearn 1.5.1 version when building the model and installed version 1.8.0 in the virtual environment.
Finally, I have all settled and installed all necessary libraries and tools including rust because the latest version of FastAPI and pydantic wouldn’t run except rust was installed. Here is a list of the libraries I installed in the virtual environment.

After of that was settled, I created a python file for loading the model and creating the API.
In this file, I loaded the pickled model (churn_pipe.pkl), created a data scheme to collect features for the model, called the fastapi function and create a function to take this features and make predictions and probability.

Note, I assigned the data type of each features based on what data type the model was trained on.
After all of that was settled, I run the command below on my terminal,
python -m uvicorn api:app — reload
This command runs fastapi locally
Sometimes, you might encounter an issue relating to the site not loading, check the https (remove the ’s’ and also add ‘/docs’ to the ending of the link).
And here is my final output.

In conclusion, I was able to solve a biased model problem and also built an API which makes the model usable for web apps etc.
메타데이터
- post_id
- 3ff3d6e2aa53
- slug
- how-i-built-a-churn-prediction-api-3ff3d6e2aa53
- url
- https://medium.com/@Codaks/how-i-built-a-churn-prediction-api-3ff3d6e2aa53
- canonical_url
- https://medium.com/@Codaks/how-i-built-a-churn-prediction-api-3ff3d6e2aa53
- author_url
- https://medium.com/@Codaks
- status
- ok
- fetched_at
- 2026-07-27 15:36:22