Multilayer Perceptron · Study

English translation

Topics covered

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

Start with a Simple Model

An MLP can represent nonlinear relationships, but increasing its size adds Parameters to learn and settings to tune. A simple classifier as a baseline on the same data reveals the benefits of the added complexity.

Combining Preprocessing and Evaluation

Fit standardization on training data, and apply only the same transformation to evaluation data. Combining transformations and the model in a scikit-learn Pipeline helps preserve this order during splitting and cross-validation.

ConvergenceWarning means that optimization did not meet its convergence criterion within the specified iterations. For this comparison, keep max_iter as specified and record both warnings and scores.

Scores from runs with warnings alone are insufficient to conclude which model is better.

Worked example

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

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)

Train and evaluate one model using the split and Standardization results prepared above.

model = LogisticRegression(max_iter=500)
model.fit(Xtr, ytr)
prediction = model.predict(Xte)
print("Train Accuracy:", model.score(Xtr, ytr))
print("Test Accuracy:", model.score(Xte, yte))

Reference example

View more examples

Reference materials