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

Converting categorical variables to int / float? #1

Closed
sherrylau opened this issue Apr 14, 2015 · 5 comments
Closed

Converting categorical variables to int / float? #1

sherrylau opened this issue Apr 14, 2015 · 5 comments

Comments

@sherrylau
Copy link

Hi, I am trying to apply your k-prototypes algorithm on my dataset. I have a list of categorical and numerical attributes. Following the soybean examples, i got the following error:
Traceback (most recent call last):

File "", line 2, in
kmodes_huang.cluster(cas_orc_join_nonus_cat, init_method='Huang')

File "/Users/212448740/Desktop/python_ml/kmodes.py", line 75, in cluster
self._perform_clustering(x, _args, verbose=0, *_kwargs)

File "/Users/212448740/Desktop/python_ml/kmodes.py", line 108, in _perform_clustering
self.init_centroids(x)

File "/Users/212448740/Desktop/python_ml/kmodes.py", line 190, in init_centroids
self.centroids[ik, iattr] = random.choice(choices)

ValueError: could not convert string to float

Is that mean i have to convert all the categorical attributes into 0, 1, 2, etc for the function to works?

Sherry

@sherrylau
Copy link
Author

Actually that error is from k-modes.cluster instead of k-prototypes.cluster

@nicodv
Copy link
Owner

nicodv commented Apr 15, 2015

Yes, that seems to be a limitation. Let me see what I can do. Was planning a significant rewrite anyway.

For now, you could use sklearn's LabelEncoder to convert to integers: http://scikit-learn.org/dev/modules/generated/sklearn.preprocessing.LabelEncoder.html

@sherrylau
Copy link
Author

I will try that! Thanks for your quick response!

@nicodv
Copy link
Owner

nicodv commented Apr 16, 2015

Thanks for the motivation fo rewriting the package. :)

Try again, using strings directly should now work.

@nicodv nicodv closed this as completed Apr 16, 2015
@sherrylau
Copy link
Author

Thanks! Glad that you wrote this package!

nicodv pushed a commit that referenced this issue May 6, 2016
nicodv added a commit that referenced this issue May 6, 2016
Merge pull request #1 from nicodv/master
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