A Naive Bayes classifier employs Bayes' theorem to construct a supervised model.
Building a Naive Bayes classifier
How to do it...
- Import the following packages:
from sklearn.naive_bayes import GaussianNB
import numpy as np
import matplotlib.pyplot as plt
- 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)
- Construct a Naive Bayes classifier:
classification_gaussiannb = GaussianNB()
classification_gaussiannb.fit(a, b)
b_pred = classification_gaussiannb...