Overview
TheuseCMeans hook provides a complete state management solution for both Fuzzy C-Means and Crisp C-Means clustering algorithms. It handles points, centroids, distance matrices, membership matrices, and iteration logic.
Function Signature
Parameters
The initial algorithm type to use for clustering. Can be either
'fuzzy' for Fuzzy C-Means or 'crisp' for Crisp C-Means.Type Definition:Return Values
The hook returns an object containing state values, setter functions, and operation methods:State Values
The current algorithm type being used (
'fuzzy' or 'crisp').Array of centroid points for the clustering algorithm.Point Type:
Array of data points to be clustered.
Matrix containing the distances between each point and each centroid.
Matrix representing the membership degree of each point to each cluster. For fuzzy clustering, values range from 0 to 1. For crisp clustering, values are binary (0 or 1).
The calculated new centroid positions after applying the algorithm. These will become the new centroids when
onIterate() is called.Array of cost values for each cluster, representing the sum of distances or membership-weighted distances.
The total cost function value for the current clustering configuration. Lower values indicate better clustering.
Setter Functions
Updates the algorithm type between
'fuzzy' and 'crisp'.Directly sets the centroid array.
Directly sets the points array.
Operation Methods
Adds a new data point to the clustering dataset.Parameters:
point: Object withxandycoordinates
Adds a new centroid to the clustering algorithm.Parameters:
centroid: Object withxandycoordinates
Executes one iteration of the clustering algorithm by updating centroids to their newly calculated positions. Shows an error notification if there are insufficient points or centroids.
Resets the clustering state by clearing all points and centroids.
Usage Example
Type Definitions
Implementation Details
The hook automatically switches between Fuzzy C-Means and Crisp C-Means implementations based on the selected algorithm type. The membership matrix interpretation differs between algorithms:
- Fuzzy: Values represent degrees of membership (0-1)
- Crisp: Values are binary cluster assignments (0 or 1)
Error Handling
TheonIterate() function includes validation to ensure sufficient data exists before performing an iteration. If there are insufficient points or centroids, an error notification is displayed using Ant Design’s notification system.
Source
Defined insrc/utils/useCmeans.ts:13