when calculating parameter p(secret / spam) I get the error: can only concatenate list (not “int”) to list
So I tried to find the problem by first scanning the values of:
for w in vocabulary:
y = ham_count[w].sum()
and then I got the error: list indices must be integers, not str
However, my code looks the same compared with the solution provided >> Chapter Calculating Parameters, The Finale training set and Calculating Constants First.
So, could someone please help me out please? Many thanks!
Replace this line with your code
What I expected to happen:
Finishing first part of the algorithm
What actually happened:
Replace this line with the output/error
can only concatenate list (not "int") to list
<!--Enter other details below: -->
Not entirely sure about comparison with this part,
I am assuming vocabulary and z are both lists here and the Vocabulary list is a collection of words. So the code for w in vocabulary: translates to for each word that exists in the vocabulary list.
the variable w can then take values like “hello”, “chat”, “won”, “lottery” etc.
when the parser comes across this, this will get translated to z["hello"].append(y) which python list is unable to handle, as the list expects an integer index value and not a string.
Even if we replace w with an integer say 0, then also the code won’t work, as z will give us the first element in the list (suppose that z list has some elements) and then to that element value which could be str type or int type you are trying to apply append() method which is not applicable to these datatypes.