Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← All challenges
ML-X09 · Classification · 30–45 minutes

One-R for categorical decisions

one_r · validation · reference · final-test discipline

Complete workflow and evidence

The question

Use the production One-R helper to classify Car acceptability from categorical inputs. Explain the selected rule and unknown-category fallback.

data/ml-learning/ML-X09.csv Complete workflow and evidence

Your inputs

Challenge input · ML-X09

1728 observations. One categorical car configuration from a decision-model evaluation dataset. Load the CSV file yourself.

Download challenge input CSV · Original source dataset · Source and original dictionary

Ready to load: comma-delimited UTF-8, header row, no saved index. No filtering or target construction is required.

ML-X09 · first 8 prepared rows
buyingmaintenancedoorspersonsluggage_bootsafetyacceptability
vhighvhigh22smalllowunacc
vhighvhigh22smallmedunacc
vhighvhigh22smallhighunacc
vhighvhigh22medlowunacc
vhighvhigh22medmedunacc
vhighvhigh22medhighunacc
vhighvhigh22biglowunacc
vhighvhigh22bigmedunacc

Column meanings and units

Buying/maintenance cost, doors, capacity, luggage and safety are categorical ratings, not observed currency prices.

Acceptability is derived from the source decision model. Performance reproduces that rating system and does not establish real-world safety or consumer preference.

Ready to load: comma-delimited UTF-8, header row, no saved index. No filtering or target construction is required.

Input schema
ColumnStored type
buyingobject
maintenanceobject
doorsobject
personsobject
luggage_bootobject
safetyobject
acceptabilityobject

How we validate this model

Reserve 20% of the declared population for the final test using split seed 42. Keep class proportions with a stratified split.

Use the same five stratified training folds, shuffled with seed 42, for reference comparison, candidates and selection. Use out-of-fold training predictions for diagnosis. Select with macro F1 and compare with a most-frequent-class reference. Accuracy is supplementary. Open final-test evidence after selection and diagnosis.

The file is available in the Python working folder. Load it yourself.

Your task
  1. Use the production One-R helper to classify Car acceptability from categorical inputs.
  2. Explain the selected rule and unknown-category fallback.
modelcv_resultsreference_resultsmatrixfinal_f1final_accuracy

Outputs you create

model
  1. Fitted estimator or pipeline for the requested model family.
cv_results
  1. Candidate validation evidence; comparison workflows use a dataframe indexed by model ID.
reference_results
  1. cross_validate result for the dummy reference; test_score contains five scores.
matrix
  1. Confusion matrix of training-only diagnostic predictions.
final_f1
  1. Macro F1 on final-test class predictions.
final_accuracy
  1. Final-test accuracy, supplementary to macro F1.

Deliverables

These conditions describe a complete workflow. Use the declared Python variables beside the editor; Check inspects evidence from your Run.

Python name key

Teal outlines name table columns. Violet left rules name Python objects. Dotted rules mark syntax, functions and parameters. Use each name exactly as shown.

Words in this task
DataFrame
a table with rows and columns
numeric
a number, rather than text
dummy
a simple reference model used for comparison
folds
training-data groups used to compare models on the same held-out rows
out-of-fold
predictions made for training rows by models that did not fit those rows
aligned
matched by row or category identity, rather than guessed position
residuals
observed values minus predicted values
  1. Data boundary

    1. Load the supplied challenge CSV, define the declared X/y and keep aligned row identities through the split.
    2. Keep learned preparation inside each training fit.
  2. Validation & selection

    1. Compare the requested initial model families with a dummy reference on matching training folds.
    2. Evaluate the declared settings where applicable and choose from training evidence.
  3. Diagnosis

    1. Use out-of-fold training predictions to inspect residuals or class errors before opening the final test.
  4. Final evidence

    1. Fit the nominated model on training rows and evaluate the reserved rows once.
    2. Report saved predictions, the requested metrics and labelled evidence
    3. Explain limitations without using the final test for further selection.

The playground workflow

Use the same data boundary as ML → Workflow: frame, split, explore, prepare, validate against a reference, diagnose, then make the final evaluation. Each step answers one question.

  1. Choose what to predictWhat changes when every predictor is categorical? Use 6 selected inputs to estimate car acceptability.
  2. Split data and save the test setWhich rows are available for learning, and which are being saved for the final check?
  3. Explore training dataDoes buying show a training pattern related to car acceptability?
  4. Define the preparation recipeWhat preparation does this particular model need?
  5. Build the model pipelineWhich single feature gives the simplest useful class rules?
  6. Validate the initial modelDo validation results stay reasonably similar across folds?
  7. Compare with a simple referenceDo the selected features beat a simple guess on the same training folds?
  8. Inspect validation errorsWhich classes are most often confused in training-only validation?
  9. Final testHow does the selected model's saved final car acceptability result compare with the initial-model CV and any selected-setting CV for Car Evaluation · All features categorical? Use the training-only diagnostic or optional final confusion view to identify a class pattern, then name one limitation.

Data policy Keep target-derived inputs out of X. Use training-only selection for prediction.

Help Hints & explained solution

Choose the amount of help you need. Opening one hint does not reveal the others.

Hint 1 — Think

One feature must carry the entire rule.

Hint 2 — Tools

OneRPreprocessor, OneRClassifier, stratified CV and macro F1.

Revisit: One feature, one rule · Finish once, then report

Hint 3 — Approach

Evaluate the learned rule against the reference, inspect its failures and finish without a numeric-bin search.

Explained solution

This complete script replaces the editor contents. It includes the input setup and the workflow.

import numpy as np
import pandas as pd
df=pd.read_csv('data/ml-learning/ML-X09.csv')
from sklearn.base import clone
from sklearn.model_selection import train_test_split, KFold, StratifiedKFold, cross_validate, cross_val_predict, GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer, TransformedTargetRegressor
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.dummy import DummyRegressor, DummyClassifier
from sklearn.metrics import root_mean_squared_error, f1_score, accuracy_score, confusion_matrix

from ml_helpers import OneRClassifier, OneRPreprocessor
numeric = []
categories = ['buying', 'maintenance', 'doors', 'persons', 'luggage_boot', 'safety']
X = df[numeric + categories]
y = df['acceptability']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)
model = Pipeline([('prepare', OneRPreprocessor(numeric_features=numeric, categorical_features=categories)), ('model', OneRClassifier(bins=5))])
folds = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
cv_results = cross_validate(model, X_train, y_train, cv=folds, scoring='f1_macro')
reference_results = cross_validate(DummyClassifier(strategy='most_frequent'), X_train, y_train, cv=folds, scoring='f1_macro')
chosen = model

oof_predictions = cross_val_predict(chosen, X_train, y_train, cv=folds)
matrix = confusion_matrix(y_train, oof_predictions, labels=sorted(y_train.unique()))
final_model = clone(chosen).fit(X_train, y_train)
final_predictions = final_model.predict(X_test)
final_f1 = f1_score(y_test, final_predictions, average='macro')
final_accuracy = accuracy_score(y_test, final_predictions)
print(final_model.named_steps['model'].rules_)
View input setup in this script

These opening lines are included in the complete script above. They load the challenge input.

import numpy as np
import pandas as pd
df=pd.read_csv('data/ml-learning/ML-X09.csv')

Why these steps exist

  • The categorical One-R path learns a single-feature rule with the production unknown-category fallback. It is compared with the majority reference on training folds without an irrelevant numeric-bin search. Error analysis qualifies how much such a simple rule can express.

Another valid approach

Equivalent ordinary Python is welcome. A defensible candidate or grouping need not match the solution’s choice; objective evidence must remain consistent.

Ctrl/⌘+Enter: Run · Tab: indent · Esc then Tab: leave editor

Python loads when you run. Code and results stay in this activity only.

Run your code to inspect its output. Check uses that same run.