Created
April 11, 2019 07:35
-
-
Save jainxy/7ecfaa15b5069b560d5c4fe21a9dcb71 to your computer and use it in GitHub Desktop.
Naive Bayes - sklearn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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