Course materials · Introduction to Machine Learning

Multilayer Perceptron

English translation

Learning objectives

Compare a complex model with a simple baseline classifier on the same data to examine whether complexity is always beneficial.

MLP and Baseline Models

Starting conditions

Split Iris using the same 70/30 split as above, and fit StandardScaler only on Train.

Procedure

  1. Compare LogisticRegression(max_iter=500) and MLPClassifier(hidden_layer_sizes=(16,),max_iter=300,random_state=7).
  2. Record Train Accuracy, Test Accuracy, and whether convergence warnings occurred.

Results to retain

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.neural_network import MLPClassifier
X,y=load_iris(return_X_y=True)
Xtr,Xte,ytr,yte=train_test_split(X,y,test_size=.3,random_state=7,stratify=y)
scaler=StandardScaler().fit(Xtr);Xtr=scaler.transform(Xtr);Xte=scaler.transform(Xte)
# TODO: Compare the two models using the same split and scaling.

Record results and self-check

Model:

Train Accuracy:

Test Accuracy:

Iterations and warnings:

Points to check

Comparing Network Sizes

Starting conditions

Change only MLP's hidden_layer_sizes to (4,),(16,),(32,), and compare using the same metrics.

Procedure

  1. Reuse the preceding Train/Test split and Scaler fitted on Train. Train MLPs by changing only hidden_layer_sizes to (4,), (16,), and (32,).
  2. Keep max_iter=300, random_state=7, and the other settings unchanged. Record hidden size, train accuracy, test accuracy, and whether convergence warnings occur in a table.
  3. Explain how Train Accuracy and Test Accuracy each change as Hidden size increases, distinguishing cases with and without warnings.

Results to retain

hidden_sizes=[(4,),(16,),(32,)]

Record results and self-check

Hidden size:

Train Accuracy:

Test Accuracy:

Convergence warnings:

Points to check

Checking Other Inputs

Starting conditions

Compare the same two models with split seed=11 and examine variability in a small dataset.

Procedure

  1. Change only the split Seed to 11, refit the Scaler on the new Train set, and transform Train and Test.
  2. Retrain the Logistic Regression and Hidden (16,) MLP from Activity 1 on the new Train set with the same settings.
  3. Tabulate split seed, model name, Train Accuracy, and Test Accuracy, and compare with the two-model comparison at seed 7. Explain how changing the Test samples affected the comparison.

Results to retain

# Use the preceding activity's data and functions to check the specified new examples.

Record results and self-check

Split Seed:

Model and settings:

Train·Test Accuracy:

Comparing different splits:

Points to check