Neural Network Example

In this article we’ll make a classifier using an artificial neural network. The impelemtation we’ll use is the one in sklearn, MLPClassifier.

While internally the neural network algorithm works different from other supervised learning algorithms, the steps are the same:

Related course: Complete Machine Learning Course with Python

Training data

Any AI algorithm needs data to operate: training data. We start with training data, which consists of two vectors:

ArrayContainsSize
X training samples represented as floating point feature vectorssize (n_samples, n_features)
y class labels for the training samplessize (n_samples,)

supervised learning

In code we define that as:

 
X = [[0., 0.], [1., 1.]]
y = [0, 1]

Where X are measurements and y contains the classes. This is an abstract example, click here to see a detailed example of a neural network.

Train classifier

A classifier is that, given new data, which type of class it belongs to. We then create the neural network classifier with the class MLPClassifier.This is an existing implementation of a neural net:

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

Train the classifier with training data (X) and it’s classes (y):

 
clf.fit(X, y)

Based on the training data, it can then make predictions.

Predict

One of the key things deep learning and machine learning algorithms do is make predictions. And finally we can make predictions. The classifier will output the class, given the measurements presented to it:

 
print( clf.predict([[2., 2.], [-1., -2.]]) )

It gives numerical output (0,1,2) that represents a class.

The neural network code is then:

 
from sklearn.neural_network import MLPClassifier

X = [[0., 0.], [1., 1.]]
y = [0, 1]
clf = MLPClassifier(solver='lbfgs', alpha=1e-5,
hidden_layer_sizes=(5, 2), random_state=1)

clf.fit(X, y)
print( clf.predict([[2., 2.], [-1., -2.]]) )