A compact machine learning project that trains a drug-prescription prediction model from patient health metrics and exposes the model through a FastAPI endpoint.
- Trains a decision tree regression pipeline on the
drug200.csvdataset. - Encodes categorical patient features and predicts the most likely prescribed drug.
- Serves inference requests through a REST-style FastAPI endpoint.
- Returns a clear JSON response with input values and the recommended drug.
drug200.csv— dataset containing patient features and prescribed drug labels.ml_drug_withAPI.py— training script that builds the preprocessing + model pipeline and exports two artifacts:drug_pipeline_model.pkllabel_encoder.pkl
main.py— FastAPI application that loads the saved artifacts and exposes a path-based inference route.
- Install dependencies
pip install fastapi uvicorn pandas scikit-learn joblib- Train the model (if artifacts are not already present)
python ml_drug_withAPI.py- Start the API server
uvicorn main:app --reload- Call the prediction endpoint
Open in browser or use curl:
http://127.0.0.1:8000/docs{
"status": "Success",
"extracted_path_parameters": {
"Age": 45,
"Sex": "M",
"BP": "HIGH",
"Cholesterol": "NORMAL",
"Na_to_K": 18.5
},
"recommended_drug": "DrugY"
}Replace
DrugYwith the actual model recommendation from your dataset.
- The API uses path parameters for inference input.
- The model expects
Sex,BP, andCholesterolvalues in uppercase format. Na_to_Kmust be a positive float.
This repository demonstrates how to connect a trained Scikit-learn pipeline to a lightweight web API, making drug-prescription predictions available for real-time use.