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 dictionary
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 ([...]) # 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)
{}
Expected Output (the output order may vary, what is important is the value for each word)
{'going': {'spam': 2, 'ham': 1}, 'river': {'spam': 2, 'ham': 3}, 'like': {'spam': 2, 'ham': 1}, 'deep': {'sp
am': 1, 'ham': 2}, 'love': {'spam': 1, 'ham': 2}, 'hate': {'spam': 1, 'ham': 2}}
Expected Output (the output order may vary, what is important is the value for each word)
{'going': {'spam': 2, 'ham': 1}, 'river': {'spam': 2, 'ham': 3}, 'like': {'spam': 2, 'ham': 1}, 'deep': {'spam': 1, 'ham': 2}, 'love': {'spam': 1, 'ham': 2}, 'hate': {'spam': 1, 'ham': 2}}
 i have python file i want to solve the exercise Exercise

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!