Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.0 kB
1
Indexable
Never
from sklearn.neural_network import MLPClassifier
import matplotlib.pyplot as plt
from sklearn.metrics import f1_score

activation_functions = ['identity', 'logistic', 'tanh', 'relu']

f1_scores = {}
loss_curves = {}

for activation_function in activation_functions:
    mlp = MLPClassifier(activation=activation_function, random_state=42)
    
    mlp.fit(X_train_new, y_train)

    y_pred = mlp.predict(X_test_new)

    f1_macro = f1_score(y_test, y_pred, average='macro')
    f1_scores[activation_function] = f1_macro

    loss_curve = mlp.loss_curve_
    loss_curves[activation_function] = loss_curve

plt.figure(figsize=(12, 6))
for activation_function, loss_curve in loss_curves.items():
    plt.plot(loss_curve, label=f'Activation: {activation_function}')
plt.title("Loss Curves for Different Activation Functions")
plt.xlabel("Number of Iterations")
plt.ylabel("Loss Metric")
plt.legend()
plt.grid(True)
plt.show()

for activation_function, f1_score in f1_scores.items():
    print("Activation:",activation_function," - F1-macro =", f1_score)