<jupyter_start><jupyter_text>Grid Search NN<jupyter_code>nn_tuned_parameters = [{'hidden_layer_sizes': [(,), (50,), (50, 10), (50, 10, 10)],
                        'alpha':[0.0001,0.001,0.01,0.1,1,10], 'max_iter': range(50,200,50)}]
data = pd.read_csv('data/processed/mushrooms_pca.csv')
y = data['class']
X = data.drop('class', axis=1)

X_train, X_test, y_train, y_test = \
        train_test_split(X, y.ravel(), test_size=.1, random_state=42)
clf = MLPClassifier()

grid_search_nn = GridSearchCV(clf, nn_tuned_parameters, scoring='f1',
                           cv=10, return_train_score=True, n_jobs=3)
grid_search_nn.fit(X_train, y_train)

y_true, y_pred = y_test, grid_search_nn.predict(X_test)
print(grid_search_nn.best_params_)
report = classification_report(y_true, y_pred)
print(report)
res=pd.DataFrame(grid_search_nn.cv_results_).sort_values('rank_test_score').reset_index(drop=True)
res = res[['param_hidden_layer_sizes', 'param_alpha', 'param_max_iter', 'mean_test_score', 'mean_fit_time']]
print(res.head(n=8).to_latex())
pickle.dump(grid_search_nn, open("models/nn.p", "wb" ) )<jupyter_output><empty_output><jupyter_text>Simple Perceptron<jupyter_code>perc = Perceptron(tol=1e-3, random_state=0)
perc.fit(X_train, y_train)
scores = cross_val_score(perc, X_train, y_train, cv=10, scoring='f1_macro')

perc_f1=scores.mean()<jupyter_output><empty_output><jupyter_text>Multilayer Perceptron GridSearch - checking number of layers influence<jupyter_code>nn_tuned_parameters = [{'hidden_layer_sizes': [(50,), (50, 10), (50, 10, 10), (50, 10, 10, 10)]}]
clf = MLPClassifier(alpha=0.1)

grid_search_nn = GridSearchCV(clf, nn_tuned_parameters, scoring='f1',
                           cv=10, return_train_score=True, n_jobs=3)
grid_search_nn.fit(X_train, y_train)

res=pd.DataFrame(grid_search_nn.cv_results_).sort_values('rank_test_score').reset_index(drop=True)
res = res[['param_hidden_layer_sizes', 'mean_test_score', 'mean_fit_time']]
print(res.head(n=8).to_latex())

f1scores = np.array(res['mean_test_score'])
plt.plot(list(range(0,len(f1scores)+1)), np.insert(f1scores,0, perc_f1))
plt.xlabel('Number of hidden layers')
plt.ylabel('f1 score')
plt.title('MLP classifier performance')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16123269316659222, 'fraction_numerical': 0.038856632425189816, 'mean_word_length': 7.028673835125448, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '603932', 'license': 'MIT', 'n_tokens_mistral': 967, 'n_tokens_neox': 896, 'n_words': 122, 'path': 'notebooks/NN.ipynb', 'repo_name': 'borismattijssen/mushrooms', 'size': '2239'}