<jupyter_start><jupyter_text>Multilayer Perceptron: Fit and evaluate a modelUsing the Titanic dataset from [this](https://www.kaggle.com/c/titanic/overview) Kaggle competition.In this section, we will fit and evaluate a simple Multilayer Perceptron model.  Read in Data<jupyter_code>import joblib
import pandas as pd
from sklearn.model_selection import GridSearchCV
from sklearn.neural_network import MLPClassifier
import warnings
warnings.filterwarnings('ignore', category=FutureWarning)
warnings.filterwarnings('ignore', category=DeprecationWarning)

tr_features = pd.read_csv('../data/train_features.csv')
tr_labels = pd.read_csv('../data/train_labels.csv')<jupyter_output><empty_output><jupyter_text>Hyperparameter tuning![neuralnet](../references/hidden_layers.png)<jupyter_code>def print_results(results):
    print('BEST PARAMS: {}\n'.format(results.best_params_))
    means = results.cv_results_['mean_test_score']
    stds = results.cv_results_['std_test_score']
    for mean, std, params in zip(means, stds, results.cv_results_['params']):
        print('{} (+/-{}) for {}'.format(round(mean, 3), round(std * 2, 3), params))
mlp = MLPClassifier()
parameters = {
    'hidden_layer_sizes': [(10,), (50,), (100,)],
    'activation': ['logistic', 'tanh', 'relu'],
    'learning_rate': ['constant', 'invscaling', 'adaptive'],
    'max_iter': [1_000]
}

cv = GridSearchCV(mlp, parameters, cv=5)
cv.fit(tr_features, tr_labels.values.ravel())<jupyter_output><empty_output><jupyter_text>Write out pickled model<jupyter_code>print_results(cv)
print(cv.best_estimator_)
joblib.dump(cv.best_estimator_, '../models/MLP_model.pkl')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14665057332528667, 'fraction_numerical': 0.009052504526252263, 'mean_word_length': 6.895238095238096, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '219600', 'license': 'MIT', 'n_tokens_mistral': 595, 'n_tokens_neox': 567, 'n_words': 106, 'path': 'notebooks/multilayer_perceptron.ipynb', 'repo_name': 'Joshua-Robison/MLWorkflow', 'size': '1657'}