Python

Naive Bayes classifier

In Machine Learning, Naive Bayes is a supervised learning classifier.

Its popular in text categorization (spam or not spam) and even competes with advanced classifiers like support vector machines.

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

Naive Bayes classifier

In the example below we create the classifier, the training set,
then train the classifier using the training set and make a prediction.

The training set (X) simply consits of length, weight and shoe size. Y contains the associated labels (male or female).

# create dataset
X = [[121, 80, 44], [180, 70, 43], [166, 60, 38], [153, 54, 37], [166, 65, 40], [190, 90, 47], [175, 64, 39],
[174, 71, 40], [159, 52, 37], [171, 76, 42], [183, 85, 43]]

Y = ['male', 'male', 'female', 'female', 'male', 'male', 'female', 'female', 'female', 'male', 'male']

Then we train the classifier with one line:
gaunb = gaunb.fit(X, Y)

And finally make predictions with new data.
from sklearn.naive_bayes import GaussianNB

# create naive bayes classifier
gaunb = GaussianNB()

# create dataset
X = [[121, 80, 44], [180, 70, 43], [166, 60, 38], [153, 54, 37], [166, 65, 40], [190, 90, 47], [175, 64, 39],
[174, 71, 40], [159, 52, 37], [171, 76, 42], [183, 85, 43]]

Y = ['male', 'male', 'female', 'female', 'male', 'male', 'female', 'female', 'female', 'male', 'male']

# train classifier with dataset
gaunb = gaunb.fit(X, Y)

# predict using classifier
prediction = gaunb.predict([[190, 70, 43]])
print(prediction)

On the final part of the program we make a prediction with new data [190,70,43]. This then outputs the predicted value.

The phases are similar in all supervised learning classifiers:
supervised learning

If you predict for another set like [166, 65, 33],
you’ll get another predicted outcome.

As you may expect, it’s important to have a good training set for receiving good results.

Previous Post Next Post

Cookie policy | Privacy policy | ©

Machine Learning