C2_W2 create_transition_matrix (Errors)

Working on create_emission_matrix in C2_W2, but it keeps giving this error, which is pointing to the pre-given code. Anyone have any tips? Thank you!

in create_emission_matrix(alpha, tag_counts, emission_counts, vocab)
17
18 # Get a list of all POS tags
—> 19 all_tags = sorted(tag_counts.keys())
20
21 # Get the total number of unique words in the vocabulary

TypeError: ‘<’ not supported between instances of ‘int’ and ‘str’

Is it posssible that when you create tag_counts dictionary in exercise 1, you insert strings and integers when counting?

I have this same error and I don’t understand how it happened. I finally did this " all_tags = sorted([x for x in tag_counts.keys() if isinstance(x, str)])
" and that removed whatever lingering int keys I had that I didn’t intend to have.

1 Like

Thanks for the help!

Hi Lynleaf,
I don’t know if your problem is solved yet, but you can solve this by going back to the first exercise and replacing every “tag” between “### START CODE HERE ###” and “### END CODE HERE ###” with “str(tag)”,
this will ensure all the tags are considered strings, even the numeric ones.

Hi Danial,
Thanks for your reply! I have figured out the problem.

glad to hear that,
By the way, have you faced any problems with the testing cell for the viterbi_backward function?
I’m getting the following:
“Wrong values for pred list.
Expected: [‘PRP’, ‘MD’, ‘RB’, ‘VB’, ‘PRP’, ‘RB’, ‘IN’, ‘PRP’, ‘.’, ‘–s–’].
Got: [‘PRP’, ‘MD’, ‘RB’, ‘VB’, ‘PRP’, ‘RB’, ‘IN’, ‘PRP’, ‘.’, ‘#’].
3 Tests passed
1 Tests failed”

How did you solve the problem?

Easy, solved it as well:)

When defining your create_transition_matrix:
Replace count_prev_tag = tag_counts[i]
with count_prev_tag = tag_counts[all_tags[i]]
if you made that mistake. Then you should not end up with integers in the tag_counts keys.

3 Likes

I have solved the problem! Thanks