How can I do ordinal regression using the mord module in python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Ordinal regression is the right tool when your labels have a natural order, such as ratings from low to high, but the gaps between classes are not truly numeric. The mord package gives you lightweight ordinal models that integrate well with NumPy and scikit-learn style workflows.
Prepare Ordered Labels and Features
mord expects the target to be an ordered set of integers. That means your labels should already reflect rank, such as 0, 1, 2, 3, rather than arbitrary category IDs.
If your raw labels are strings such as "poor", "fair", "good", and "excellent", map them to integers in that exact order before training.
Train an Ordinal Model with mord
One common starting point is LogisticIT, which fits an ordinal logistic model with threshold structure between classes.
The scaler is helpful because the model is linear and can be sensitive to feature magnitudes. alpha controls regularization, so it is worth tuning instead of leaving it as an afterthought.
Evaluate with Ordinal-Aware Metrics
Accuracy alone can hide whether mistakes are small or large. In ordinal problems, predicting one class away is usually less serious than jumping three levels.
mean_absolute_error is often a better summary because it respects the order of the classes. You can also report how often predictions are exact, off by one, or off by more than one.
For tuning, a simple cross-validation loop works well:
That is enough to establish a practical baseline.
Compare Against a Baseline
Do not assume an ordinal model is automatically better than a simpler baseline. Compare it with at least a trivial predictor such as the median training class.
If the ordinal model does not beat that baseline, the issue may be the features, not the algorithm.
Common Pitfalls
The most common mistake is using unordered integer labels. If your class IDs were assigned alphabetically or arbitrarily, the model learns the wrong ranking. Encode the labels in their real order.
Another common problem is evaluating only with accuracy. That throws away useful information about how far a prediction missed the target rank.
Feature scaling is also easy to skip. Since mord models are linear, poorly scaled inputs can distort the fit and make regularization harder to tune.
Finally, keep in mind that ordinal regression assumes a sensible ordered structure. If the target classes are not genuinely ordered, use multiclass classification instead.
Summary
- Use ordinal regression when labels are ranked but not truly continuous.
- Encode targets as ordered integers before fitting
mord. - '
LogisticITis a solid starting model, especially with a scaling pipeline.' - Evaluate with ordinal-aware metrics such as mean absolute error, not accuracy alone.
- Always compare against a simple baseline before trusting the model.

