#### Overview of this book

Machine learning is becoming increasingly pervasive in the modern data-driven world. It is used extensively across many fields such as search engines, robotics, self-driving cars, and more. With this book, you will learn how to perform various machine learning tasks in different environments. We’ll start by exploring a range of real-life scenarios where machine learning can be used, and look at various building blocks. Throughout the book, you’ll use a wide variety of machine learning algorithms to solve real-world problems and use Python to implement these algorithms. You’ll discover how to deal with various types of data and explore the differences between machine learning paradigms such as supervised and unsupervised learning. We also cover a range of regression techniques, classification algorithms, predictive modeling, data visualization techniques, recommendation engines, and more with the help of real-world examples.
Python Machine Learning Cookbook
Credits
www.PacktPub.com
Preface
Free Chapter
The Realm of Supervised Learning
Visualizing Data
Index

## 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:
data = [float(x) for x in line.split(',')]
X.append(data[:-1])
y.append(data[-1])

X = np.array(X)
y = np.array(y)```

We have now loaded the input data into `X` and the labels into `y`.

3. Let's build the Naive Bayes classifier:

```classifier_gaussiannb = GaussianNB()
classifier_gaussiannb.fit(X, y)
y_pred...```