0

When fitting an MLP neural network with scikit-learn's MLPCLassifier to the dataset iris this super-classic way:

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn import datasets
from sklearn.neural_network import MLPClassifier 
iris = datasets.load_iris()
df = pd.DataFrame(data= np.c_[iris['data'], iris['target']],
             columns= list(iris['feature_names']) + ['target'])
df.rename(columns={'sepal length (cm)': 'sepal length', 'sepal width (cm)': 'sepal width',
               'petal length (cm)': 'petal length','petal width (cm)': 'petal width'}, inplace=True)
predictors = ['sepal length','sepal width','petal length','petal width']
outcome = 'target'  
X = df[predictors]
y = df[outcome]
train_X, valid_X, train_y, valid_y = train_test_split(X, y, test_size=0.4, random_state=1)
clf = MLPClassifier(hidden_layer_sizes=(5), activation='relu', solver='lbfgs',                                          
            random_state=1, max_iter=200,
            early_stopping=True)            # <-- the required parameter!!
clf.fit(train_X, train_y)
clf.best_validation_score_

I get the following error message:

AttributeError: 'MLPClassifier' object has no attribute 'best_validation_score_'

as the attribute best_validation_score_ is NOT available. According to the latest stable scikit-learn documentation here, the attribute best_validation_score_ should be available when the network's instantiation is done with the parameter early_stopping=True, as before.

The scikit-learn version installed is 1.4.2.

Any explanation for that? Thanks.

0

1 Answer 1

0

I've find out that, with early_stopping=True, the 'solver' parameter must be sgd or adam. This way, the attribute best_validation_score_ is available.

Sign up to request clarification or add additional context in comments.

1 Comment

Interesting finding. This behavior seems like an oversight in the scikit-learn documentation, as it should specify that best_validation_score_ is only available with sgd or adam solvers when early_stopping=True. It would be helpful to raise an issue on GitHub to clarify this in the documentation and discuss whether it's possible to support other solvers like lbfgs. Your current approach appears more as a workaround rather than a solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.