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…
- 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
- 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]) X = np.array(X) y = np.array(y)
We have now loaded the input data into
X
and the labels intoy
. - Let's build the Naive Bayes classifier:
classifier_gaussiannb...