Sling Academy
Home/Scikit-Learn/Multi-Layer Perceptrons in Scikit-Learn

Multi-Layer Perceptrons in Scikit-Learn

Last updated: December 17, 2024

Multi-Layer Perceptrons (MLPs) are a class of feedforward artificial neural networks (ANN). They consist of multiple layers of nodes, interconnected in a network, where the output of one layer becomes the input to the next. In machine learning, MLPs are particularly valuable for practical applications involving large datasets and complex models. Scikit-learn, a powerful Python library, offers an efficient implementation of MLP that simplifies the handling of such networks.

Understanding Multi-Layer Perceptrons

The basic unit of a neural network, including the MLP, is the neuron or perceptron. An MLP consists of an input layer, one or more hidden layers, and an output layer. Each layer has multiple neurons, and each neuron applies a linear transformation followed by a non-linear activation function to its inputs. These layers together can learn complex patterns through backpropagation by adjusting weights and biases.

Setting Up Scikit-Learn for MLP

Before diving into MLPs in Scikit-learn, you need to set up your environment. Ensure you have Python installed, and then install the Scikit-learn library:

pip install scikit-learn

Building a Basic MLP with Scikit-Learn

Using Scikit-learn to create an MLP involves using the MLPClassifier or MLPRegressor class depending on whether you want to perform classification or regression.

from sklearn.neural_network import MLPClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

Loading the Dataset

For this example, we'll use the Iris dataset, a classic in machine learning. First, load the dataset and split it into training and test sets:

iris = load_iris()
X, y = iris.data, iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

Feature Scaling

MLPs are sensitive to feature scaling, so it's crucial to normalize your data. Scikit-learn provides a StandardScaler to do this effectively:

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

Training the MLP Model

We can now define and train our MLP model. Here, we initialize the MLPClassifier with desired number of layers and neurons:

mlp = MLPClassifier(hidden_layer_sizes=(10,), max_iter=1000, random_state=42)
mlp.fit(X_train, y_train)

In this simple example, we use a single hidden layer with ten neurons. The max_iter specifies how many iterations the optimizer should perform during the weight update. You can customize this to improve model accuracy or reduce computation time.

Evaluating the Model

After training, evaluate the model by making predictions on the test dataset and calculating accuracy:

y_pred = mlp.predict(X_test)
accuracy = (y_pred == y_test).mean()
print("Accuracy:", accuracy)

With a well-implemented model, you should achieve substantial accuracy on the Iris dataset.

Conclusion

Using Scikit-learn's MLPClassifier allows for straightforward development of neural networks with customizable architectures. While our example is relatively simple, MLPs can be adapted for a wide array of supervised learning problems by tuning hyperparameters, layer configurations, and training strategies. Exploring these options can lead to models performing well beyond what linear algorithms might offer.

Next Article: Pipeline Construction in Scikit-Learn

Previous Article: Nearest Centroid Classification in Scikit-Learn

Series: Scikit-Learn Tutorials

Scikit-Learn

You May Also Like

  • Generating Gaussian Quantiles with Scikit-Learn
  • Spectral Biclustering with Scikit-Learn
  • Scikit-Learn Complete Cheat Sheet
  • ValueError: Estimator Does Not Support Sparse Input in Scikit-Learn
  • Scikit-Learn TypeError: Cannot Broadcast Due to Shape Mismatch
  • AttributeError: 'dict' Object Has No Attribute 'predict' in Scikit-Learn
  • KeyError: Missing 'param_grid' in Scikit-Learn GridSearchCV
  • Scikit-Learn ValueError: 'max_iter' Must Be Positive Integer
  • Fixing Log Function Error with Negative Values in Scikit-Learn
  • RuntimeError: Distributed Computing Backend Not Found in Scikit-Learn
  • Scikit-Learn TypeError: '<' Not Supported Between 'str' and 'int'
  • AttributeError: GridSearchCV Has No Attribute 'fit_transform' in Scikit-Learn
  • Fixing Scikit-Learn Split Error: Number of Splits > Number of Samples
  • Scikit-Learn TypeError: Cannot Concatenate 'str' and 'int'
  • ValueError: Cannot Use 'predict' Before Fitting Model in Scikit-Learn
  • Fixing AttributeError: NoneType Has No Attribute 'predict' in Scikit-Learn
  • Scikit-Learn ValueError: Cannot Reshape Array of Incorrect Size
  • LinAlgError: Matrix is Singular to Machine Precision in Scikit-Learn
  • Fixing TypeError: ndarray Object is Not Callable in Scikit-Learn