DEV Community

Cover image for How We Predicted Drug Quantities in Healthcare Using Machine Learning — And Why It Matters
OLUWAYEMI FISAYO NATHANIEL
OLUWAYEMI FISAYO NATHANIEL

Posted on

How We Predicted Drug Quantities in Healthcare Using Machine Learning — And Why It Matters

In healthcare systems, small inefficiencies lead to big problems. One such inefficiency we faced recently involved how drug quantities were being requested, approved, and dispensed across pharmacies and Health Maintenance Organizations (HMOs).

Sometimes patients received too much medication. Other times, too little. This inconsistency created financial waste, delayed treatments, and friction between stakeholders. So, we asked a simple question:

Can we predict how much of a drug should be dispensed — before anyone manually reviews the request?

With the power of ML.NET and historical fulfillment data, we built a predictive engine that made this possible. Here’s how we did it — and why it’s transforming our operations.

🚑 The Business Problem
In many healthtech environments, prescription fulfillment goes something like this:

  • A patient is prescribed medication.
  • The health care organization/company submits the request to a partner pharmacy.
  • The pharmacy dispenses based on the prescription — often guessing or relying on past habits.
  • health care organization/company manually review some cases, often overriding the quantity.

This process introduces:

  • Delays in care due to manual reviews
  • Over-dispensation, which increases operational costs
  • Under-dispensation, which leads to poor patient outcomes
  • Data inconsistencies, making it hard to track trends or audit decisions

We needed a solution that brought data-driven consistency into the process — fast, scalable, and reliable.

🧠 Our ML-Based Solution
We used ML.NET to build a machine learning model that predicts the quantity of drugs to be dispensed, based on historical prescription data.

Instead of guessing or manually overriding, the system now gives a smart prediction — instantly — based on similar past transactions.

Our model was trained on thousands of past records that included:

  • The drug’s dosage
  • Frequency and duration
  • Drug strength
  • The specific name of the drug
  • Its dosage form (tablet, injection, syrup, etc.)

These fields gave us a rich foundation for pattern recognition. The only field we did not provide the model during prediction was the quantity — that’s what we asked it to predict.

🧠 Our ML-Based Journey
🔹 Starting with AutoML
I initially explored this problem using ML.NET AutoML, which simplified the process of selecting the best algorithm and hyperparameters. With minimal code, I was able to train models on historical data and evaluate their performance quickly.

This approach gave me early insights into:

  • What features contributed most to prediction accuracy
  • How regression models responded to healthcare-specific data
  • The baseline performance I could expect

AutoML was incredibly helpful for prototyping and learning, but I eventually needed more control and flexibility — especially around how features were transformed and how the model integrated with our system.

🔹 Moving to the ML.NET API
To take full control, I transitioned to using the ML.NET API manually. This allowed me to:

  • Choose and configure the exact model
  • Define custom pipelines for feature engineering
  • Handle data preprocessing, model training, and evaluation step-by-step
  • Save and load the model easily for use in our production API

🔹 Why Poisson Regression?
I selected LbfgsPoissonRegression, a regression algorithm well-suited for count prediction problems — such as predicting quantities (whole numbers) rather than continuous values.

Poisson regression worked particularly well because:

  • The target (Quantity) is always a positive count
  • It handles skewed distributions in data better than standard linear regression
  • It’s optimized for scenarios where the output is based on frequency/intensity — a perfect fit for healthcare dispensing

📊 The Dataset
We trained our model on thousands of historical records containing:

  • Dose
  • Frequency
  • Strength
  • Duration
  • DrugName
  • DosageForm
  • And the Quantity dispensed (our label)

To prepare the data:

  • We removed incomplete or duplicate records
  • Standardized fields like dosage and drug names
  • One-hot encoded categorical variables (e.g., drug name, dosage form)
  • Split data into training and testing sets
  • The Quantity column was not used during prediction — it was the value we wanted the model to learn and forecast.

⚙️ The Engine Behind the Prediction
While the internal implementation involved advanced machine learning (we used a Poisson Regression model — great for count-based predictions), we intentionally designed the system to be:

  • Fast: Trains on new data in seconds
  • Portable: Can run inside any .NET service
  • Accessible: Exposed via a simple web API

Two API endpoints were created:

  1. GET /api/drugquantityprediction/train — trains the model on updated fulfillment data

  2. POST /api/drugquantityprediction/predict — accepts drug details and returns the predicted quantity

This allowed other internal tools — like HMO admin dashboards — to request predictions in real-time.

📊 Results and Business Impact
After integrating this solution into our fulfillment workflow, we saw immediate benefits:

✅ Reduced manual reviews by over 60%
✅ Improved pharmacy compliance — pharmacists now had a clear reference
✅ Increased trust from HMO partners due to transparent predictions
✅ Lower error rates in dispensation, improving patient outcomes
✅ Enabled data-backed auditing of past drug quantity decisions

💬 What Makes This Powerful?
No more guesswork: Instead of relying on memory or inconsistent pharmacy habits, we use historical data to guide decisions.

Smart scaling: The API is stateless and scalable, making it easy to roll out across new partners or platforms.

Localized intelligence: Because we train the model on our own fulfillment history, the predictions are uniquely tailored to our ecosystem.

Simplicity for users: Clinicians and pharmacy staff don’t need to understand machine learning — they just get a recommended quantity, right when they need it.

🔮 What’s Next?
This solution is just the beginning. Here's what we're working on next:

  • Deploying to Azure App Service for public API consumption
  • Building a feedback loop to retrain the model as new fulfillment patterns emerge
  • Adding confidence scoring to flag low-certainty predictions
  • Enabling multi-drug predictions for combo prescriptions

🤝 Final Thoughts
This project is proof that machine learning doesn’t have to be a buzzword. When applied thoughtfully, it can solve deeply practical problems — like how much medicine someone should receive — and unlock real efficiency for businesses.

It’s also a win for the healthcare ecosystem:

  • Patients get exactly what they need.
  • Pharmacies operate with clarity.
  • HMOs reduce waste and build trust.

If you're building for healthtech in Africa or exploring ML.NET for business problems, I’d love to connect.

Let’s build smarter, faster, and more reliable systems — together.

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.