### Build kmodes from source Source: https://github.com/nicodv/kmodes/blob/master/README.rst Installs the latest development version of kmodes by cloning the repository and running the setup script. This method is for users who want the most recent features or need to contribute to the project. ```bash git clone https://github.com/nicodv/kmodes.git cd kmodes python setup.py install ``` -------------------------------- ### Install kmodes using pip Source: https://github.com/nicodv/kmodes/blob/master/README.rst Installs the kmodes library using pip, the standard Python package installer. This is the recommended method for most users. The upgrade command ensures the latest version is installed. ```bash pip install kmodes ``` ```bash pip install --upgrade kmodes ``` -------------------------------- ### Install kmodes using conda Source: https://github.com/nicodv/kmodes/blob/master/README.rst Installs the kmodes library using conda, a popular package and environment manager, specifically from the conda-forge channel. This is convenient for users already managing their environments with conda. ```bash conda install -c conda-forge kmodes ``` -------------------------------- ### KPrototypes Clustering Mixed Data Types (Python) Source: https://context7.com/nicodv/kmodes/llms.txt This example demonstrates clustering mixed-type data (numerical and categorical) using the KPrototypes algorithm. It shows how to prepare data with mixed types, specify categorical column indices, initialize and fit the KPrototypes model, and access the cluster centroids, which include both numerical means and categorical modes. Dependencies include numpy and the KPrototypes class. ```Python import numpy as np from kmodes.kprototypes import KPrototypes # Create mixed data: market cap (numerical) + sector and country (categorical) # Columns: [market_cap, sector, country] X = np.array([ [1200.5, 'Tech', 'USA'], [340.2, 'Finance', 'UK'], [2100.0, 'Tech', 'USA'], [450.0, 'Healthcare', 'Germany'], [890.0, 'Finance', 'USA'], [1500.0, 'Tech', 'China'] ], dtype=object) # Convert first column to float X[:, 0] = X[:, 0].astype(float) # Specify which columns are categorical (1=sector, 2=country) kproto = KPrototypes(n_clusters=3, init='Cao', verbose=2, random_state=42) clusters = kproto.fit_predict(X, categorical=[1, 2]) # Centroids contain both numerical means and categorical modes print("Cluster centroids:") print(kproto.cluster_centroids_) ``` -------------------------------- ### KModes Clustering with Custom Dissimilarity (Ng's) (Python) Source: https://context7.com/nicodv/kmodes/llms.txt This example demonstrates using KModes with a custom dissimilarity measure, specifically Ng's dissimilarity, to potentially improve convergence. It shows how to import the custom function and pass it to the `cat_dissim` parameter during KModes initialization. The code fits the model and prints cost history and final labels. ```Python import numpy as np from kmodes.kmodes import KModes from kmodes.util.dissim import ng_dissim # Generate categorical data X = np.random.choice(15, (200, 8)) # Use Ng's dissimilarity measure km = KModes( n_clusters=5, init='Huang', cat_dissim=ng_dissim, n_init=3, verbose=1, random_state=42 ) # Fit and get labels labels = km.fit_predict(X) # Cost history over epochs print(f"Cost per epoch: {km.epoch_costs_}") print(f"Final labels: {labels}") ``` -------------------------------- ### KModes Clustering Categorical Data with Cao Initialization (Python) Source: https://context7.com/nicodv/kmodes/llms.txt This code illustrates clustering categorical data using KModes with the Cao initialization method, which is a density-based approach. It shows how to load data, initialize and fit the KModes model, retrieve centroids and cost, and predict labels for new data points. Cao initialization is deterministic, setting n_init to 1. ```Python import numpy as np from kmodes.kmodes import KModes # Load categorical data data = np.genfromtxt('soybean.csv', dtype=int, delimiter=',')[:, :-1] # Cao initialization is deterministic, so n_init automatically set to 1 kmodes_cao = KModes(n_clusters=4, init='Cao', verbose=1) kmodes_cao.fit(data) # Get results print('Centroids:') print(kmodes_cao.cluster_centroids_) print(f'Final cost: {kmodes_cao.cost_}') print(f'Iterations: {kmodes_cao.n_iter_}') # Predict new data points new_data = np.random.choice(10, (5, data.shape[1])) predictions = kmodes_cao.predict(new_data) print(f'Predictions for new data: {predictions}') ``` -------------------------------- ### Analyzing KModes Convergence with Epoch Costs Source: https://context7.com/nicodv/kmodes/llms.txt Illustrates how to analyze the convergence of the KModes algorithm by examining the cost reduction across epochs. It includes plotting the cost per epoch using matplotlib. Dependencies include numpy and matplotlib. ```python import numpy as np from kmodes.kmodes import KModes import matplotlib.pyplot as plt # Generate categorical data data = np.random.choice(20, (500, 12)) # Run k-modes km = KModes( n_clusters=6, init='Huang', max_iter=100, n_init=1, verbose=1, random_state=42 ) km.fit(data) # Analyze convergence print(f"Total iterations: {km.n_iter_}") print(f"Initial cost: {km.epoch_costs_[0]}") print(f"Final cost: {km.epoch_costs_[-1]}") print(f"Cost reduction: {km.epoch_costs_[0] - km.epoch_costs_[-1]}") # Plot convergence (if matplotlib available) print("\nCost per epoch:") for i, cost in enumerate(km.epoch_costs_): print(f"Epoch {i}: {cost:.2f}") # Check if converged before max_iter if km.n_iter_ < 100: print(f"\nConverged early at iteration {km.n_iter_}") else: print("\nReached maximum iterations") ``` -------------------------------- ### Basic k-modes clustering in Python Source: https://github.com/nicodv/kmodes/blob/master/README.rst Demonstrates the basic usage of the KModes algorithm from the kmodes library. It initializes random categorical data, fits the KModes model, and predicts cluster assignments. The cluster centroids are then printed. ```python 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_) ``` -------------------------------- ### Handling KModes Initialization Errors with Manual Centroids Source: https://context7.com/nicodv/kmodes/llms.txt Illustrates how to handle potential ValueError exceptions during KModes initialization, such as when requesting too many clusters for a small dataset. It demonstrates providing manual initial centroids as a fallback. ```python import numpy as np from kmodes.kmodes import KModes # Small dataset that might cause initialization issues problematic_data = np.array([ [1, 1, 1], [1, 1, 1], [2, 2, 2], [2, 2, 2] ]) # Attempt clustering with too many clusters try: km = KModes(n_clusters=5, init='Huang', verbose=1) km.fit(problematic_data) except ValueError as e: print(f"Initialization error: {e}") print("Reducing number of clusters or providing manual initialization") # Provide manual initial centroids manual_centroids = np.array([ [1, 1, 1], [2, 2, 2] ]) km = KModes(n_clusters=2, init=manual_centroids, verbose=1) labels = km.fit_predict(problematic_data) print(f"With manual initialization - Labels: {labels}") print(f"Centroids:\n{km.cluster_centroids_}") ``` -------------------------------- ### Troubleshoot kmodes Initialization Errors Source: https://github.com/nicodv/kmodes/blob/master/README.rst Addresses the ValueError encountered when the kmodes clustering algorithm cannot initialize. It suggests potential solutions such as reducing the number of clusters, exploring and visualizing data for distributions and outliers, cleaning and normalizing data, and increasing the row-to-column ratio. -------------------------------- ### KModes Clustering Categorical Data with Huang Initialization (Python) Source: https://context7.com/nicodv/kmodes/llms.txt This snippet demonstrates how to use the KModes algorithm to cluster purely categorical data. It utilizes the Huang initialization method and shows how to fit the model, predict cluster labels, and access cluster centroids and cost. Dependencies include numpy and the KModes class from kmodes.kmodes. ```Python import numpy as np from kmodes.kmodes import KModes # Create categorical dataset (100 samples, 10 features, values from 0-19) data = np.random.choice(20, (100, 10)) # Initialize k-modes with 4 clusters using Huang initialization km = KModes(n_clusters=4, init='Huang', n_init=5, verbose=1) # Fit the model and predict cluster labels clusters = km.fit_predict(data) # Access cluster centroids (modes for each feature) print("Cluster centroids:") print(km.cluster_centroids_) # Check clustering statistics print(f"Total cost: {km.cost_}") print(f"Iterations: {km.n_iter_}") print(f"Cluster labels: {clusters}") ``` -------------------------------- ### kmodes Library Citation Source: https://github.com/nicodv/kmodes/blob/master/README.rst Provides the recommended citation format for the kmodes library, suitable for academic papers and reports. It includes author, title, publication details, and URL. ```bibtex @Misc{devos2015, author = {Nelis J. de Vos}, title = {kmodes categorical clustering library}, howpublished = {\url{https://github.com/nicodv/kmodes}}, year = {2015--2024} } ``` -------------------------------- ### KModes Clustering with Parallel Execution (Python) Source: https://context7.com/nicodv/kmodes/llms.txt This snippet shows how to leverage parallel processing with KModes for faster execution, especially when running multiple initializations (`n_init`). By setting `n_jobs=-1`, the algorithm utilizes all available CPU cores. The code fits the model on a large dataset and prints the best clustering cost and cluster distribution. ```Python import numpy as np from kmodes.kmodes import KModes # Large categorical dataset data = np.random.choice(25, (1000, 15)) # Use all available CPU cores with n_jobs=-1 km = KModes( n_clusters=8, init='Huang', n_init=10, # Run 10 different initializations in parallel n_jobs=-1, # Use all CPU cores verbose=1, random_state=42 ) clusters = km.fit_predict(data) print(f"Best clustering cost: {km.cost_}") print(f"Cluster distribution: {np.bincount(clusters)}") ``` -------------------------------- ### Predicting Clusters for New Data with KPrototypes Source: https://context7.com/nicodv/kmodes/llms.txt Demonstrates how to use a fitted KPrototypes model to predict cluster assignments for new, unseen data points. The model is trained on initial data and then applied to new samples. ```python import numpy as np from kmodes.kprototypes import KPrototypes # Training data X_train = np.array([ [25.5, 'Engineer', 'USA'], [40.2, 'Manager', 'UK'], [32.1, 'Analyst', 'Canada'], [28.7, 'Engineer', 'USA'] ], dtype=object) X_train[:, 0] = X_train[:, 0].astype(float) # Train model kproto = KPrototypes(n_clusters=2, init='Huang', verbose=0, random_state=42) kproto.fit(X_train, categorical=[1, 2]) # New unseen data X_new = np.array([ [26.0, 'Engineer', 'USA'], [42.0, 'Director', 'UK'], [30.5, 'Developer', 'Canada'] ], dtype=object) X_new[:, 0] = X_new[:, 0].astype(float) # Predict clusters for new data predictions = kproto.predict(X_new, categorical=[1, 2]) print(f"Training centroids:\n{kproto.cluster_centroids_}") print(f"Predictions for new data: {predictions}") ``` -------------------------------- ### KPrototypes with Pandas DataFrames Source: https://context7.com/nicodv/kmodes/llms.txt Demonstrates how to use KPrototypes for clustering mixed-type data directly from a pandas DataFrame. It shows data preparation, model fitting, cluster assignment, and analysis of cluster centroids and statistics. Dependencies include pandas and numpy. ```python import numpy as np import pandas as pd from kmodes.kprototypes import KPrototypes # Create pandas DataFrame with mixed types df = pd.DataFrame({ 'age': [25, 45, 32, 28, 51, 38], 'income': [45000, 95000, 62000, 48000, 110000, 72000], 'education': ['Bachelor', 'Master', 'Bachelor', 'PhD', 'Master', 'Bachelor'], 'city': ['NYC', 'LA', 'Chicago', 'Boston', 'NYC', 'LA'] }) # Categorical column indices (education=2, city=3) categorical_indices = [2, 3] # KPrototypes accepts pandas DataFrames directly kproto = KPrototypes(n_clusters=3, init='Cao', verbose=1, random_state=42) clusters = kproto.fit_predict(df, categorical=categorical_indices) # Add cluster labels to DataFrame df['cluster'] = clusters print("Data with cluster assignments:") print(df) print(f"\nCluster centroids: {kproto.cluster_centroids_}") # Group by cluster to analyze print("\nCluster statistics:") print(df.groupby('cluster').agg({ 'age': 'mean', 'income': 'mean', 'education': lambda x: x.mode()[0], 'city': lambda x: x.mode()[0] })) ``` -------------------------------- ### KPrototypes Clustering Statistics Source: https://context7.com/nicodv/kmodes/llms.txt Prints statistics about a fitted KPrototypes clustering model, including cost, number of iterations, gamma value, and assigned clusters. ```python print(f"Clustering cost: {kproto.cost_}") print(f"Iterations: {kproto.n_iter_}") print(f"Gamma (weighting factor): {kproto.gamma}") print(f"Assigned clusters: {clusters}") ``` -------------------------------- ### Custom Dissimilarity Functions with KModes Source: https://context7.com/nicodv/kmodes/llms.txt Shows how to use custom dissimilarity functions, such as Jaccard or matching dissimilarity, with the KModes algorithm for clustering binary categorical data. Includes error handling for invalid data types. ```python import numpy as np from kmodes.kmodes import KModes from kmodes.util.dissim import matching_dissim, jaccard_dissim_binary # Binary categorical data (0s and 1s only) binary_data = np.random.choice([0, 1], (100, 10)) # Use Jaccard dissimilarity for binary data try: km = KModes( n_clusters=3, init='Huang', cat_dissim=jaccard_dissim_binary, n_init=5, verbose=1, random_state=42 ) clusters = km.fit_predict(binary_data) print(f"Clusters using Jaccard dissimilarity: {clusters}") print(f"Cost: {km.cost_}") except ValueError as e: print(f"Error: {e}") # Fall back to matching dissimilarity if needed km_matching = KModes( n_clusters=3, init='Cao', cat_dissim=matching_dissim, verbose=0 ) clusters = km_matching.fit_predict(binary_data) print(f"Clusters using matching dissimilarity: {clusters}") ``` -------------------------------- ### KPrototypes with Sample Weights for Emphasizing Observations Source: https://context7.com/nicodv/kmodes/llms.txt Applies KPrototypes clustering to a dataset with sample weights, allowing specific data points to have a greater influence on the clustering process. This is useful for emphasizing certain observations. ```python import numpy as np from kmodes.kprototypes import KPrototypes # Dataset with mixed types X = np.array([ [100, 'A', 'X'], [150, 'B', 'Y'], [120, 'A', 'X'], [200, 'C', 'Z'], [180, 'B', 'Y'] ], dtype=object) X[:, 0] = X[:, 0].astype(float) # Assign higher weights to certain samples (e.g., more recent data) sample_weights = [1.0, 2.0, 1.0, 1.5, 2.5] kproto = KPrototypes(n_clusters=2, init='Cao', verbose=1) kproto.fit(X, categorical=[1, 2], sample_weight=sample_weights) print(f"Weighted cluster centroids:\n{kproto.cluster_centroids_}") print(f"Labels: {kproto.labels_}") print(f"Cost: {kproto.cost_}") ``` -------------------------------- ### KPrototypes with Custom Gamma for Mixed Data Source: https://context7.com/nicodv/kmodes/llms.txt Demonstrates KPrototypes clustering on mixed numerical and categorical data with a manually set gamma value to control feature importance. Higher gamma gives more weight to categorical features. ```python import numpy as np from kmodes.kprototypes import KPrototypes # Mixed dataset with numerical and categorical columns # Columns: [age, income, education, occupation] data = np.array([ [25, 45000, 'Bachelor', 'Engineer'], [45, 95000, 'Master', 'Manager'], [32, 62000, 'Bachelor', 'Analyst'], [28, 48000, 'PhD', 'Researcher'], [51, 110000, 'Master', 'Director'] ], dtype=object) # Convert numerical columns data[:, 0] = data[:, 0].astype(float) data[:, 1] = data[:, 1].astype(float) # Higher gamma = more weight on categorical features kproto = KPrototypes( n_clusters=2, init='Huang', gamma=5.0, # Manually set gamma n_init=5, verbose=1, random_state=42 ) labels = kproto.fit_predict(data, categorical=[2, 3]) print(f"Labels: {labels}") print(f"Centroids:\n{kproto.cluster_centroids_}") ``` -------------------------------- ### Specify categorical columns for KPrototypes in Python Source: https://github.com/nicodv/kmodes/blob/master/README.rst Illustrates how to use the KPrototypes algorithm when dealing with mixed data types. The 'categorical' argument is used to specify the indices of columns that contain categorical data, while the rest are treated as numerical. ```python clusters = KPrototypes().fit_predict(X, categorical=[1, 2]) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.