(Solution code removed, as posting it publicly is against the honour code of this community, regardless if it is correct or not)
Can someone let me know what’s wrong am I doing?
First, the get method uses the value
(the second argument) when the dictionary does not have the key (in this case a tuple), so you should have 0.0 in both cases (.get(tuple, 0)
) - it means that if we do not have the tuple, the count is 0.
Second, the tuple is constructed in such a way (word, 1 or 0) depending if positive or negative. I think the hints explain it better:
- For each word, check the ‘freqs’ dictionary for the count when that word has a positive ‘1’ label. (Check for the key (word, 1.0)
- Do the same for the count for when the word is associated with the negative label ‘0’. (Check for the key (word, 0.0).)
So in the elif
case the tuple should be (word, 0.0)
and not (word,1)
Cheers
{moderator edit: code removed - please do not post your code on the forum}
In the final testing, 4 test cases are passed and 4 test cases are failed with the attached code.
The way your if .. elif .. else
statement is constructed is flawed. Think of what each case is checking.
With the help of .get
method you do not need if
statements at all. But you can implement the assignment function with if
s if you want, but make sure what are you checking for.
In any case, you still need the tuple to pass to the .get
method, since the freqs
dictionary’s keys are tuples (word, 1 or 0). So in your code the freqs.get(word, 1.0)
will always return 1.0 and freqs.get(word, 0.0)
will always return 0.0.
The array x
may be populated without if
statements - using only freqs.get((word, 1.0), 0)
and freqs.get((word, 0.0), 0)
.
i was try with if…elif…else and also with get method but half of the testcases pass and half of the test cases failed. let me how to resolve this problem sir
I could not understand your query.
extract_features section. when i use if … elif… else statement 8 test case passed and 8 test cases failed.and unexpected output occures when i use x[1] += freqs.get((word, 1.0), 0) like this i got an error in exercise4 predict_tweet section like AttributeError: ‘numpy.ndarray’ object has no attribute ‘get’ so how to resolve this error please let me know