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







