How to do it...

  1. Import the following packages:
from sklearn.naive_bayes import GaussianNBimport numpy as npimport matplotlib.pyplot as plt
  1. 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)
  1. Construct a Naive Bayes classifier:
classification_gaussiannb = GaussianNB()classification_gaussiannb.fit(a, b)b_pred = classification_gaussiannb.predict(a)
  1. Calculate the accuracy of Naive Bayes:
correctness = 100.0 * (b == b_pred).sum() / a.shape[0]print "correctness of the classification =", round(correctness, ...

Get Raspberry Pi 3 Cookbook for Python Programmers - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.