Complete trainnaivebayes to 100% match the expected output: UNQC2 GRADED FUNCTION:…
- User
Complete train_naive_bayes to 100% match the expected output:
UNQ_C2 GRADED FUNCTION: train_naive_bayes
def train_naive_bayes(freqs, train_x, train_y): ''' Input: freqs: dictionary from (word, label) to how often the word appears train_x: a list of tweets train_y: a list of labels corresponding to the tweets (0,1) Output: logprior: the log prior. (equation 3 above) loglikelihood: the log likelihood of you Naive bayes equation. (equation 6 above) ''' loglikelihood = {} logprior = 0
### START CODE HERE ### # calculate V, the number of unique words in the vocabulary vocab = None V = None # calculate N_pos, N_neg, V_pos, V_neg N_pos = N_neg = None for pair in freqs.keys(): # if the label is positive (greater than zero) if pair[1] > 0: # Increment the number of positive words by the count for this (word, label) pair N_pos += None # else, the label is negative else: # increment the number of negative words by the count for this (word,label) pair N_neg += None # Calculate D, the number of documents D = None # Calculate D_pos, the number of positive documents D_pos = None # Calculate D_neg, the number of negative documents D_neg = None # Calculate logprior logprior = None # For each word in the vocabulary... for word in vocab: # get the positive and negative frequency of the word freq_pos = None freq_neg = None # calculate the probability that each word is positive, and negative p_w_pos = None p_w_neg = None # calculate the log likelihood of the word loglikelihood[word] = None ### END CODE HERE ### return logprior, loglikelihoodTest 1: logprior, loglikelihood = train_naive_bayes(freqs, train_x, train_y) print(logprior) print(len(loglikelihood))
Expected Output of test 1: 0.0 9165
Math used:
2 - Train your Model using Naive Bayes
Naive bayes is an algorithm that could be used for sentiment analysis. It takes a short time to train and also has a short prediction time.
So how do you train a Naive Bayes classifier?
- The first part of training a naive bayes classifier is to identify the number of classes that you have.
- You will create a probability for each class. is the probability that the document is positive. is the probability that the document is negative. Use the formulas as follows and store the values in a dictionary:
- Assistant
def train_naive_bayes(freqs, train_x, train_y): ''' Input: freqs: dictionary from (word, label) to how often the word appears train_x: a list of tweets train_y: a list of labels corresponding to the tweets (0,1) Output: logprior: the log prior. (equation 3 above) loglikelihood: the log likelihood of your Naive bayes equation. (equation 6 above) ''' import numpy as np loglikelihood = {} logprior = 0 ### START CODE HERE ### # calculate V, the number of unique words in the vocabulary vocab = set([pair[0] for pair in freqs.keys()]) V = len(vocab) # calculate N_pos and N_neg N_pos = 0 N_neg = 0 for pair in freqs.keys(): # if the label is positive (greater than zero) if pair[1] > 0: # Increment the number of positive words by the count for this (word, label) pair N_pos += freqs[pair] # else, the label is negative else: # increment the number of negative words by the count for this (word,label) pair N_neg += freqs[pair] # Calculate D, the number of documents D = len(train_y) # Calculate D_pos, the number of positive documents (tweets) D_pos = sum(train_y) # Calculate D_neg, the number of negative documents (tweets) D_neg = D - D_pos # Calculate logprior logprior = np.log(D_pos) - np.log(D_neg) # For each word in the vocabulary... for word in vocab: # get the positive and negative frequency of the word freq_pos = freqs.get((word, 1), 0) freq_neg = freqs.get((word, 0), 0) # calculate the probability that each word is positive, and negative p_w_pos = (freq_pos + 1) / (N_pos + V) p_w_neg = (freq_neg + 1) / (N_neg + V) # calculate the log likelihood of the word loglikelihood[word] = np.log(p_w_pos / p_w_neg) ### END CODE HERE ### return logprior, loglikelihood