Professional Documents
Culture Documents
Machine Learning Program 4 (SHANKAR)
Machine Learning Program 4 (SHANKAR)
No: 4
NAIVE BAYESIAN CLASSIFIER FOR A SAMPLE TRAINING
DATE: DATA SET STORED AS A CSV FILE
AIM:
The aim of this experiment is to use Naive Bayes classifier and its application
for classification tasks. and implement the Naive Bayes algorithm using a sample
training dataset stored in a CSV file.
HARDWARE SPECIFICATION:
Processor : Apple M1
Installed RAM : 8.00 GB
SOFTWARE SPECIFICATION:
LIBRARIES:
NumPy
Pandas
Sklearn
ALGORITHM:
2. Library Imports:
Begin by importing necessary libraries like numpy, matplotlib,
pandas, and scikit-learn.
3.Data Loading:
Load the dataset named "Social_Network_Ads.csv" into a pandas
DataFrame.
SUDARSAN R
21EE113
4.Data Preparation:
Separate the independent variables (features) as 'x' and the
dependent variable as 'y'.
6.Train-Test Split:
Split the dataset into training and testing sets using the
train_test_split function from scikit-learn.
7.Feature Scaling:
Normalize the features using StandardScaler from scikit-learn to
ensure fair comparison.
8.Model Training:
Initialize a Gaussian Naive Bayes classifier and train it using the
training data.
9.Prediction:
Make predictions on the test set using the trained model.
11.Model Evaluation:
I. Confusion Matrix:
Compute the confusion matrix to assess the model's
performance.
II. Accuracy Score:
Calculate the accuracy score to gauge the model's
effectiveness.
SUDARSAN R
21EE113
12.Result Display:
Print the confusion matrix and accuracy score to understand the
model's performance.
PROGRAM:
# Train-test splitting
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.20,
random_state=0)
# Feature scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)
SUDARSAN R
21EE113
classifier.fit(x_train, y_train)
y_pred = classifier.predict(x_test)
# printing values
print("\nActual y_test values:")
print(y_test)
print("\nPredicted y_pred values:")
print(y_pred)
OUTPUT:
Python 3.12.1 (tags/v3.12.1:2305ca5, Dec 7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)]
on win32
Type "help", "copyright", "credits" or "license()" for more information.
= RESTART: C:/Users/DELL/OneDrive/Documents/agni/program 4/ML program 04.py
X values:
Confusion matrix:
[[58 0]
[ 0 22]]
Accuracy score:
1.0
SUDARSAN R
21EE113
INFERENCE:
SUDARSAN R
21EE113
RUBRICS:
RESULT:
SUDARSAN R
21EE113