The question
Classify Candy popular using sugarpercent, pricepercent and the nine flags. Keep winpercent out of X and learn numeric bins only inside fits.
data/ml-learning/ML-X10.csv Complete workflow and evidenceYour inputs
Challenge input · ML-X10
85 observations. One candy product in the survey. 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. popular is 50% or above when winpercent >= 50, otherwise below 50%. winpercent is retained only for the leakage-detection task: exclude it from X.
sugarpercent | pricepercent | chocolate | fruity | caramel | peanutyalmondy | nougat | crispedricewafer | hard | bar | pluribus | winpercent | popular |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0.732 | 0.86 | 1 | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 66.9717 | 50% or above |
| 0.604 | 0.511 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 67.6029 | 50% or above |
| 0.011 | 0.116 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 32.2611 | below 50% |
| 0.011 | 0.511 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 46.1165 | below 50% |
| 0.906 | 0.511 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 52.3415 | 50% or above |
| 0.465 | 0.767 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 | 50.3475 | 50% or above |
| 0.604 | 0.767 | 1 | 0 | 1 | 1 | 1 | 0 | 0 | 1 | 0 | 56.9145 | 50% or above |
| 0.313 | 0.511 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 23.4178 | below 50% |
Column meanings and units
sugarpercent: sugar percentile. pricepercent: price percentile. winpercent: percentage of survey matchups won. Ingredient, bar and multipack fields are 0/1 flags.
Percentile ranks are neither physical sugar percentages nor currency prices. Ratios of percentiles do not measure economic value. Chocolate and fruit flags can overlap; non-chocolate is not synonymous with fruit. The ML class target is defined by winpercent ≥ 50.
Ready to load: comma-delimited UTF-8, header row, no saved index. No filtering or target construction is required. popular is 50% or above when winpercent >= 50, otherwise below 50%. winpercent is retained only for the leakage-detection task: exclude it from X.
| Column | Stored type |
|---|---|
sugarpercent | float64 |
pricepercent | float64 |
chocolate | int64 |
fruity | int64 |
caramel | int64 |
peanutyalmondy | int64 |
nougat | int64 |
crispedricewafer | int64 |
hard | int64 |
bar | int64 |
pluribus | int64 |
winpercent | float64 |
popular | object |
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.
- Classify Candy
popularusingsugarpercent,pricepercentand the nine flags. - Keep
winpercentout ofXand learnnumericbins only inside fits.
modelcv_resultsreference_resultsmatrixfinal_f1final_accuracyOutputs you create
model- Fitted estimator or pipeline for the requested model family.
cv_results- Candidate validation evidence; comparison workflows use a dataframe indexed by model ID.
reference_resultscross_validateresult for the dummy reference; test_score contains five scores.
matrix- Confusion
matrixof training-only diagnostic predictions.
- Confusion
final_f1- Macro F1 on final-test class predictions.
final_accuracy- 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
Data boundary
- Load the supplied challenge CSV, define the declared
X/yand keep aligned row identities through the split. - Keep learned preparation inside each training fit.
- Load the supplied challenge CSV, define the declared
Validation & selection
- Compare the requested initial model families with a dummy reference on matching training
folds. - Evaluate the declared settings where applicable and choose from training evidence.
- Compare the requested initial model families with a dummy reference on matching training
Diagnosis
- Use out-of-fold training predictions to inspect residuals or class errors before opening the final test.
Final evidence
- Fit the nominated model on training rows and evaluate the reserved rows once.
- Report saved predictions, the requested metrics and labelled evidence
- 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.
- Choose what to predictClassify whether a candy reaches at least 50% wins using 11 selected inputs; each row is one candy.
- Split data and save the test setWhich rows are available for learning, and which are being saved for the final check?
- Explore training dataDoes sugarpercent show a training pattern related to popularity class?
- Define the preparation recipeWhat preparation does this particular model need?
- Build the model pipelineWhich single feature gives the simplest useful class rules?
- Validate the initial modelDo validation results stay reasonably similar across folds?
- Compare with a simple referenceDo the selected features beat a simple guess on the same training folds?
- Tune the modelHow many numeric intervals are useful for the one-feature rule?
- Inspect validation errorsWhich classes are most often confused in training-only validation?
- Final testHow does the selected model's saved final popularity class result compare with the initial-model CV and any selected-setting CV for Candy Popularity · Continuous + binary? 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
Bin boundaries are learned information.
Hint 2 — Tools
OneRPreprocessor numeric/discrete metadata and the bins grid.
Revisit: One-R with numeric inputs · Finish once, then report
Hint 3 — Approach
Compare bin counts inside training folds, preserve binary types and explain the selected rule.
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-X10.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 = ['sugarpercent', 'pricepercent']
categories = ['chocolate', 'fruity', 'caramel', 'peanutyalmondy', 'nougat', 'crispedricewafer', 'hard', 'bar', 'pluribus']
X = df[numeric + categories]
y = df['popular']
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')
search = GridSearchCV(model, {'model__bins':[3,5,8]}, cv=folds, scoring='f1_macro')
search.fit(X_train, y_train)
chosen = search.best_estimator_
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-X10.csv')Why these steps exist
- The prepared popular target is explicit, but its source winpercent is deliberately excluded from
Xas a leakage test. The preprocessor preserves numeric versus binary meanings, and bin boundaries are learned inside each training fit before class diagnostics and final evaluation.
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.