Skip to Content
Raspberry Pi 3 Cookbook for Python Programmers - Third Edition
book

Raspberry Pi 3 Cookbook for Python Programmers - Third Edition

by Steven Lawrence Fernandes, Tim Cox
April 2018
Beginner content levelBeginner
552 pages
13h 58m
English
Packt Publishing
Content preview from Raspberry Pi 3 Cookbook for Python Programmers - Third Edition

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, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Raspberry Pi for Python Programmers Cookbook - Second Edition

Raspberry Pi for Python Programmers Cookbook - Second Edition

Tim Cox

Publisher Resources

ISBN: 9781788629874Supplemental Content