Building a Naive Bayes classifier
A Naive Bayes classifier employs Bayes' theorem to construct a supervised model.
How to do it...
- Import the following packages:
from sklearn.naive_bayes import GaussianNB import numpy as np import matplotlib.pyplot as plt
- Use the following data file, which includes comma-separated arithmetical data:
in_file = 'data_multivar.txt' a = [] b = [] with open(in_file, 'r') as f: for line in f.readlines(): data = [float(x) for x in line.split(',')] a.append(data[:-1]) b.append(data[-1]) a = np.array(a) b = np.array(b)
- Construct a Naive Bayes classifier:
classification_gaussiannb = GaussianNB() classification_gaussiannb.fit(a, b) b_pred = classification_gaussiannb.predict(a)
- Calculate the accuracy of Naive Bayes:
correctness = 100.0 * (b == b_pred).sum() / a.shape[0] print "correctness of the classification =", round(correctness, 2), "%"
- Plot the classifier result:
def plot_classification(classification_gaussiannb, a , b): a_min, a_max = min(a[:, 0]) - 1.0,...