CMEANS_PREDICT
Prediction of new data in a given trained fuzzy c-means framework. This algorithm repeats the clustering with fixed cluster centers efficiently finding the fuzzy membership of all data points.
It computes the membership u_{ij} of a new data point x_i to an existing cluster center c_j using the formula:
u_{ij} = \frac{1}{\sum_{k=1}^{C} \left(\frac{||x_i - c_j||}{||x_i - c_k||}\right)^{\frac{2}{m-1}}}
where C is the total number of clusters, and m is the fuzziness parameter.
Excel Usage
=CMEANS_PREDICT(test_data, cntr_trained, m, error, maxiter)
test_data(list[list], required): 2D array of new data to predict (features x samples S x N).cntr_trained(list[list], required): Location of trained centers from prior training c-means.m(float, required): Array exponentiation applied to the membership function.error(float, required): Stopping criterion.maxiter(int, required): Maximum number of iterations allowed.
Returns (dict): Dictionary of prediction results, including the fuzzy partition matrix.
Example 1: Fuzzy c-means prediction on new data
Inputs:
| test_data | cntr_trained | m | error | maxiter | ||
|---|---|---|---|---|---|---|
| 1.5 | 8.5 | 1.5 | 1.5 | 2 | 0.005 | 2 |
| 1.5 | 8.5 | 8.5 | 8.5 |
Excel formula:
=CMEANS_PREDICT({1.5,8.5;1.5,8.5}, {1.5,1.5;8.5,8.5}, 2, 0.005, 2)
Expected output:
{"type":"Double","basicValue":1,"properties":{"fpc":{"type":"Double","basicValue":1},"u":{"type":"Array","elements":[[{"type":"Double","basicValue":1},{"type":"Double","basicValue":4.93038e-32}],[{"type":"Double","basicValue":4.93038e-32},{"type":"Double","basicValue":1}]]},"d":{"type":"Array","elements":[[{"type":"Double","basicValue":2.22045e-16},{"type":"Double","basicValue":9.89949}],[{"type":"Double","basicValue":9.89949},{"type":"Double","basicValue":2.22045e-16}]]},"jm":{"type":"Array","elements":[[{"type":"Double","basicValue":56.0623}],[{"type":"Double","basicValue":9.76215e-30}]]},"p":{"type":"Double","basicValue":2}}}
Python Code
import numpy as np
from skfuzzy import cmeans_predict as fuzz_cmeans_predict
def cmeans_predict(test_data, cntr_trained, m, error, maxiter):
"""
Predict cluster membership for new data given a trained fuzzy c-means model.
See: https://pythonhosted.org/scikit-fuzzy/api/skfuzzy.html#skfuzzy.cmeans_predict
This example function is provided as-is without any representation of accuracy.
Args:
test_data (list[list]): 2D array of new data to predict (features x samples S x N).
cntr_trained (list[list]): Location of trained centers from prior training c-means.
m (float): Array exponentiation applied to the membership function.
error (float): Stopping criterion.
maxiter (int): Maximum number of iterations allowed.
Returns:
dict: Dictionary of prediction results, including the fuzzy partition matrix.
"""
try:
test_data_np = np.array(test_data, dtype=float)
if test_data_np.ndim != 2:
return "Error: test_data must be a 2D array"
cntr_trained_np = np.array(cntr_trained, dtype=float)
u, u0, d, jm, p, fpc = fuzz_cmeans_predict(
test_data=test_data_np,
cntr_trained=cntr_trained_np,
m=m,
error=error,
maxiter=maxiter
)
return {
"type": "Double",
"basicValue": float(fpc),
"properties": {
"fpc": {"type": "Double", "basicValue": float(fpc)},
"u": {
"type": "Array",
"elements": [[{"type": "Double", "basicValue": float(val)} for val in row] for row in u]
},
"d": {
"type": "Array",
"elements": [[{"type": "Double", "basicValue": float(val)} for val in row] for row in d]
},
"jm": {
"type": "Array",
"elements": [[{"type": "Double", "basicValue": float(val)}] for val in jm]
},
"p": {"type": "Double", "basicValue": float(p)}
}
}
except Exception as e:
return f"Error: {str(e)}"