Question: i have python file i want to solve the exercise Exercise 1 Your task is to implement the function that generates a dictionary, recording the

i have python file i want to solve the exercise
Exercise 1
Your task is to implement the function that generates a dictionary, recording the frequency with which each word in the dataset appears as spam (1) or ham
(0).
In [1]:
import numpy as np
def get_word_frequency (x,Y):
"I"!
Calculate the frequency of each word in a set of emails categorized as spam (1) or not spam (0).
Parameters:
X (numpy.array): Array of emails, where each email is represented as a list of words.
Y (numpy.array): Array of labels corresponding to each email in X.1 indicates spam, 0 indicates ham.
Returns:
word_dict (dict): A dictionary where keys are unique words found in the emails, and values
are dictionaries containing the frequency of each word for spam (1) and not spam (0) emails.
"'"!
# Creates an empty dichar lab
word_dict ={}
### START CODE HERE ###
### END CODE HERE ###
return word_dict
# Example usage:
# Assuming we have numpy arrays x and Y with the email data and corresponding labels:
# x= np.array([...]) # Replace with actual email text data split into words
# Y= np.array [[dots]) # Replace with actual labels (0 or 1)
# word_freq_dict = get_word_frequency (x,Y)
# print(word_freq_dict)
In [2]: test_output = get_word_frequency(['like','going','river'],['love', 'deep', 'river'],['hate', 'river']],[1,0,0])
print(test_output)

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 Databases Questions!