Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Neural Networks lessons
Task workflows · ML-N03 · 25–40 MIN

Neural classification workflow

Apply class outputs and classification evidence to a small network.

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

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.

Apply class outputs and classification evidence to a small network.InputsHidden unitsOutputs
Apply class outputs and classification evidence to a small network.Scroll the diagram horizontally if needed.

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 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)
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
  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. Fit the supplied scaled neural classifier and predict labels.
answer
Hints

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.

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.