Multi-layer Perceptron regressor.
This model optimizes the squared error using LBFGS or stochastic gradient descent.
Added in version 0.18.
The ith element represents the number of neurons in the ith hidden layer.
Activation function for the hidden layer.
The solver for weight optimization.
For a comparison between Adam optimizer and SGD, see Compare Stochastic learning strategies for MLPClassifier.
Note: The default solver ‘adam’ works pretty well on relatively large datasets (with thousands of training samples or more) in terms of both training time and validation score. For small datasets, however, ‘lbfgs’ can converge faster and perform better.
Strength of the L2 regularization term. The L2 regularization term is divided by the sample size when added to the loss.
Size of minibatches for stochastic optimizers. If the solver is ‘lbfgs’, the regressor will not use minibatch. When set to “auto”, batch_size=min(200, n_samples).
Learning rate schedule for weight updates.
learning_rate_ at each time step ‘t’ using an inverse scaling exponent of ‘power_t’. effective_learning_rate = learning_rate_init / pow(t, power_t)Only used when solver=’sgd’.
The initial learning rate used. It controls the step-size in updating the weights. Only used when solver=’sgd’ or ‘adam’.
The exponent for inverse scaling learning rate. It is used in updating effective learning rate when the learning_rate is set to ‘invscaling’. Only used when solver=’sgd’.
Maximum number of iterations. The solver iterates until convergence (determined by ‘tol’) or this number of iterations. For stochastic solvers (‘sgd’, ‘adam’), note that this determines the number of epochs (how many times each data point will be used), not the number of gradient steps.
Whether to shuffle samples in each iteration. Only used when solver=’sgd’ or ‘adam’.
Determines random number generation for weights and bias initialization, train-test split if early stopping is used, and batch sampling when solver=’sgd’ or ‘adam’. Pass an int for reproducible results across multiple function calls. See Glossary.
Tolerance for the optimization. When the loss or score is not improving by at least tol for n_iter_no_change consecutive iterations, unless learning_rate is set to ‘adaptive’, convergence is considered to be reached and training stops.
Whether to print progress messages to stdout.
When set to True, reuse the solution of the previous call to fit as initialization, otherwise, just erase the previous solution. See the Glossary.
Momentum for gradient descent update. Should be between 0 and 1. Only used when solver=’sgd’.
Whether to use Nesterov’s momentum. Only used when solver=’sgd’ and momentum > 0.
Whether to use early stopping to terminate training when validation score is not improving. If set to True, it will automatically set aside validation_fraction of training data as validation and terminate training when validation score is not improving by at least tol for n_iter_no_change consecutive epochs. Only effective when solver=’sgd’ or ‘adam’.
The proportion of training data to set aside as validation set for early stopping. Must be between 0 and 1. Only used if early_stopping is True.
Exponential decay rate for estimates of first moment vector in adam, should be in [0, 1). Only used when solver=’adam’.
Exponential decay rate for estimates of second moment vector in adam, should be in [0, 1). Only used when solver=’adam’.
Value for numerical stability in adam. Only used when solver=’adam’.
Maximum number of epochs to not meet tol improvement. Only effective when solver=’sgd’ or ‘adam’.
Added in version 0.20.
Only used when solver=’lbfgs’. Maximum number of function calls. The solver iterates until convergence (determined by tol), number of iterations reaches max_iter, or this number of function calls. Note that number of function calls will be greater than or equal to the number of iterations for the MLPRegressor.
Added in version 0.22.
The current loss computed with the loss function.
The minimum loss reached by the solver throughout fitting. If early_stopping=True, this attribute is set to None. Refer to the best_validation_score_ fitted attribute instead. Only accessible when solver=’sgd’ or ‘adam’.
n_iter_,)
Loss value evaluated at the end of each training step. The ith element in the list represents the loss at the ith iteration. Only accessible when solver=’sgd’ or ‘adam’.
n_iter_,) or None
The score at each iteration on a held-out validation set. The score reported is the R2 score. Only available if early_stopping=True, otherwise the attribute is set to None. Only accessible when solver=’sgd’ or ‘adam’.
The best validation score (i.e. R2 score) that triggered the early stopping. Only available if early_stopping=True, otherwise the attribute is set to None. Only accessible when solver=’sgd’ or ‘adam’.
The number of training samples seen by the solver during fitting. Mathematically equals n_iters * X.shape[0], it means time_step and it is used by optimizer’s learning rate scheduler.
The ith element in the list represents the weight matrix corresponding to layer i.
The ith element in the list represents the bias vector corresponding to layer i + 1.
Number of features seen during fit.
Added in version 0.24.
n_features_in_,)
Names of features seen during fit. Defined only when X has feature names that are all strings.
Added in version 1.0.
The number of iterations the solver has run.
Number of layers.
Number of outputs.
Name of the output activation function.
See also
BernoulliRBMBernoulli Restricted Boltzmann Machine (RBM).
MLPClassifierMulti-layer Perceptron classifier.
sklearn.linear_model.SGDRegressorLinear model fitted by minimizing a regularized empirical loss with SGD.
MLPRegressor trains iteratively since at each time step the partial derivatives of the loss function with respect to the model parameters are computed to update the parameters.
It can also have a regularization term added to the loss function that shrinks model parameters to prevent overfitting.
This implementation works with data represented as dense and sparse numpy arrays of floating point values.
Hinton, Geoffrey E. “Connectionist learning procedures.” Artificial intelligence 40.1 (1989): 185-234.
Glorot, Xavier, and Yoshua Bengio. “Understanding the difficulty of training deep feedforward neural networks.” International Conference on Artificial Intelligence and Statistics. 2010.
Kingma, Diederik, and Jimmy Ba (2014) “Adam: A method for stochastic optimization.”
>>> from sklearn.neural_network import MLPRegressor >>> from sklearn.datasets import make_regression >>> from sklearn.model_selection import train_test_split >>> X, y = make_regression(n_samples=200, n_features=20, random_state=1) >>> X_train, X_test, y_train, y_test = train_test_split(X, y, ... random_state=1) >>> regr = MLPRegressor(random_state=1, max_iter=2000, tol=0.1) >>> regr.fit(X_train, y_train) MLPRegressor(max_iter=2000, random_state=1, tol=0.1) >>> regr.predict(X_test[:2]) array([ 28..., -290...]) >>> regr.score(X_test, y_test) 0.98...
Fit the model to data matrix X and target(s) y.
The input data.
The target values (class labels in classification, real numbers in regression).
Returns a trained MLP model.
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
A MetadataRequest encapsulating routing information.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Update the model with a single iteration over the given data.
The input data.
The target values.
Trained MLP model.
Predict using the multi-layer perceptron model.
The input data.
The predicted values.
Return the coefficient of determination of the prediction.
The coefficient of determination \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)** 2).sum() and \(v\) is the total sum of squares ((y_true - y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.
Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.
True values for X.
Sample weights.
\(R^2\) of self.predict(X) w.r.t. y.
The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score. This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Request metadata passed to the score method.
Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config). Please see User Guide on how the routing mechanism works.
The options for each parameter are:
True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it to score.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.
Added in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.
Metadata routing for sample_weight parameter in score.
The updated object.
© 2007–2025 The scikit-learn developers
Licensed under the 3-clause BSD License.
https://scikit-learn.org/1.6/modules/generated/sklearn.neural_network.MLPRegressor.html