Answered step by step
Verified Expert Solution
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
Your task is to implement the function that generates a dictionary, recording the frequency with which each word in the dataset appears as spam or ham
In : import numpy as np
def getwordfrequency X Y:
"I"!
Calculate the frequency of each word in a set of emails categorized as spam or not spam
Parameters:
X numpyarray: Array of emails, where each email is represented as a list of words.
Y numpyarray: Array of labels corresponding to each email in X indicates spam, indicates ham.
Returns:
worddict dict: A dictionary where keys are unique words found in the emails, and values
are dictionaries containing the frequency of each word for spam and not spam emails.
# Creates an empty dictionary
worddict
### START CODE HERE ###
### END CODE HERE ###
return worddict
# Example usage:
# Assuming we have numpy arrays and with the email data and corresponding labels:
# nparray # Replace with actual email text data split into words
# nparray # Replace with actual labels or
# wordfreqdict getwordfrequency
# printwordfreqdict
In : testoutput getwordfrequencylike'going','river'love 'deep', 'river'hate'river'
printtestoutput
Expected Output the output order may vary, what is important is the value for each word
going: spam: 'ham': 'river': spam: 'ham': 'like': spam: 'ham': 'deep': sp
am: 'ham': 'love': spam: 'ham': 'hate': spam: 'ham':
Expected Output the output order may vary, what is important is the value for each word
going: spam: 'ham': 'river': spam: 'ham': 'like': spam: 'ham': 'deep': spam: 'ham': 'love': spam: 'ham': 'hate': spam: 'ham':
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started