This project builds a logistic regression-based machine learning pipeline to predict refund requests for UK rail journeys using the MavenRail dataset. It includes data preprocessing, model training, and inference, with a FastAPI service for serving the model via API endpoints.
Access the endpoint here - https://mavenrail.onrender.com/
The MavenRail dataset originates from a fictional dataset developed for a data visualization competition hosted by Maven Analytics. It covers UK rail journeys from January 1 to April 30, 2024.
Dataset Source: Kaggle - MavenRail
- Purchase Type: Online or Station
- Payment Method: Contactless, Credit Card, or Debit Card.
- Railcard: Adult, Disabled, Senior, or empty if not used.
- Ticket Class: Standard or First Class.
- Ticket Type: Advance, Off-Peak, or Anytime.
- Price: The cost of the ticket in pounds (£).
- Departure Station: One of the following major UK train stations: London Paddington, London Kings Cross, Liverpool Lime Street, London Euston, York, Manchester Piccadilly, Birmingham New Street, London St Pancras, Oxford, Reading, Edinburgh Waverley, Bristol Temple Meads.
- Arrival Destination: One of the following stations: Liverpool Lime Street, York, Manchester Piccadilly, Reading, London Euston, Oxford, Durham, London St Pancras, Birmingham New Street, London Paddington, Bristol Temple Meads, Tamworth, London Waterloo, Sheffield, Wolverhampton, Leeds, Stafford, Doncaster, Swindon, Nottingham, Peterborough, Edinburgh, Crewe, London Kings Cross, Leicester, Nuneaton, Didcot, Edinburgh Waverley, Coventry, Wakefield, Cardiff Central, Warrington.
- Arrival Time: The scheduled arrival time, formatted as
HH:mm:ss. - Actual Arrival Time: The actual arrival time, also formatted as
HH:mm:ss. - Journey Status: On Time, Delayed, or Cancelled.
- Reason for Delay: If delayed, the reason is specified as one of: Signal Failure, Technical Issue, Weather Conditions, Weather, Staffing, Staff Shortage, Signal failure, Traffic.
- Refund Request: Whether a refund was requested (Yes/No).
conda create --name mavenrail_venv python=3.12
conda activate mavenrail_venvconda env export | grep -v "^prefix: " > environment.ymlpython -m src.preprocess --filename TrainRides.csv --test_size 0.1 --random_state 42
If you want to specify which columns to use for training, you can pass them using the --columns argument:
python -m src.preprocess --filename TrainRides.csv --test_size 0.1 --random_state 42 --columns col1 col2 col3If --columns is not specified, the model will be trained on all the features mentioned above.
python -m src.train
python -m src.inference
pre-commit run --all-files
uvicorn main:app --reload
Open your browser and go to: http://127.0.0.1:8000/
Use this JSON payload in Swagger or a tool like Postman:
{
"Purchase Type": "Online",
"Payment Method": "Contactless",
"Railcard": "Adult",
"Arrival Time": "13:30:00",
"Actual Arrival Time": "13:30:00",
"Ticket Class": "Standard",
"Ticket Type": "Advance",
"Price": 43,
"Departure Station": "London Paddington",
"Arrival Destination": "Liverpool Lime Street",
"Journey Status": "On Time",
"Reason for Delay": "Not Delayed"
}
This project provides a functional machine learning pipeline for predicting refund requests using logistic regression. It includes data preprocessing, training, and API-based inference with the flexibility to choose input features. However, there are areas where it can be further enhanced:
- Expanding Model Options: Currently, the model uses logistic regression. Exploring additional models like Random Forest, XGBoost, or Neural Networks could improve performance.
- Advanced Feature Selection: The pipeline allows selecting input features, but further refinement using techniques like correlation analysis, feature importance scores, or PCA could enhance model efficiency.
- Dynamic Input Validation: FastAPI currently treats all features as mandatory. A more sophisticated approach could dynamically enforce mandatory and optional fields based on model training insights.
- Hyperparameter Optimization: The model is trained with default parameters. Tuning hyperparameters using methods like Grid Search or Bayesian Optimization could yield better results.
This project serves as a foundation for a more advanced refund prediction system, and future improvements can significantly enhance its accuracy and usability.