Question: Q1. Naive Bayes: Code [25] In this question, you will learn to build a Naive Bayes Classifier for the binary classification task. 1. Dataset:

image

Q1. Naive Bayes: Code [25] In this question, you will learn to build a Naive Bayes Classifier for the binary classification task. 1. Dataset: "Financial Phrasebank" dataset from HuggingFace. 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. 2. 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. 3. Clean the dataset with the steps from the previous assignment and build a vocabulary of all the words. 4. Compute the prior probability of each class p(ci) = count(ci) N Here, count(c) is the number of samples with class c; and N is the total number of samples in the dataset. 5. Compute the likelihood p(w;/c) for a all words w; and all classes c with following equation: p(wi|c) = count (w, c) +1 |V| + wey count(w, c) Here, the count(w, c) is the frequency of the word w; in class c while wey 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. 6. 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 = argmax(log p(c) + log p(w;/c)) CC WiV (3.1) 7. Using the metrics from scikit-learn library, calculate the accuracy and macro-average precision, recall, and F1 score, and also provide the confusion matrix on the test set.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Algorithms Questions!