def word_freq_per_class(df):
“”"
Calculates the frequency of words in each class (spam and ham) based on a given dataframe.
Args:
df (pandas.DataFrame): The input dataframe containing email data,
with a column named 'words' representing the words in each email.
Returns:
dict: A dictionary containing the frequency of words in each class.
The keys of the dictionary are words, and the values are nested dictionaries with keys
'spam' and 'ham' representing the frequency of the word in spam and ham emails, respectively.
"""
word_freq_dict = {}
### START CODE HERE ###
# Hint: You can use the iterrows() method to iterate over the rows of a dataframe.
# This method yields an index and the data in the row so you can ignore the first returned value.
for i in range(1, len(df.axes[0])):
# Iterate over the words in each email
for word in df.loc[i]['words'] :
# Check if word doesn't exist within the dictionary
if word not in word_freq_dict:
#if word_freq_dict.has_key(word) :
# If word doesn't exist, initialize the count at 0
word_freq_dict[word] = {'spam': 0, 'ham': 0}
# Check if the email was spam
match df["spam"].loc[df.index[i]]:
case 0:
# If ham then add 1 to the count of ham
word_freq_dict[word]['ham'] += 1
case 1:
# If spam then add 1 to the count of spam
word_freq_dict[word]['spam'] += 1
### END CODE HERE ###
return word_freq_dict
this code gives the correct answer . However it is graded 0 . Can someone please help