Python implementations of the kmodes and kprototypes clustering algorithms for clustering categorical data.
Project description
kmodes
Description
Python implementations of the kmodes and kprototypes clustering algorithms. Relies on numpy for a lot of the heavy lifting.
kmodes is used for clustering categorical variables. It defines clusters based on the number of matching categories between data points. (This is in contrast to the more wellknown kmeans algorithm, which clusters numerical data based on Euclidean distance.) The kprototypes algorithm combines kmodes and kmeans and is able to cluster mixed numerical / categorical data.
Implemented are:
 kmodes [HUANG97] [HUANG98]
 kmodes with initialization based on density [CAO09]
 kprototypes [HUANG97]
The code is modeled after the clustering algorithms in scikitlearn
and has the same familiar interface.
I would love to have more people play around with this and give me feedback on my implementation. If you come across any issues in running or installing kmodes, please submit a bug report.
Enjoy!
Installation
kmodes can be installed using pip:
pip install kmodes
To upgrade to the latest version (recommended), run it like this:
pip install upgrade kmodes
Alternatively, you can build the latest development version from source:
git clone https://github.com/nicodv/kmodes.git
cd kmodes
python setup.py install
Usage
import numpy as np from kmodes.kmodes import KModes # random categorical data data = np.random.choice(20, (100, 10)) km = KModes(n_clusters=4, init='Huang', n_init=5, verbose=1) clusters = km.fit_predict(data) # Print the cluster centroids print(km.cluster_centroids_)
The examples directory showcases simple use cases of both kmodes (‘soybean.py’) and kprototypes (‘stocks.py’).
Missing / unseen data
The kmodes algorithm accepts np.NaN
values as missing values in
the X
matrix. However, users are strongly suggested to consider
filling in the missing data themselves in a way that makes sense for
the problem at hand. This is especially important in case of many missing
values.
The kmodes algorithm currently handles missing data as follows. When
fitting the model, np.NaN
values are encoded into their own
category (let’s call it “unknown values”). When predicting, the model
treats any values in X
that (1) it has not seen before during
training, or (2) are missing, as being a member of the “unknown values”
category. Simply put, the algorithm treats any missing / unseen data as
matching with each other but mismatching with nonmissing / seen data
when determining similarity between points.
The kprototypes also accepts np.NaN
values as missing values for
the categorical variables, but does not accept missing values for the
numerical values. It is up to the user to come up with a way of
handling these missing data that is appropriate for the problem at hand.
Parallel execution
The kmodes and kprototypes implementations both offer support for
multiprocessing via the
joblib library,
similar to e.g. scikitlearn’s implementation of kmeans, using the
n_jobs
parameter. It generally does not make sense to set more jobs
than there are processor cores available on your system.
This potentially speeds up any execution with more than one initialization try,
n_init > 1
, which may be helpful to reduce the execution time for
larger problems. Note that it depends on your problem whether multiprocessing
actually helps, so be sure to try that out first. You can check out the
examples for some benchmarks.
FAQ
Q: I’m seeing errors such as TypeError: '<' not supported between instances of 'str' and 'float'
when using the kprototypes
algorithm.
A: One or more of your numerical feature columns have string values in them. Make sure that all columns have consistent data types.
References
[HUANG97]  (1, 2) Huang, Z.: Clustering large data sets with mixed numeric and categorical values, Proceedings of the First Pacific Asia Knowledge Discovery and Data Mining Conference, Singapore, pp. 2134, 1997. 
[HUANG98]  Huang, Z.: Extensions to the kmodes algorithm for clustering large data sets with categorical values, Data Mining and Knowledge Discovery 2(3), pp. 283304, 1998. 
[CAO09]  Cao, F., Liang, J, Bai, L.: A new initialization method for categorical data clustering, Expert Systems with Applications 36(7), pp. 1022310228., 2009. 
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Filename, size  File type  Python version  Upload date  Hashes 

Filename, size kmodes0.10.1py2.py3noneany.whl (17.8 kB)  File type Wheel  Python version py2.py3  Upload date  Hashes View hashes 
Filename, size kmodes0.10.1.tar.gz (13.9 kB)  File type Source  Python version None  Upload date  Hashes View hashes 
Hashes for kmodes0.10.1py2.py3noneany.whl
Algorithm  Hash digest  

SHA256  bce1108382bffc09902c2fe5e1acb1cbb10736634efce2af88f195b4998f7c5e 

MD5  14a3b381013303ccd33e539444ccba46 

BLAKE2256  79c0f7d8a0eb41ac6f302b4bc100f91b6e0f2558425ccfefaa0ec0430f77ee97 