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
- Compare LogisticRegression(max_iter=500) and MLPClassifier(hidden_layer_sizes=(16,),max_iter=300,
random_state=7). - Record Train Accuracy, Test Accuracy, and whether convergence warnings occurred.
Results to retain
- A table of both models and Test Confusion Matrices.
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
- Did you avoid refitting the scaler on test data?
- Did you avoid hiding warnings and assuming that the result had converged?
Comparing Network Sizes
Starting conditions
Change only MLP's hidden_layer_sizes to (4,),(16,),(32,), and compare using the same metrics.
Procedure
- Reuse the preceding Train/Test split and Scaler fitted on Train. Train MLPs by changing only
hidden_layer_sizesto(4,),(16,), and(32,). - 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. - Explain how Train Accuracy and Test Accuracy each change as Hidden size increases, distinguishing cases with and without warnings.
Results to retain
- A table comparing Accuracy and convergence warnings by Hidden size.
hidden_sizes=[(4,),(16,),(32,)]
Record results and self-check
Hidden size:
Train Accuracy:
Test Accuracy:
Convergence warnings:
Points to check
- Did you use the same split and preprocessing for each model?
Checking Other Inputs
Starting conditions
Compare the same two models with split seed=11 and examine variability in a small dataset.
Procedure
- Change only the split Seed to 11, refit the Scaler on the new Train set, and transform Train and Test.
- Retrain the Logistic Regression and Hidden
(16,)MLP from Activity 1 on the new Train set with the same settings. - 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
- A table of both models' Accuracy for each shared split and changes in the comparison results.
# 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
- Did you refit the scaler only on the new train data?