<jupyter_start><jupyter_text>MLPRegressor - Concrete Dataset<jupyter_code>concrete = np.genfromtxt('./concrete.csv', delimiter=',')
X = concrete[:, :-1]
Y = concrete[:, [-1]]

x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=.2)
x_train.shape, x_test.shape, X.shape
params = {
    'hidden_layer_sizes': (2, [4, 9]),
    'learning_rate_init': (10, [-3, -1]),
    'alpha': (10, [-6, -3]),
}

default_params = {
    'max_iter': 1000,
    'batch_size': 32,
    'solver': 'sgd',
    'momentum': .9,
    'shuffle': True,
    'activation': 'tanh',
}

grid = RandomSearch(MLPRegressor, params, default_params, random_state=42)
grid.fit(x_train,y_train, n_iter=10, n_folds=5, add_bias=True, norm_x=True, norm_y=True, scorer=neg_rmse)
model = grid.best_estimator
grid.get_scores_()
print('Validation Metrics')
score = kfold_validation(model,
                 x_train,
                 y_train,
                 random_state = grid.random_state,
                 scorer=neg_rmse,
                 norm_x=True,
                 norm_y=True,
                 n_folds=5, 
                 plot_loss=True,
                 add_bias=True,
                 print_metrics={
                    'RMSE': RMSE,
                    'MAE': MAE,
                    'MRE': MRE
                })
plt.title('Loss Curve (Train)');
norm_x_train, norm_x, _  = standard_scaler(x_train)
norm_y_train, _, inv_norm = standard_scaler(y_train)

model.fit(norm_x_train, norm_y_train.ravel())
plt.title('Learning Curve (Train + Validation)')
plt.plot(model.loss_curve_);
norm_x_test = norm_x(x_test)

y_predict = model.predict(norm_x_test)
RMSE(inv_norm(y_predict), y_test)

print('Matrics (Train + Validation)')
print(f'RMSE: {RMSE(inv_norm(model.predict(norm_x_train)), y_train)}')
print(f'MAE: {MAE(inv_norm(model.predict(norm_x_train)), y_train)}')
print(f'MRE: {MRE(inv_norm(model.predict(norm_x_train)), y_train)}')

print('\nMetrics (Test)')
print(f'RMSE: {RMSE(inv_norm(y_predict), y_test)}')
print(f'MAE: {MAE(inv_norm(y_predict), y_test)}')
print(f'MRE: {MRE(inv_norm(y_predict), y_test)}')<jupyter_output>Matrics (Train + Validation)
RMSE: 4.501564485656955
MAE: 3.4286436302725565
MRE: 0.12140620099170228

Metrics (Test)
RMSE: 5.593910113038309
MAE: 4.2195771800782085
MRE: 0.15823957102536457<jupyter_text>MLPClassifier - Vowels Dataset<jupyter_code>vowels = np.genfromtxt('./vowel.csv', delimiter=',')
vowels_X = vowels[:, :-1]
vowels_Y = vowels[:, [-1]]

vowels_x_train, vowels_x_test, vowels_y_train, vowels_y_test = train_test_split(vowels_X, vowels_Y, test_size=.2)
vowels_x_train.shape, vowels_x_test.shape, vowels_X.shape, vowels_y_train.shape
params = {
    'hidden_layer_sizes': (2, [6, 10]),
    'learning_rate_init': (10, [-2, -1]),
    'alpha': (10, [-5, -2]),
}

default_params = {
    'max_iter': 1000,
    'batch_size': 32,
    'solver': 'sgd',
    'momentum': .9,
    'shuffle': True,
    'activation': 'relu'
}

grid = RandomSearch(MLPClassifier, params, default_params, random_state=42)
grid.fit(vowels_x_train, vowels_y_train, n_iter=10, n_folds=5, norm_x=True, scorer=accurracy)
model = grid.best_estimator
grid.get_scores_()
print('Validation Metrics\n')
kfold_validation(model,
                 vowels_x_train,
                 vowels_y_train,
                 scorer=accurracy,
                 random_state=grid.random_state,
                 norm_x=True,
                 n_folds=5, 
                 plot_loss=True,
                 print_metrics={
                     'accurracy': accurracy
                })
plt.title('Loss Curve (Train)');
norm_x_train, norm_x, _  = standard_scaler(vowels_x_train)

model.fit(norm_x_train, vowels_y_train.ravel())
plt.title('Learning Curve (Train + Validation)')
plt.plot(model.loss_curve_);
norm_x_test = norm_x(vowels_x_test)

y_predict = model.predict(norm_x_test)

print('Matrics (Train + Validation)')
print(f'Accurracy: {accurracy(model.predict(norm_x_train), vowels_y_train)}')

print('\nMetrics (Test)')
print(f'Accurracy: {accurracy(y_predict, vowels_y_test)}')<jupyter_output>Matrics (Train + Validation)
Accurracy: 1.0

Metrics (Test)
Accurracy: 0.9646464646464646
{'fraction_non_alphanumeric': 0.14720812182741116, 'fraction_numerical': 0.04254290548706793, 'mean_word_length': 3.696935300794552, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '448070', 'license': 'MIT', 'n_tokens_mistral': 1769, 'n_tokens_neox': 1619, 'n_words': 254, 'path': 'lista03/Lista03.ipynb', 'repo_name': 'DiegoFreitasH/Aprendizado_de_Maquina', 'size': '4137'}