How can I verify that my deployed XGBoost model is using the correct saved `DictVectorizer`?

Open
#569 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

@github-actions is already working on this.

Since Nov 10, 2026.

  • #570 by @github-actions — open

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
1/100
Issue type
Documentation
Clarity
Clearly specified
Activity status
Stale
Tech stack
python

Research direction

The issue provides a proposed FAQ answer with a Python example using DictVectorizer and XGBoost; no repository file or test is named. Start by checking the FAQ contribution format and whether the open pull request #570 already covers this content. Done means the answer is included in the FAQ and explains that feature counts alone do not verify encoder compatibility.

Written by the indexing model from the issue text.

Description

faq-proposal
Course

machine-learning-zoomcamp

Question

How can I verify that my deployed XGBoost model is using the correct saved DictVectorizer?

Answer

When deploying a machine learning model, it is important to ensure that the preprocessing encoder matches the one used during training.

If you retrain an XGBoost model but accidentally deploy an older dv.bin file, the encoded features may differ from those expected by the model. This can lead to prediction errors or incorrect results.

Solution:

  1. Save the trained XGBoost model and fitted DictVectorizer together during the same training run.
  2. Load both files in the prediction service.
  3. Check that the number of encoded features matches the number expected by the model.
  4. Test the model with the same sample input locally and inside Docker to verify consistent predictions.

Example:

import pickle
import xgboost as xgb

with open("dv.bin", "rb") as f:
    dv = pickle.load(f)

model = xgb.Booster()
model.load_model("model.json")

encoder_features = len(dv.get_feature_names_out())
model_features = model.num_features()

print("Encoder features:", encoder_features)
print("Model features:", model_features)

assert encoder_features == model_features, (
    "Feature count mismatch between model and DictVectorizer!"
)

Important: Matching feature counts alone is not enough to guarantee compatibility. Two different encoders may produce the same number of features but use different feature names or column ordering.

For more reliable deployment:

  • Keep the model and encoder files from the same training run.
  • Avoid refitting the DictVectorizer during prediction.
  • Use a fixed sample input to compare predictions between the original environment and the deployed service.
  • Consider storing the expected feature names or a version identifier alongside the model artifacts.

Key takeaway:

Always deploy the trained model and its fitted preprocessing encoder together. Verifying feature compatibility helps prevent inconsistencies between training and prediction.

Checklist
  • I have searched existing FAQs and this question is not already answered
  • The answer provides accurate, helpful information
  • I have included any relevant code examples or links
Dominant language
Python
Stars
8
Forks
20
Avg merge
4d 9h
Merged PRs (30d)
27

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from DataTalksClub/faq

All issues in DataTalksClub/faq

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.