Skip to content

Instantly share code, notes, and snippets.

@jainxy
Created April 11, 2019 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jainxy/7ecfaa15b5069b560d5c4fe21a9dcb71 to your computer and use it in GitHub Desktop.
Save jainxy/7ecfaa15b5069b560d5c4fe21a9dcb71 to your computer and use it in GitHub Desktop.
Naive Bayes - sklearn
# load the iris dataset
from sklearn.datasets import load_iris
iris = load_iris()
# store the feature matrix (X) and response vector (y)
X = iris.data
y = iris.target
# splitting X and y into training and testing sets
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=1)
# training the model on training set
from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
gnb.fit(X_train, y_train)
# making predictions on the testing set
y_pred = gnb.predict(X_test)
# comparing actual response values (y_test) with predicted response values (y_pred)
from sklearn import metrics
print("Gaussian Naive Bayes model accuracy(in %):", metrics.accuracy_score(y_test, y_pred)*100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment