ordinal regression
mord module
Python
machine learning
statistical analysis

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.

python
1import numpy as np
2from sklearn.model_selection import train_test_split
3
4X = np.array([
5    [1.0, 0.8],
6    [1.2, 0.7],
7    [2.0, 1.1],
8    [2.2, 1.3],
9    [3.0, 1.9],
10    [3.4, 2.1],
11    [4.0, 2.8],
12    [4.3, 3.0],
13])
14
15y = np.array([0, 0, 1, 1, 2, 2, 3, 3])
16
17X_train, X_test, y_train, y_test = train_test_split(
18    X, y, test_size=0.25, random_state=42
19)

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.

python
1from mord import LogisticIT
2from sklearn.pipeline import Pipeline
3from sklearn.preprocessing import StandardScaler
4
5model = Pipeline([
6    ("scale", StandardScaler()),
7    ("ord", LogisticIT(alpha=1.0)),
8])
9
10model.fit(X_train, y_train)
11predictions = model.predict(X_test)
12
13print("predictions:", predictions)

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.

python
1from sklearn.metrics import accuracy_score, mean_absolute_error
2
3accuracy = accuracy_score(y_test, predictions)
4mae = mean_absolute_error(y_test, predictions)
5
6print("accuracy:", accuracy)
7print("ordinal MAE:", mae)

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:

python
1from mord import LogisticIT
2from sklearn.model_selection import KFold
3
4alphas = [0.01, 0.1, 1.0, 10.0]
5kfold = KFold(n_splits=4, shuffle=True, random_state=42)
6
7best_alpha = None
8best_mae = float("inf")
9
10for alpha in alphas:
11    fold_scores = []
12    for train_idx, valid_idx in kfold.split(X):
13        candidate = LogisticIT(alpha=alpha)
14        candidate.fit(X[train_idx], y[train_idx])
15        fold_pred = candidate.predict(X[valid_idx])
16        fold_scores.append(mean_absolute_error(y[valid_idx], fold_pred))
17
18    avg_score = float(np.mean(fold_scores))
19    if avg_score < best_mae:
20        best_mae = avg_score
21        best_alpha = alpha
22
23print("best alpha:", best_alpha)
24print("best cv mae:", best_mae)

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.

python
baseline = np.full_like(y_test, fill_value=int(np.median(y_train)))
print("baseline MAE:", mean_absolute_error(y_test, baseline))

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.
  • 'LogisticIT is 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.

Course illustration
Course illustration

All Rights Reserved.