Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ch2 - kNN.py - TypeError: unhashable type: 'list' #19

Open
elliottjscott-uk opened this issue Aug 15, 2020 · 2 comments
Open

Ch2 - kNN.py - TypeError: unhashable type: 'list' #19

elliottjscott-uk opened this issue Aug 15, 2020 · 2 comments

Comments

@elliottjscott-uk
Copy link

I'm getting an error with the classify0() function in the kNN.py module.

  9     for i in range(k):
 10         voteIlabel = labels[sortedDistIndicies[i]]

---> 11 classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1
12 sortedClassCount = sorted(classCount.items(), key=operator.itemgetter(1), reverse=True)
13 return sortedClassCount[0][0]

TypeError: unhashable type: 'list'

I've taken the code from the pbharrin/machinelearninginaction3x github repo so it shouldn't be down to typos. I've run the code line by line outside of the function and I get the same error at the same point when I try to run the for loop. I'm not an experienced Python coder so it's a bit beyond me to try and solve this myself. I'm loving the book so far though.

@pbharrin
Copy link
Owner

Can you please check the type of voteIlabel? It should not be a list.
You can check the type by adding print(type(voteIlabel)) after line 10 inside the for loop.
There may be some conversion that was done automatically in Python 2 but not Python 3.

@elliottjscott-uk
Copy link
Author

Thanks for getting back to me so quickly.
Adding the print statement you provided gives: <class 'list'>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants