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

TypeError: unhashable type #40

Closed
visayang2005 opened this issue Apr 17, 2017 · 11 comments
Closed

TypeError: unhashable type #40

visayang2005 opened this issue Apr 17, 2017 · 11 comments
Labels

Comments

@visayang2005
Copy link

when i commit the code below:

clustermodel=kprototypes.KPrototypes(n_clusters=6, init='Cao', verbose=2)
clustermodel.fit(sd1,categorical=[0,1])

it can't run with the " TypeError: unhashable type", but kmode is ok. could you pls tell me how can solve this problem? Thank very much!

@nicodv
Copy link
Owner

nicodv commented Apr 17, 2017 via email

@visayang2005
Copy link
Author

ok, my full code like this,

from kmodes import kmodes
from kmodes import kprototypes
import pandas as pd
from sklearn import cluster

the first two varibles of dataframe sd1 is categorical, other varibles is continus

sd1=sd[['state','newmode_id','at_price_scaled','day_diff_scaled', 'time_avg_rate_scaled', 'total_dealnum_scaled', 'total_dealprice_scaled', 'total_dealnum_fws_scaled']]

clustermodel=kprototypes.KPrototypes(n_clusters=6, init='Cao', verbose=2)
cluser=clustermodel.fit_predict(sd1,categorical=[0,1])

@nicodv
Copy link
Owner

nicodv commented Apr 17, 2017 via email

@visayang2005
Copy link
Author

sorry, pls ignore before, use this:

cluser=clustermodel.fit_predict(sd1,categorical=[0,1])
Traceback (most recent call last):

File "", line 1, in
cluser=clustermodel.fit_predict(sd1,categorical=[0,1])

File "build\bdist.win-amd64\egg\kmodes\kmodes.py", line 374, in fit_predict
return self.fit(X, **kwargs).labels_

File "build\bdist.win-amd64\egg\kmodes\kprototypes.py", line 415, in fit
self.verbose)

File "build\bdist.win-amd64\egg\kmodes\kprototypes.py", line 154, in k_prototypes
Xnum, Xcat = _split_num_cat(X, categorical)

File "build\bdist.win-amd64\egg\kmodes\kprototypes.py", line 47, in _split_num_cat
if ii not in categorical]]).astype(np.float64)

File "D:\Anaconda2\lib\site-packages\pandas\core\frame.py", line 2059, in getitem
return self._getitem_column(key)

File "D:\Anaconda2\lib\site-packages\pandas\core\frame.py", line 2066, in _getitem_column
return self._get_item_cache(key)

File "D:\Anaconda2\lib\site-packages\pandas\core\generic.py", line 1384, in _get_item_cache
res = cache.get(item)

TypeError: unhashable type

@visayang2005
Copy link
Author

Hi Nico,

The full error like this, can you help me to solve it, I really appreciate your help.

cluser=clustermodel.fit_predict(sd1,categorical=[0,1])
Traceback (most recent call last):

File "", line 1, in
cluser=clustermodel.fit_predict(sd1,categorical=[0,1])

File "build\bdist.win-amd64\egg\kmodes\kmodes.py", line 374, in fit_predict
return self.fit(X, **kwargs).labels_

File "build\bdist.win-amd64\egg\kmodes\kprototypes.py", line 415, in fit
self.verbose)

File "build\bdist.win-amd64\egg\kmodes\kprototypes.py", line 154, in k_prototypes
Xnum, Xcat = _split_num_cat(X, categorical)

File "build\bdist.win-amd64\egg\kmodes\kprototypes.py", line 47, in _split_num_cat
if ii not in categorical]]).astype(np.float64)

File "D:\Anaconda2\lib\site-packages\pandas\core\frame.py", line 2059, in getitem
return self._getitem_column(key)

File "D:\Anaconda2\lib\site-packages\pandas\core\frame.py", line 2066, in _getitem_column
return self._get_item_cache(key)

File "D:\Anaconda2\lib\site-packages\pandas\core\generic.py", line 1384, in _get_item_cache
res = cache.get(item)

TypeError: unhashable type

@nicodv
Copy link
Owner

nicodv commented Apr 17, 2017

Try presenting a numpy array to the algorithm, instead of a pandas DataFrame:

cluster=clustermodel.fit_predict(sd1.values,categorical=[0,1])

@nicodv nicodv added the bug label Apr 17, 2017
@nicodv
Copy link
Owner

nicodv commented Apr 17, 2017

Since it seems reasonable to have users present pandas DataFrames as inputs to the algorithm, it's probably a good idea to include a check (e.g., 'pandas' in str(X.__class__)) and do a X.values if True.

Alternatively, make sure all operations (such as the one that caused this bug) support DataFrames, but that's hard to implement and check consistently.

@visayang2005
Copy link
Author

Thanks, Nico. My problem is solved by using a numpy array to the algorithm.

@nicodv
Copy link
Owner

nicodv commented Jun 6, 2017

This should no longer occur: a30da80

@nicodv nicodv closed this as completed Jun 6, 2017
@nicodv nicodv mentioned this issue Nov 15, 2017
@cibic89
Copy link

cibic89 commented Dec 21, 2017

This is not fixed in the latest version. Added the .values to the dataframe and worked like a charm.
The type error is "TypeError: unhashable type: slice"

Snippet from kprototypes:

# Convert pandas objects to numpy arrays.
if 'pandas' in str(X.__class__):
X = X.values

@nicodv
Copy link
Owner

nicodv commented Jan 10, 2019

Fixed for real by: #67

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

No branches or pull requests

3 participants