Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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}}
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_2

Step: 3

blur-text-image_3

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

Database And Expert Systems Applications 23rd International Conference Dexa 2012 Vienna Austria September 2012 Proceedings Part 1 Lncs 7446

Authors: Stephen W. Liddle ,Klaus-Dieter Schewe ,A Min Tjoa ,Xiaofang Zhou

2012th Edition

3642325998, 978-3642325991

More Books

Students also viewed these Databases questions

Question

Why could the Robert Bosch approach make sense to the company?

Answered: 1 week ago