Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Classification lessons
Probabilities and neighbours · ML-C06 · 25–40 MIN

Tune logistic regularisation

Compare regularisation choices using training-validation evidence.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. TransferAdapt the Python
  4. ApplyBuild a guided full workflow

Understand the idea

Larger C means weaker regularisation. Compare supported C values within a pipeline. A geographic-feature comparison must use matching training folds, not final-test results.

Tune regularisation and investigate shortcuts using training evidence.Fold 1VTTTTFold 2TVTTTFold 3TTVTTFold 4TTTVTFold 5TTTTVFinalT: fit on training rows V: validate Final: held away
Tune regularisation and investigate shortcuts using training evidence.Scroll the diagram horizontally if needed.

What the syntax does

'model__C'
  • Addresses inverse regularisation strength in the pipeline’s model step.
  • C: 0.5, 1, 2, 10

Supplied for this practice

These objects are prepared afresh before your editor runs.

numeric
Supplied numeric; its definition below specifies the values and role.
numeric = ['bill_length_mm', 'bill_depth_mm', 'flipper_length_mm', 'body_mass_g']
X
Feature dataframe for the declared population, preserving row indices.
X = df[numeric + ['island', 'sex', 'year']]
y
Target series, aligned with X.
y = df.species
X_train / X_test / y_train / y_test
Training feature rows from the declared split. Final-test feature rows from the declared split. Training targets, aligned with X_train. Final-test targets, aligned with X_test.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
prepare
Supplied prepare; its definition below specifies the values and role.
prepare = ColumnTransformer([('numeric', StandardScaler(), numeric), ('category', OneHotEncoder(handle_unknown='ignore', sparse_output=False), ['island', 'sex', 'year'])])
model
An unfitted recipe is supplied; construction has not learned parameters. Fit it here only when the task requests fitting.
model = Pipeline([('prepare', prepare), ('model', LogisticRegression(max_iter=2000, random_state=42))])
folds
Supplied folds; its definition below specifies the values and role.
folds = StratifiedKFold(5, shuffle=True, random_state=42)

Outputs you create

answer
  1. The requested result, with the values, labels and row order described by this task.
penguins · first 8 rows
islandbill_length_mmbill_depth_mmflipper_length_mmbody_mass_gsexyearspecies
Torgersen39.118.71813750male2007Adelie
Torgersen39.517.41863800female2007Adelie
Torgersen40.3181953250female2007Adelie
Torgersen36.719.31933450female2007Adelie
Torgersen39.320.61903650male2007Adelie
Torgersen38.917.81813625female2007Adelie
Torgersen39.219.61954675male2007Adelie
Torgersen41.117.61823200female2007Adelie
Your task
  1. Search C for the mixed Penguin pipeline.
answer
Hints

Think: Regularisation strength is chosen using training-only class-balanced evidence.

Tools: GridSearchCV, model__C and f1_macro.

Approach: Search the declared C values with the supplied mixed pipeline and fixed folds.

Explained solution
search=GridSearchCV(model,{'model__C':[.1,1,10]},cv=folds,scoring='f1_macro').fit(X_train,y_train)
answer=search.cv_results_['mean_test_score']

Preparation is refitted within each fold, so the comparison evaluates the complete candidate recipe without test leakage.

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.