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.
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 withX_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- The requested result, with the values, labels and row order described by this task.
island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | year | species |
|---|---|---|---|---|---|---|---|
| Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 | Adelie |
| Torgersen | 39.5 | 17.4 | 186 | 3800 | female | 2007 | Adelie |
| Torgersen | 40.3 | 18 | 195 | 3250 | female | 2007 | Adelie |
| Torgersen | 36.7 | 19.3 | 193 | 3450 | female | 2007 | Adelie |
| Torgersen | 39.3 | 20.6 | 190 | 3650 | male | 2007 | Adelie |
| Torgersen | 38.9 | 17.8 | 181 | 3625 | female | 2007 | Adelie |
| Torgersen | 39.2 | 19.6 | 195 | 4675 | male | 2007 | Adelie |
| Torgersen | 41.1 | 17.6 | 182 | 3200 | female | 2007 | Adelie |
- Search C for the mixed Penguin pipeline.
answerHints
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.