Skip to Content
Python Machine Learning Cookbook
book

Python Machine Learning Cookbook

by Prateek Joshi, Vahid Mirjalili
June 2016
Beginner to intermediate
304 pages
6h 24m
English
Packt Publishing
Content preview from Python Machine Learning Cookbook

Building a Naive Bayes classifier

A Naive Bayes classifier is a supervised learning classifier that uses Bayes' theorem to build the model. Let's go ahead and build a Naïve Bayes classifier.

How to do it…

  1. We will use naive_bayes.py that is provided to you as reference. Let's import a couple of things:
    from sklearn.naive_bayes import GaussianNB 
    from logistic_regression import plot_classifier
  2. You were provided with a data_multivar.txt file. This contains data that we will use here. This contains comma-separated numerical data in each line. Let's load the data from this file:
    input_file = 'data_multivar.txt' X = [] y = [] with open(input_file, 'r') as f: for line in f.readlines(): data = [float(x) for x in line.split(',')] X.append(data[:-1]) y.append(data[-1]) ...
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

Python Machine Learning Cookbook - Second Edition

Python Machine Learning Cookbook - Second Edition

Giuseppe Ciaburro, Prateek Joshi
Python: Real World Machine Learning

Python: Real World Machine Learning

Prateek Joshi, John Hearty, Bastiaan Sjardin, Luca Massaron, Alberto Boschetti

Publisher Resources

ISBN: 9781786464477Supplemental Content