User Tools

Site Tools


code:naive_bayes

Naive Bayes in scikit-learn

Scikit-learn provides both Gaussian and multinomial/binomial flavors of Naive Bayes. Here's a code snippet that uses the Gaussian version and shows the resulting decision boundary for toy 2-d data (it uses decision_boundary.py from a previous demo):

naive_bayes.py
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.naive_bayes import GaussianNB
import decision_boundary
 
 
X, y = make_blobs(n_samples=1500, random_state=2)
classifier = GaussianNB()
decision_boundary.plot_boundary(classifier, X, y)
 
transformation = [[ 0.60834549, -0.63667341], [-0.40887718, 0.85253229]]
X, y = make_blobs(n_samples=1500, random_state=170)
X_aniso = np.dot(X, transformation)
decision_boundary.plot_boundary(classifier, X_aniso, y)
 
X_varied, y_varied = make_blobs(n_samples=1500,
                                cluster_std=[1.0, 2.5, 0.5],
                                random_state=170)
decision_boundary.plot_boundary(classifier, X_varied, y_varied)
code/naive_bayes.txt ยท Last modified: 2016/11/15 13:50 by asa