![]() |
![]() |
|
|
Multi Layer Perceptrons are the usual representation used by neural networks for dealing with multidimensional data. In Python they are implemented through the MLPClassifier
in the
sklearn.neural_network
.
Its training is done through an implementation of the backpropagation algorithm, and so it makes use of a learning rate specified through the learning_rate_init
parameter, a number of maximum iterations (max_iter
parameter) and the kind of learning rate change through the learning_rate
parameter.
trains a set of n decision trees, that are combined in an ensemble of n_estimators
.
Beside those, we also may choose among different activation functions through the activation
parameter here instantiated as the logistic function.
Setting the verbose
parameter we are able to see the error evolution along iterations.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sklearn.metrics as metrics
from sklearn.neural_network import MLPClassifier
import ds_charts as ds
file_tag = 'diabetes'
filename = 'data/diabetes'
target = 'class'
train: pd.DataFrame = pd.read_csv(f'{filename}_train.csv')
trnY: np.ndarray = train.pop(target).values
trnX: np.ndarray = train.values
labels = pd.unique(trnY)
test: pd.DataFrame = pd.read_csv(f'{filename}_test.csv')
tstY: np.ndarray = test.pop(target).values
tstX: np.ndarray = test.values
lr_type = ['constant', 'invscaling', 'adaptive']
max_iter = [100, 300, 500, 750, 1000]
learning_rate = [.1, .3, .5, .7, .9]
best = ('', 0, 0)
last_best = 0
best_model = None
cols = len(lr_type)
plt.figure()
fig, axs = plt.subplots(1, cols, figsize=(cols*ds.HEIGHT, ds.HEIGHT), squeeze=False)
for k in range(len(lr_type)):
d = lr_type[k]
values = {}
for lr in learning_rate:
yvalues = []
for n in max_iter:
mlp = MLPClassifier(activation='logistic', solver='sgd', learning_rate=d,
learning_rate_init=lr, max_iter=n, verbose=False)
mlp.fit(trnX, trnY)
prdY = mlp.predict(tstX)
yvalues.append(metrics.accuracy_score(tstY, prdY))
if yvalues[-1] > last_best:
best = (d, lr, n)
last_best = yvalues[-1]
best_model = mlp
values[lr] = yvalues
ds.multiple_line_chart(max_iter, values, ax=axs[0, k], title=f'MLP with lr_type={d}',
xlabel='mx iter', ylabel='accuracy', percentage=True)
plt.savefig(f'images/{file_tag}_mlp_study.png')
plt.show()
print(f'Best results with lr_type={best[0]}, learning rate={best[1]} and {best[2]} max iter, with accuracy={last_best}')
<Figure size 600x450 with 0 Axes>
Best results with lr_type=invscaling, learning rate=0.3 and 500 max iter, with accuracy=0.6796536796536796
After the plot you can see the parameters for which the best results were achieved. So let's see its performance, in that context in terms of other metrics.
prd_trn = best_model.predict(trnX)
prd_tst = best_model.predict(tstX)
ds.plot_evaluation_results(labels, trnY, prd_trn, tstY, prd_tst)
plt.savefig('images/{file_tag}_mlp_best.png')
plt.show()