Python implementations of the k-modes and k-prototypes clustering algorithms for clustering categorical data. It is currently at version 0.12.2 and sees active development with several releases per year.
pip install kmodesVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic usage of the KModes algorithm for clustering purely categorical data. Initialize the KModes estimator, fit it to your data, and retrieve the cluster assignments and centroids.
Pre-process your data to handle `np.NaN` values (e.g., fill with a specific category for categorical features, or remove rows) before passing to `kmodes`.
Upgrade your Python environment to at least 3.6. For full compatibility with the latest features, Python 3.10 or newer is recommended.
Upgrade your `scikit-learn` library to version 0.22 or newer: `pip install --upgrade scikit-learn`.
Ensure that all numerical columns are consistently typed as numeric (e.g., `float` or `int`) before passing them to `KPrototypes`. Convert string representations of numbers to their proper numeric types.
Pass a list of categorical column indices to the `categorical` parameter during `KPrototypes` initialization, e.g., `KPrototypes(..., categorical=[0, 2, 5])`.
Rename your local Python script to something other than `kmodes.py` (e.g., `my_script.py`) to avoid module name conflicts.
Consider reducing the number of clusters, cleaning or normalizing your data, exploring different initialization methods, or ensuring sufficient data density for the chosen cluster count.
Rename your Python script to something other than 'kmodes.py'. Ensure 'kmodes' is installed in your current environment using `pip install kmodes` or `conda install -c conda-forge kmodes`.
Try reducing the number of clusters (`n_clusters`), pre-processing your data, exploring different initialization methods (`init='Huang'` or `init='Cao'`), or increasing `n_init` (number of initialization runs) to find a better starting point.
Ensure all numerical columns are consistently typed as numeric (e.g., `float` or `int`) before passing them to `KPrototypes`. If using `KPrototypes`, explicitly specify categorical column indices using the `categorical` parameter; if your data is purely categorical, use `KModes`.
Pre-process your data to handle `np.NaN` values by imputation (e.g., filling with a specific category for categorical features, or mean/median for numerical features) or by removing rows/columns with missing data before passing it to `kmodes`.