Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Classification lessonsQUESTIONS · MODELS · EVIDENCE
Margins and rules · ML-C12 · 12–18 MIN

One feature, one rule

Understand the production One-R rule and fallback.

Exercises within this concept

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. TransferExplain the Python result

Understand the idea

One-R evaluates single-feature rules and chooses one with the fewest training errors. Production preserves discrete feature metadata and uses a majority fallback for unknown rule values.

Understand the production One-R rule and fallback.One selected feature → majority rule for each valueServiceRule predictionstandardon timeexpresson timeeconomylateSelect by training errors; unseen values use a fallback.
Schematic · Understand the production One-R rule and fallback.Scroll the diagram horizontally if needed.

Python skill: Preserves declared categorical values and learns bins only for declared numeric inputs.

Meet the syntax

OneRPreprocessor(...)
OneRClassifier(bins=5)
model.named_steps["model"].rules_
OneRPreprocessor
Preserves declared categorical values and learns bins only for declared numeric inputs.
OneRClassifier(bins=5)
Creates the production one-feature rule learner; bins matters when numeric inputs need discretisation.
model.named_steps["model"].rules_
Reads the learned value-to-label mapping for the selected One-R feature.

Follow the code

Use the numbered comments to connect each Python block to the workflow above.

answer=model.named_steps['model'].rules_

This practice: Read and run the Python. Next: Change · One feature, one rule.

One-R classification · from idea to workflow

Question: How well can one input alone assign a class?

Mechanism: Learn one feature’s category or interval rule and choose it using training evidence.

Watch for: Interactions are ignored; rare categories or unstable cut points can mislead.

Interpret or debug · self-review: Inspect which feature won. Explain why a rule learned before the split leaks information even if the final classifier uses only one feature.

Independent full workflow: ML-X09 · ML-X10. First practise complete regression and classification in F11–F12; check readiness in W-K2–W-K3.

Given data · RULE24

24 observations. Deterministic teaching observations; values illustrate the concept rather than a real population claim. The dataframe df is supplied afresh for each Run.

RULE24 · first 8 prepared rows
distanceservicefragilelabel
1standard0on time
2express1on time
3economy0late
4standard1on time
5express0on time
6economy1on time
7standard0on time
8express1on time

Column meanings and units

Column names describe the supplied features and target. Keep the stated units and row identities when making comparisons.

Synthetic data are deliberately small and reproducible. Their patterns illustrate an idea; they are not evidence about a real population.

Input schema
ColumnStored type
distanceint64
servicestr
fragileint64
labelstr
Supplied setup · available if you need to inspect it

This code runs before your editor on every Run. These are the objects your exercise uses.

from ml_helpers import OneRClassifier,OneRPreprocessor
from sklearn.pipeline import Pipeline
X=df[['service','fragile']]
y=df.label
model=Pipeline([('prepare',OneRPreprocessor(numeric_features=[],categorical_features=['service','fragile'])),('model',OneRClassifier())]).fit(X,y)

Your task · Follow

Store the fitted One-R classifier’s rules_ dictionary in answer.

Hint 1 — Think

One-R retains a single selected feature’s mapping rather than combining all inputs.

Hint 2 — Tools

The fitted One-R step and rules_.

Hint 3 — Approach

Access the classifier inside the pipeline and inspect its learned rules.

Explained solution
answer=model.named_steps['model'].rules_

The rule mapping makes the simple classifier inspectable and keeps its limited expressiveness visible.

Helpful prior knowledge: Macro F1 and imbalance · Keep preparation with the estimator These links are guidance, not locks.

Sources and API context

Examples run with this Playground’s scikit-learn 1.4.2 / Pyodide 0.26.4 runtime.

Your task · Follow

Store the fitted One-R classifier’s rules_ dictionary in answer.

answer

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.