C3_w1 Assignment 1 , Exercise 8

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

1 Like

Seeing this as well.

Hi @Vrishti_Godhwani! I just submitted your exact implementation and got a 10/10. Are you still facing this issue? If yes then send me your notebook via DM and I’ll take a look. Also could you edit your post so it doesn’t show your implementation? We handle all implementation-specific discussion privately to avoid leaking answers.

Hi @Adam_Moses what error are you getting from the grader?

yes , the test case is failing again
getting 0/10
error Failed test case: word_freq_per_class has incorrect type.
Expected:
<class ‘function’>,
but got:
<class ‘NoneType’>.

Also how to edit the post?

@Vrishti_Godhwani, the issue is that you left the # grade-up-to-here tag in the cell that has the predict_breed function. The grader omits all cells after encountering this tag so it is unable to find your word_freq_per_class function and that is why you get a NoneType error.

To edit your post there is an icon of a pencil under it, you can click it and this will allow you to edit it.

Actually I resolved it, I was reusing a function call in more places than I should have.

Nice to hear you sorted it out @Adam_Moses! :slight_smile: