Giter Site home page Giter Site logo

TypeError: unhashable type about kmodes HOT 11 CLOSED

nicodv avatar nicodv commented on July 25, 2024
TypeError: unhashable type

from kmodes.

Comments (11)

nicodv avatar nicodv commented on July 25, 2024

from kmodes.

visayang2005 avatar visayang2005 commented on July 25, 2024

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])

from kmodes.

nicodv avatar nicodv commented on July 25, 2024

from kmodes.

visayang2005 avatar visayang2005 commented on July 25, 2024

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

from kmodes.

visayang2005 avatar visayang2005 commented on July 25, 2024

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

from kmodes.

nicodv avatar nicodv commented on July 25, 2024

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

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

from kmodes.

nicodv avatar nicodv commented on July 25, 2024

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.

from kmodes.

visayang2005 avatar visayang2005 commented on July 25, 2024

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

from kmodes.

nicodv avatar nicodv commented on July 25, 2024

This should no longer occur: a30da80

from kmodes.

cibic89 avatar cibic89 commented on July 25, 2024

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

from kmodes.

nicodv avatar nicodv commented on July 25, 2024

Fixed for real by: #67

from kmodes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.