Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Q 1 . Naive Bayes: Code [ 2 5 ] In this question, you will learn to build a Naive Bayes Classifier for the binary

Q1. Naive Bayes: Code [25]
In this question, you will learn to build a Naive Bayes Classifier for the binary classification
task.
Dataset: "Financial Phrasebank" dataset from HuggingFace. ?1 To load the data, you
need to install library "datasets" (pip install datasets) and then use load_datset ()
method to load the dataset. You can find the code on the link provided above.
The dataset contains 3 class labels, neutral (1), positive (2), and negative (0). Consider
only positive and negative samples and ignore the neutral samples. Use 80% of the
samples selected randomly to train the model and the remaining 20% for the test.
Clean the dataset with the steps from the previous assignment and build a vocabulary of
all the words.
Compute the prior probability of each class
p(ci)=count(ci)N
Here, count(ci) is the number of samples with class ci and N is the total number of
samples in the dataset.
Compute the likelihood p(wi|c) for a all words wi and all classes c with following equation:
p(wi|c)=count(wi,c)+1|V|+wV?count(w,c)
Here, the count(wi,c) is the frequency of the word wi in class c while wV?count(w,c) is
the frequency of all the words in the class c. Laplace smoothing is used to avoid zero
probability in the case of a new word.
For each sample in the test set, predict class cNB which is the class with the highest
posterior probability. To avoid underflow and increase speed, use log space to predict
the class as follows:
cNB=argmaxcC(logp(c)+wiV?logp(wi|c))
Using the metrics from scikit-learn library ?2, calculate the accuracy and macro-average
precision, recall, and F1 score, and also provide the confusion matrix on the test set.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

More Books

Students also viewed these Databases questions

Question

Q2 . When would researchers choose to conduct a meta-analysis?

Answered: 1 week ago