Neural Networks with SkLearn

Neural Networks provide infinite possibilities for holding complex models with extreme precision. We have higher libraries like Tensorflow and Pytorch that provide high performance implementations specialized for different architectures of neural networks. But, Scikitlearn does provide us with a clean and simple implementations for the basic models.


A simple, connected and unidirectional neural network model is defined by the number of layers and the number of perceptrons in each individual layer. Scikit Learn is provides a basic implementation for the Neural Networks. In order to implement it in Python, we start with importing the libraries.

from sklearn.datasets import load_breast_cancer
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split

Next, load the data set to train and test the network. As in the previous examples, we can use the dataset of breast cancer.

cancer = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(,,, random_state=42)

Now we can create an instance of the classifier and train it

clf = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1), y_train)

With this training, we can check how well our model works

print('Accuracy on the training subset: {:.3f}'.format(clf.score(X_train, y_train)))
Accuracy on the training subset: 0.939
print('Accuracy on the training subset: {:.3f}'.format(clf.score(X_test, y_test)))
Accuracy on the training subset: 0.944

That is a decent performance. We can try playing around with the structure of the network, to get better implementations.

ScikitLearn was good to demonstrate small examples. But it does not perform well enough to be used in bigger Neural Networks. Tensorflow and other such libraries are used to handle Deep Neural Networks.