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

ML Lab Program 7

The document describes a Python program that implements Naive Bayes classification to analyze text data. It reads a dataset from a CSV file containing statements labeled as positive or negative. It then splits the data, counts word frequencies, trains a Naive Bayes classifier model, and uses it to predict the labels of statements, including ones entered by the user. Key steps include preprocessing the text into word counts, training and testing the model, and evaluating it using various metrics.

Uploaded by

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

ML Lab Program 7

The document describes a Python program that implements Naive Bayes classification to analyze text data. It reads a dataset from a CSV file containing statements labeled as positive or negative. It then splits the data, counts word frequencies, trains a Naive Bayes classifier model, and uses it to predict the labels of statements, including ones entered by the user. Key steps include preprocessing the text into word counts, training and testing the model, and evaluating it using various metrics.

Uploaded by

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

2/3/24, 6:12 PM Study Glance | Machine Learning Lab Manual(Programs)

 Menu

Machine Learning - Lab Programs

Aim:

Implement naive baye's theorem to classify the English text

https://studyglance.in/labprograms/mldisplay.php?url1=ml/Week7.py&url2=ml/Statements_data.csv&url3=&url4=&title=Implement naive baye%27s the… 1/7


2/3/24, 6:12 PM Study Glance | Machine Learning Lab Manual(Programs)

Source Code:
Week7.py

''' Implement linear regression using python

=================================
Explanation:
=================================

===> To run this program you need to install the pandas Module

---> pandas Module is used to read csv files

===> To install, Open Command propmt and then execute the following comman
d

---> pip install pandas

And, then you need to install the sklearn Module

===> Open Command propmt and then execute the following command to install
sklearn Module

---> pip install scikit-learn

Finally, you need to create dataset called "Statements_data.csv" file.

===============================
Source Code :
===============================

'''
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, confusion_matrix, precision_sc
ore, recall_score

https://studyglance.in/labprograms/mldisplay.php?url1=ml/Week7.py&url2=ml/Statements_data.csv&url3=&url4=&title=Implement naive baye%27s the… 2/7


2/3/24, 6:12 PM Study Glance | Machine Learning Lab Manual(Programs)

msglbl_data = pd.read_csv('Statements_data.csv', names=['Message', 'Labe


l'])
print("The Total instances in the Dataset: ", msglbl_data.shape[0])
msglbl_data['labelnum'] = msglbl_data.Label.map({'pos': 1, 'neg': 0})
# place the data in X and Y Vectors
X = msglbl_data["Message"]
Y = msglbl_data.labelnum
# to split the data into train se and test set
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, Y)

count_vect = CountVectorizer()
Xtrain_dims = count_vect.fit_transform(Xtrain)
Xtest_dims = count_vect.transform(Xtest)
df = pd.DataFrame(Xtrain_dims.toarray(),columns=count_vect.get_feature_nam
es_out())
clf = MultinomialNB()
# to fit the train data into model
clf.fit(Xtrain_dims, Ytrain)
# to predict the test data
prediction = clf.predict(Xtest_dims)
print('******** Accuracy Metrics *********')
print('Accuracy : ', accuracy_score(Ytest, prediction))
print('Recall : ', recall_score(Ytest, prediction))
print('Precision : ',precision_score(Ytest, prediction))
print('Confusion Matrix : \n', confusion_matrix(Ytest, prediction))
print(10*"-")
# to predict the input statement
test_stmt = [input("Enter any statement to predict :")]
test_dims = count_vect.transform(test_stmt)
pred = clf.predict(test_dims)
for stmt,lbl in zip(test_stmt,pred):
if lbl == 1:
print("Statement is Positive")
else:
print("Statement is Negative")

https://studyglance.in/labprograms/mldisplay.php?url1=ml/Week7.py&url2=ml/Statements_data.csv&url3=&url4=&title=Implement naive baye%27s the… 3/7


2/3/24, 6:12 PM Study Glance | Machine Learning Lab Manual(Programs)

Statements_data.csv

This is very good place,pos


I like this biryani,pos
I feel very happy,pos
This is my best work,pos
I do not like this restaurant,neg
I am tired of this stuff,neg
I can't deal with this,neg
What an idea it is,pos
My place is horrible,neg
This is an awesome place,pos
I do not like the taste of this juice,neg
I love to sing,pos
I am sick and tired,neg
I love to dance,pos
What a great holiday,pos
That is a bad locality to stay,neg
We will have good fun tomorrow,pos
I hate this food,neg

Output:

https://studyglance.in/labprograms/mldisplay.php?url1=ml/Week7.py&url2=ml/Statements_data.csv&url3=&url4=&title=Implement naive baye%27s the… 4/7


2/3/24, 6:12 PM Study Glance | Machine Learning Lab Manual(Programs)

Related Content :

Machine Learning Lab Programs


1) The probability that it is Friday and that a student is absent is 3%. Since there are 5 school days in a week,
the probability that it is Friday is 20%. What is theprobability that a student is absent given that today is
Friday? Apply Baye’s rule in python to get the result.(Ans: 15%) View Solution

2) Extract the data from database using python View Solution

3) Implement k-nearest neighbours classification using python View Solution

https://studyglance.in/labprograms/mldisplay.php?url1=ml/Week7.py&url2=ml/Statements_data.csv&url3=&url4=&title=Implement naive baye%27s the… 5/7


2/3/24, 6:12 PM Study Glance | Machine Learning Lab Manual(Programs)

4) Given the following data, which specify classifications for nine ombinations of VAR1 and VAR2 predict a
classification for a case where VAR1=0.906 and VAR2=0.606, using the result of k-means clustering with 3
means (i.e., 3 centroids) View Solution

5) The following training examples map descriptions of individuals onto high, medium and low credit-
worthiness.Input attributes are (from left to right) income, recreation, job, status, age-group, home-owner.
Find the unconditional probability of 'golf' and the conditional probability of 'single' given 'medRisk' in the
dataset View Solution

6) Implement linear regression using python View Solution

7) Implement naive baye's theorem to classify the English text View Solution

8) Implement an algorithm to demonstrate the significance of genetic algorithm View Solution

9) Implement the finite words classification system using Back-propagation algorithm View Solution

Suggestion/Feedback Form :

Name:
Full Name
Please provide rate this topic

Excellent Very Good Good Average Poor

Please tick the boxes to show what this form is about

This is a Compliment

This is a Suggestion for improvement

This is Feedback

This is Grievance

Do you have suggestions to improve our services ?


https://studyglance.in/labprograms/mldisplay.php?url1=ml/Week7.py&url2=ml/Statements_data.csv&url3=&url4=&title=Implement naive baye%27s the… 6/7
2/3/24, 6:12 PM Study Glance | Machine Learning Lab Manual(Programs)

Type Here..

Submit

ABOUT
Study Glance provides Tutorials , Power point Presentations(ppts), Lecture Notes,
Important & previously asked questions, Objective Type questions, Laboratory programs
and we provide Syllabus of various subjects.

CA TEGO R I ES
Tutorials Questions
PPTs Lab Programs
Lecture Notes Syllabus

Copyright © 2020 All Rights Reserved by StudyGlance.

   

https://studyglance.in/labprograms/mldisplay.php?url1=ml/Week7.py&url2=ml/Statements_data.csv&url3=&url4=&title=Implement naive baye%27s the… 7/7

You might also like