kmodes

repository·master·Indexed 23 days ago

https://github.com/nicodv/kmodes

A Python library providing implementations of k-modes and k-prototypes clustering algorithms for categorical and mixed-type data. It follows the scikit-learn API pattern and supports parallel execution via joblib using the n_jobs parameter.

Tokens
884
Snippets
3
Records
5
Agent score
30%

What's inside kmodes

  1. Enable parallel execution with n_jobs

    master

    Both k-modes and k-prototypes support multiprocessing via the joblib library. You can enable this by setting the n_jobs parameter.

    Parallel execution is most effective when n_init > 1, as it can speed up the execution of multiple initialization tries. It is generally recommended not to set more jobs than there are available processor cores on your system.

  2. Use KPrototypes for mixed numerical and categorical data

    master

    The KPrototypes algorithm combines k-modes and k-means to cluster datasets containing both numerical and categorical features.

    When using KPrototypes, you must specify which columns are categorical using the categorical argument (a list of column indices). All other columns are treated as numerical.

    clusters = KPrototypes().fit_predict(X, categorical=[1, 2])
  3. Install kmodes

    master

    You can install kmodes using pip, conda, or by building from source.

    Using pip:

    pip install kmodes

    To upgrade to the latest version:

    pip install --upgrade kmodes

    Using conda: Install from the conda-forge channel:

    conda install -c conda-forge kmodes

    Building from source:

    git clone https://github.com/nicodv/kmodes.git
    cd kmodes
    python setup.py install
    pip install kmodes
  4. Troubleshoot kmodes errors

    master

    Common Errors and Solutions

    TypeError: '<' not supported between instances of 'str' and 'float'

    • Cause: One or more numerical feature columns contain string values.
    • Solution: Ensure all columns have consistent data types.

    ModuleNotFoundError: No module named 'kmodes.kmodes'; 'kmodes' is not a package

    • Cause: Your local script is named kmodes.py, which shadows the installed package.
    • Solution: Rename your file to something else.

    ValueError: Clustering algorithm could not initialize. Consider assigning the initial clusters manually.

    • Cause: The algorithm cannot find meaningful clusters with the current parameters.
    • Solutions:
      • Run with fewer clusters.
      • Clean, normalize, or visualize your data to check for outliers/distributions.
      • Increase the ratio of rows to columns.

    ValueError: Input contains NaN, infinity, or a value too large for dtype('float64').

    • Cause: The input matrix X contains np.NaN values.
    • Solution: Fill in missing data before passing it to the algorithm.

    How do I specify categorical columns in KPrototypes?

    • Solution: Use the categorical argument with a list of indices. Example: KPrototypes().fit_predict(X, categorical=[1, 2]).
  5. Use KModes for categorical clustering

    master

    The KModes class is used for clustering categorical variables. It defines clusters based on the number of matching categories between data points. The API is modeled after scikit-learn.

    To use it, import KModes from kmodes.kmodes, initialize it with parameters like n_clusters and init, and then call fit_predict(data) to get the cluster assignments. You can access the cluster centroids via the cluster_centroids_ attribute.

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