0% found this document useful (0 votes)
36 views

2 Naive Bayee Algorithm - Jupyter Notebook

This document presents code for analyzing a social network advertising dataset using a Gaussian Naive Bayes classifier. It loads and prepares the data, splits it into training and test sets, fits a Gaussian NB classifier to the training set, uses it to predict the test set labels, and calculates the confusion matrix and accuracy to evaluate the model's performance.

Uploaded by

venkatesh m
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

2 Naive Bayee Algorithm - Jupyter Notebook

This document presents code for analyzing a social network advertising dataset using a Gaussian Naive Bayes classifier. It loads and prepares the data, splits it into training and test sets, fits a Gaussian NB classifier to the training set, uses it to predict the test set labels, and calculates the confusion matrix and accuracy to evaluate the model's performance.

Uploaded by

venkatesh m
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

In 

[ ]: ​

In [1]: import numpy as np


import matplotlib.pyplot as plt
import pandas as pd
import sklearn

In [2]: data = pd.read_csv("D:\\Course\\Python\\Datasets\\Social_Network_Ads.csv")

In [3]: data

...

In [4]: X = data.iloc[:, [2, 3]].values

In [5]: y = data.iloc[:, 4].values

In [6]: # Splitting the dataset into the Training set and Test set

In [7]: from sklearn.model_selection import train_test_split


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, rando

In [8]: # Feature Scaling

In [9]: from sklearn.preprocessing import StandardScaler # importing method and Packages


sc = StandardScaler() # creating an function using imported method
X_train = sc.fit_transform(X_train) # apply that method only on input varabile
#X_test = sc.transform(X_test)

In [10]: # Fitting Naive Bayes to the Training set

In [11]: from sklearn.naive_bayes import GaussianNB


classifier = GaussianNB()
classifier.fit(X_train, y_train)

Out[11]: GaussianNB(priors=None, var_smoothing=1e-09)

In [12]: # Predicting the Test set results

In [13]: y_pred = classifier.predict(X_test)

In [14]: y_pred
...
In [15]: y_test
...

In [16]: # Making the Confusion Matrix


from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score

In [17]: # Confusion Matrix


cm = confusion_matrix(y_test, y_pred)

In [18]: cm

Out[18]: array([[65, 3],

[ 7, 25]], dtype=int64)

In [19]: # Accuracy Score


accuracy_score(y_test, y_pred)

Out[19]: 0.9

You might also like