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

ML Assignment-7

Uploaded by

hiren.patel24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

ML Assignment-7

Uploaded by

hiren.patel24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical No: – 7

Name: Hiren Daxeshbhai Patel Roll No.: 07

Title: Write a Program to Implement the Naïve Bayesian Classifier for a Sample Training Data
Set Stored as a .CSV File. Compute the Accuracy of the Classifier, Considering Few Test Data
Sets.

Software Requirement:

• Python
• NumPy
• scikit-learn
• Jupyter Notebook
Source Code:

import pandas as pd
from sklearn.model_selection import train_test_split from
sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score

# Load the training dataset from a CSV file data


= pd.read_csv('sample_data.csv')

# Separate features and labels X


= data[['Feature1', 'Feature2']] y
= data['Label']

# Split the dataset into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Create and train the Naïve Bayes


Classifier nb_classifier = GaussianNB()
nb_classifier.fit(X_train, y_train)

# Predict the labels for the test set


y_pred = nb_classifier.predict(X_test)
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy of the Naïve Bayes Classifier: {accuracy * 100:.2f}%")

# Example test dataset for additional predictions


test_data = pd.DataFrame({
'Feature1': [5.0, 6.0],
'Feature2': [3.4, 3.1]
})

# Predict the labels for the additional test dataset


test_predictions = nb_classifier.predict(test_data)
print("Predictions for the additional test dataset:") for
i, prediction in enumerate(test_predictions):
print(f"Test Sample {i + 1}: Predicted Label = {prediction}")

Output:

Conclusions:

The Naïve Bayes classifier has been implemented successfully using the provided sample
dataset. The classifier was trained on the training data and evaluated on test data to compute its
accuracy. The high accuracy indicates that the classifier performs well in predicting class labels
for the given test samples. This implementation demonstrates the effectiveness of Naïve Bayes in
handling classification tasks with probabilistic modeling.

You might also like