Understand the idea
A scaled MLPClassifier uses the shared optimisation ideas for class prediction. Search a small width grid and compare with logistic regression on the same training folds. Complexity needs evidence.
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] 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) model- An unfitted recipe is supplied; construction has not learned parameters. Fit it here only when the task requests fitting.
model = Pipeline([('scale', StandardScaler()), ('model', MLPClassifier(hidden_layer_sizes=(24,), max_iter=500, early_stopping=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 |
- Fit the supplied scaled neural classifier and predict labels.
answerHints
Think: The supplied pipeline keeps feature scaling attached to the neural classifier.
Tools: Pipeline.fit and predict.
Approach: Fit the training pair through the supplied recipe and predict held-away class labels.
Explained solution
model.fit(X_train,y_train)
answer=model.predict(X_test)
The pipeline learns preparation only from training rows and applies the same representation during classification.