liveBook · Manning

Ordinal data

As explained in Starting Data Analytics with Generative AI and Python

An ordinal variable is a categorical variable whose values have a meaningful order but not necessarily equal intervals.

Ordinal data records categories with a meaningful order, while the gaps between values are not necessarily equal. A 1–5 review score is ordinal: five ranks above four, but not necessarily by the same amount. section 3.4.3

Use ordinal data carefully in an analysis workflow:

  • Prepare a merged DataFrame containing review score, product category, delivery time, price, and product attributes. section 3.4.3
  • Model the ordered review scores with ordinal logistic regression rather than treating them as ordinary numerical measurements. section 3.4.3
  • Evaluate predictions with accuracy, a confusion matrix, precision, recall, and F1-score, then inspect feature coefficients. section 3.4.3
  • Challenge generated assumptions such as an unexplained analysis_df, and verify joins, missing values, and encoded categories manually. section 3.4.3

Review-score prediction

Predict 1–5 customer review scores from delivery time, price, freight value, product attributes, and payment variables. A sample mord.LogisticAT workflow reports accuracy, a confusion matrix, and coefficients connected to feature names. section 3.4.3

import pandas as pd
import mord
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, accuracy_score

# Prepare the data (assuming you have already merged necessary 
DataFrames into analysis_df)

# Convert categorical variables to numerical format if required
# (e.g., using pd.get_dummies() for product categories)

# Split the data into training and testing sets
X = analysis_df.drop("review_score", axis=1)
y = analysis_df["review_score"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, 
random_state=42)

# Fit the ordinal logistic regression model
model = mord.LogisticAT(alpha=0)
model.fit(X_train, y_train)

# Predict the review scores on the test data
y_pred = model.predict(X_test)

# Calculate the accuracy of the predictions
print("Accuracy:", accuracy_score(y_test, y_pred))

# Display the confusion matrix
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))

# Analyze the model's coefficients
print("Coefficients:\n", model.coef_)

Inspect the Python workflow for splitting data, fitting mord.LogisticAT, predicting review scores, and calculating accuracy and a confusion matrix. section 3.4.3

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Defines ordinal review scores and demonstrates regression preparation, fitting, prediction, evaluation, coefficient inspection, and assumption checking.
Examples
Uses 1–5 review scores, merged product and order records, Python predictions, confusion matrices, and named feature coefficients.
What it assumes
Assumes tabular data and DataFrame handling, while teaching data merging, categorical encoding, ordinal regression, and validation checks.
What you can do after
Diagnose ordinal-score predictions with a fitted model, evaluation metrics, confusion matrices, and coefficient-to-feature mappings.
How it connects
Connects ordinal data with categorical variables, ordinal logistic regression, review scores, product categories, delivery time, and model evaluation.

Also explained in

Compare how 8 books explain ordinal data →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.