### Install Dependencies with requirements.txt Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/installation.html Install all project dependencies listed in the requirements.txt file using pip. ```bash pip install -r requirements.txt ``` -------------------------------- ### Clone Repository and Install Dependencies (Manual) Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/_sources/installation.rst.txt Clone the FuzzyCMeans repository and install its dependencies using pip and a requirements file. ```bash git clone https://github.com/your-repo/fuzzy-c-means cd fuzzy-c-means ``` ```bash pip install -r requirements.txt ``` -------------------------------- ### Install Dependencies with Poetry Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/installation.html Use Poetry to install project dependencies and automatically create a virtual environment. ```bash poetry install ``` -------------------------------- ### Install Poetry Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/installation.html Install the Poetry dependency manager using pip if it is not already installed. ```bash pip install poetry ``` -------------------------------- ### Install FuzzyCMeans with pip Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/installation.html Install the FuzzyCMeans package using pip after activating your virtual environment. ```bash pip install scikit-fuzzy-c-means ``` -------------------------------- ### Install Dependencies with Poetry Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/_sources/installation.rst.txt Use Poetry to install project dependencies and automatically create a virtual environment. This command should be run after cloning the repository. ```bash git clone https://github.com/your-repo/fuzzy-c-means cd fuzzy-c-means ``` ```bash poetry install ``` -------------------------------- ### Activate Poetry Shell Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/installation.html Activate the virtual environment managed by Poetry to start working on the project. ```bash poetry shell ``` -------------------------------- ### Clone Repository and Navigate Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/installation.html Clone the FuzzyCMeans repository from GitHub and navigate into the project directory. ```bash git clone https://github.com/your-repo/fuzzy-c-means cd fuzzy-c-means ``` -------------------------------- ### K-Means++ Initialization for Cluster Centers Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/algorithm.html Initializes cluster centers using the K-Means++ algorithm to ensure better initial placement and faster convergence. It selects initial centers probabilistically based on squared distances to existing centers. ```python def _kmeans_plusplus_init(self, X): np.random.seed(self.random_state) centers = [] initial_idx = np.random.choice(n_samples) centers.append(X[initial_idx]) for _ in range(1, self.n_clusters): distances = np.min(cdist(X, np.array(centers)), axis=1) probabilities = distances**2 / np.sum(distances**2) next_idx = np.random.choice(n_samples, p=probabilities) centers.append(X[next_idx]) return np.array(centers) ``` -------------------------------- ### Create Virtual Environment (venv) Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/installation.html Use this command to create a virtual environment for isolating project dependencies. Activate it using the appropriate command for your operating system. ```bash python -m venv .venv source .venv/bin/activate # On macOS/Linux # OR .venv\Scripts\activate # On Windows ``` -------------------------------- ### Fuzzy C-Means Constructor Initialization Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/algorithm.html Initializes the FuzzyCMeans class with parameters for number of clusters, fuzziness coefficient, maximum iterations, convergence tolerance, and random state for reproducibility. ```python def __init__(self, n_clusters=3, m=2, max_iter=150, tol=1e-4, random_state=None): self.n_clusters = n_clusters self.m = m self.max_iter = max_iter self.tol = tol self.random_state = random_state ``` -------------------------------- ### Select Best Parameters and Evaluate Clustering Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/usage.html This snippet demonstrates how to select the optimal parameters for Fuzzy C-Means by sorting results based on the silhouette score, fitting the model, predicting memberships, and evaluating the clustering using Silhouette, Davies-Bouldin, and Calinski-Harabasz indices. ```python best_params = results_df.sort_values("silhouette_score", ascending=False).iloc[0] fcm_best = FuzzyCMeans( n_clusters=int(best_params["n_clusters"]), m=best_params["m"], random_state=42 ) fcm_best.fit(X_train) # Predict memberships for the validation data U_val_best = fcm_best._compute_memberships(X_val) y_pred_best = U_val_best.argmax(axis=1) # Evaluate using all metrics silhouette = silhouette_score(X_val, y_pred_best) davies_bouldin = davies_bouldin_score(X_val, y_pred_best) calinski_harabasz = calinski_harabasz_score(X_val, y_pred_best) print(f"Silhouette Score: {silhouette:.3f}") print(f"Davies-Bouldin Index: {davies_bouldin:.3f}") print(f"Calinski-Harabasz Index: {calinski_harabasz:.3f}") ``` -------------------------------- ### Fit Fuzzy C-Means Model and Visualize Centers Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/_sources/usage.rst.txt Initializes and fits a Fuzzy C-Means model to the training data. Visualizes the computed cluster centers on the training data. ```python # Initialize and fit the Fuzzy C-Means model fcm = FuzzyCMeans(n_clusters=3, m=2, random_state=42) fcm.fit(X_train) # Visualize the fuzzy cluster centers sns.scatterplot(x=X_train[:, 0], y=X_train[:, 1], color="gray", alpha=0.6) sns.scatterplot( x=fcm.cluster_centers_[:, 0], y=fcm.cluster_centers_[:, 1], color="red", s=100, label="Cluster Centers", ) plt.title("Fuzzy Cluster Centers on Training Data") plt.xlabel("Feature 1") plt.ylabel("Feature 2") plt.legend() plt.show() ``` -------------------------------- ### Select Best Parameters and Evaluate Final Model Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/_sources/usage.rst.txt Selects the best hyperparameters based on the silhouette score from the tuning results. Fits the Fuzzy C-Means model with the best parameters and evaluates its performance on the validation data, printing the key metrics. ```python # Select best parameters best_params = results_df.sort_values("silhouette_score", ascending=False).iloc[0] fcm_best = FuzzyCMeans( n_clusters=int(best_params["n_clusters"]), m=best_params["m"], random_state=42, ) fcm_best.fit(X_train) # Predict memberships for the validation data U_val_best = fcm_best._compute_memberships(X_val) y_pred_best = U_val_best.argmax(axis=1) # Evaluate using all metrics silhouette = silhouette_score(X_val, y_pred_best) davies_bouldin = davies_bouldin_score(X_val, y_pred_best) calinski_harabasz = calinski_harabasz_score(X_val, y_pred_best) print(f"Silhouette Score: {silhouette:.3f}") print(f"Davies-Bouldin Index: {davies_bouldin:.3f}") print(f"Calinski-Harabasz Index: {calinski_harabasz:.3f}") ``` -------------------------------- ### Split Data into Training and Validation Sets Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/_sources/usage.rst.txt Splits the generated dataset into training and validation sets using train_test_split. The test_size parameter is set to 0.2, meaning 20% of the data will be used for validation. ```python from sklearn.model_selection import train_test_split # Split data into training and validation sets X_train, X_val = train_test_split(X, test_size=0.2, random_state=42) ``` -------------------------------- ### Fuzzy C-Means Model Fitting Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/algorithm.html Fits the Fuzzy C-Means model to the input data X. It initializes cluster centers using K-Means++, then iteratively updates cluster centers and the membership matrix until convergence or max_iter is reached. ```python def fit(self, X, y=None): n_samples, n_features = X.shape self.X_ = X # Step 1: Initialize cluster centers self.cluster_centers_ = self._kmeans_plusplus_init(X) # Step 2: Initialize membership matrix self.U_ = np.random.dirichlet(np.ones(self.n_clusters), size=n_samples) for iteration in range(self.max_iter): U_prev = self.U_.copy() # Step 3: Update cluster centers self.cluster_centers_ = self._compute_centers(X) # Step 4: Update membership matrix self.U_ = self._update_memberships(X) # Step 5: Check for convergence if np.linalg.norm(self.U_ - U_prev) < self.tol: break return self ``` -------------------------------- ### Generate Synthetic Dataset with Overlapping Clusters Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/_sources/usage.rst.txt Generates a synthetic dataset using make_blobs with increased standard deviation to create overlapping clusters. Visualizes the generated data. ```python from sklearn.datasets import make_blobs import matplotlib.pyplot as plt import seaborn as sns import pandas as pd import numpy as np # Generate synthetic dataset with overlapping clusters X, y_true = make_blobs( n_samples=300, centers=3, cluster_std=2.5, # Increased std for overlap random_state=42, ) # Visualize the dataset sns.scatterplot(x=X[:, 0], y=X[:, 1], hue=y_true, palette="deep", legend=None) plt.title("Generated Data with Overlap") plt.xlabel("Feature 1") plt.ylabel("Feature 2") plt.show() ``` -------------------------------- ### Perform Hyperparameter Tuning for Fuzzy C-Means Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/_sources/usage.rst.txt Performs a grid search over specified hyperparameters (n_clusters, m) for the Fuzzy C-Means model. Evaluates clustering performance on the validation set using silhouette, Davies-Bouldin, and Calinski-Harabasz scores. ```python from sklearn.model_selection import ParameterGrid from sklearn.metrics import ( silhouette_score, davies_bouldin_score, calinski_harabasz_score, ) import numpy as np # Define hyperparameter grid param_grid = {"n_clusters": [2, 3, 4], "m": [1.5, 2, 2.5]} results = [] # Perform grid search for params in ParameterGrid(param_grid): fcm = FuzzyCMeans(n_clusters=params["n_clusters"], m=params["m"], random_state=42) fcm.fit(X_train) # Predict membership on validation data U_val = fcm._compute_memberships(X_val) y_pred = U_val.argmax(axis=1) # Evaluate clustering performance silhouette = silhouette_score(X_val, y_pred) davies_bouldin = davies_bouldin_score(X_val, y_pred) calinski_harabasz = calinski_harabasz_score(X_val, y_pred) results.append( { "n_clusters": params["n_clusters"], "m": params["m"], "silhouette_score": silhouette, "davies_bouldin_score": davies_bouldin, "calinski_harabasz_score": calinski_harabasz, } ) ``` -------------------------------- ### Predicting Cluster Labels with Fuzzy C-Means Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/_sources/algorithm.rst.txt Assigns hard cluster labels to input data X by computing membership degrees and selecting the cluster with the highest degree for each data point. Requires the `_compute_memberships` method to be implemented. ```python def predict(self, X): memberships = self._compute_memberships(X) return memberships.argmax(axis=1) ``` -------------------------------- ### Update Membership Matrix Calculation Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/algorithm.html Updates the membership matrix U based on the Euclidean distances between data points and cluster centers. It applies the formula uij = 1 / sum((||xi - cj|| / ||xi - ck||)^(2/(m-1))) for each data point and cluster. ```python def _update_memberships(self, X): distances = cdist(X, self.cluster_centers_, metric="euclidean") distances = np.fmax(distances, 1e-10) inv_distances = 1.0 / distances power = 2 / (self.m - 1) return inv_distances**power / np.sum(inv_distances**power, axis=1, keepdims=True) ``` -------------------------------- ### Update Cluster Centers Calculation Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/algorithm.html Computes the updated cluster centers based on the current membership matrix U and the fuzziness coefficient m. The formula used is cj = sum(uij^m * xi) / sum(uij^m). ```python def _compute_centers(self, X): um = self.U_**self.m return (um.T @ X) / np.sum(um.T, axis=1, keepdims=True) ``` -------------------------------- ### Fuzzy C-Means Algorithm Class Definition Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/algorithm.html Defines the FuzzyCMeans class, inheriting from scikit-learn's BaseEstimator and ClusterMixin. Requires numpy and scipy.spatial.distance.cdist. ```python import numpy as np from sklearn.base import BaseEstimator, ClusterMixin from scipy.spatial.distance import cdist class FuzzyCMeans(BaseEstimator, ClusterMixin): """ Fuzzy C-Means clustering algorithm. """ ``` -------------------------------- ### Predict Cluster Labels Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/algorithm.html Assigns hard cluster labels to new data points based on the maximum membership degree calculated by the _compute_memberships method. This provides a crisp assignment from the fuzzy memberships. ```python def predict(self, X): memberships = self._compute_memberships(X) return memberships.argmax(axis=1) ``` -------------------------------- ### Visualize Fuzzy C-Means Membership Degrees Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/_sources/usage.rst.txt This code snippet visualizes the membership degrees of data points in a Fuzzy C-Means clustering using Plotly. It converts the membership matrix to a long format suitable for interactive scatter plots, where point size represents the membership degree. ```python import plotly.express as px # Convert membership matrix to long format df_membership = pd.DataFrame( U_val_best, columns=[f"Cluster {i+1}" for i in range(fcm_best.n_clusters)] ) df_membership["Max Cluster"] = df_membership.idxmax(axis=1) df_membership["X"] = X_val[:, 0] df_membership["Y"] = X_val[:, 1] # Long format for interactive plot df_membership_long = df_membership.melt( id_vars=["X", "Y", "Max Cluster"], value_vars=[f"Cluster {i+1}" for i in range(fcm_best.n_clusters)], var_name="Cluster", value_name="Membership Degree", ) # Interactive plot fig = px.scatter( df_membership_long, x="X", y="Y", color="Cluster", size="Membership Degree", title="Membership Degrees per Data Point", labels={"Membership Degree": "Degree"}, ) fig.show() ``` -------------------------------- ### Update Membership Matrix Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/_sources/algorithm.rst.txt Updates the membership matrix U based on the current cluster centers and data points. It calculates the inverse distances raised to a power to determine the degree of membership for each data point in each cluster. ```python def _update_memberships(self, X): distances = cdist(X, self.cluster_centers_, metric="euclidean") distances = np.fmax(distances, 1e-10) # Avoid division by zero inv_distances = 1.0 / distances power = 2 / (self.m - 1) return inv_distances**power / np.sum(inv_distances**power, axis=1, keepdims=True) ``` -------------------------------- ### Fuzzy NOT (Complement) Operation Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/fuzzy_logic.html Represents the complement of a fuzzy set, calculated as one minus the membership value. Used for negation in fuzzy logic. ```mathematics μ¬A(x)=1−μA(x) ``` -------------------------------- ### Fuzzy OR (Union) Operation Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/fuzzy_logic.html Represents the union of two fuzzy sets, calculated as the maximum of their membership values. Used in fuzzy rule evaluation. ```mathematics μA∪B(x)=max(μA(x),μB(x)) ``` -------------------------------- ### Triangular Membership Function Formula Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/fuzzy_logic.html Defines a triangular membership function using three parameters: left endpoint, peak, and right endpoint. Used to represent fuzzy sets. ```mathematics μ(x)=max(min(x−ab−a,c−xc−b),0) ``` -------------------------------- ### Trapezoidal Membership Function Formula Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/fuzzy_logic.html Defines a trapezoidal membership function using four parameters: a, b, c, and d. It's a generalized form of the triangular membership function. ```mathematics μ(x)=max(min(x−ab−a,1,d−xd−c),0) ``` -------------------------------- ### Gaussian Membership Function Formula Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/fuzzy_logic.html Defines a Gaussian membership function with a smooth bell shape, characterized by a mean (c) and standard deviation (sigma). ```mathematics μ(x)=exp⁡(−(x−c)22σ2) ``` -------------------------------- ### Fuzzy AND (Intersection) Operation Source: https://scikit-fuzzy-c-means.readthedocs.io/en/latest/fuzzy_logic.html Represents the intersection of two fuzzy sets, calculated as the minimum of their membership values. Used in fuzzy rule evaluation. ```mathematics μA∩B(x)=min(μA(x),μB(x)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.