Python

Neural Network Example

Neural Network Example

In this article we’ll make a classifier using an artificial neural network.
While internally the neural network algorithm works different from other supervised learning algorithms, the steps are the same:
supervised learning

Related course: Data Science and Machine Learning with Python – Hands On!

Training data

We start with training data:

ArrayContainsSize
Xtraining samples represented as floating point feature vectorssize (n_samples, n_features)
yclass labels for the training samplessize (n_samples,)

In code we define that as:

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

Train classifier

We then create the classifier:

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

Train the classifier with training data:

 
clf.fit(X, y)

Predict

And finally we can make predictions:

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

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.]]) )

Previous Post Next Post

Cookie policy | Privacy policy | ©

Machine Learning