Next Chapter: Introduction into Text Classification using Naive Bayes
Naive Bayes Classifier with Scikit
We have written Naive Bayes Classifiers from scratch in our previous chapter of our tutorial. In this part of the tutorial on Machine Learning with Python, we want to show you how to use ready-made classifiers. The module Scikit provides naive Bayes classifiers "off the rack".
Our first example uses the "iris dataset" contained in the model to train and test the classifier
# Gaussian Naive Bayes
from sklearn import datasets
from sklearn import metrics
from sklearn.naive_bayes import GaussianNB
# load the iris datasets
dataset = datasets.load_iris()
# fit a Naive Bayes model to the data
model = GaussianNB()
model.fit(dataset.data, dataset.target)
print(model)
# make predictions
expected = dataset.target
predicted = model.predict(dataset.data)
# summarize the fit of the model
print(metrics.classification_report(expected, predicted))
print(metrics.confusion_matrix(expected, predicted))
We use our person data from the previous chapter of our tutorial to train another classifier in the next example:
import numpy as np
def prepare_person_dataset(fname):
genders = ["male", "female"]
persons = []
with open(fname) as fh:
for line in fh:
persons.append(line.strip().split())
firstnames = []
dataset = [] # weight and height
for person in persons:
firstnames.append( (person[0], person[4]) )
height_weight = (float(person[2]), float(person[3]))
dataset.append( (height_weight, person[4]))
return dataset
learnset = prepare_person_dataset("data/person_data.txt")
testset = prepare_person_dataset("data/person_data_testset.txt")
print(learnset)
# Gaussian Naive Bayes
from sklearn import datasets
from sklearn import metrics
from sklearn.naive_bayes import GaussianNB
model = GaussianNB()
#print(dataset.data, dataset.target)
w, l = zip(*learnset)
w = np.array(w)
l = np.array(l)
model.fit(w, l)
print(model)
w, l = zip(*testset)
w = np.array(w)
l = np.array(l)
predicted = model.predict(w)
print(predicted)
print(l)
# summarize the fit of the model
print(metrics.classification_report(l, predicted))
print(metrics.confusion_matrix(l, predicted))
Next Chapter: Introduction into Text Classification using Naive Bayes