How to do it...

  1. Import the packages:
from sklearn import cross_validationfrom sklearn.naive_bayes import GaussianNBimport numpy as npin_file = 'cross_validation_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)classification_gaussiannb = GaussianNB()
  1. Compute the accuracy of the classifier:
num_of_validations = 5accuracy = cross_validation.cross_val_score(classification_gaussiannb, a, b, scoring='accuracy', cv=num_of_validations)print "Accuracy: " + str(round(100* accuracy.mean(), 2)) + "%"f1 = cross_validation.cross_val_score(classification_gaussiannb, a, b, scoring='f1_weighted', cv=num_of_validations) ...

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.