DEFUZZ
Defuzzification converts a fuzzy membership function into a single boolean or crisp numerical value. It is the final step in a fuzzy logic control system.
This function takes an array of universe variables and their corresponding fuzzy membership values, and applies a selected defuzzification method such as the centroid of area.
Excel Usage
=DEFUZZ(x, mfx, defuzz_mode)
x(list[list], required): Array of independent universe variables.mfx(list[list], required): Array of fuzzy membership values corresponding to x.defuzz_mode(str, optional, default: “centroid”): Defuzzification method to use.
Returns (float): Defuzzified crisp result.
Example 1: Centroid defuzzification of a simple triangle
Inputs:
| x | mfx | defuzz_mode | ||||
|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 0 | 1 | 0 | centroid |
Excel formula:
=DEFUZZ({0,1,2}, {0,1,0}, "centroid")
Expected output:
1
Example 2: Bisector defuzzification of a symmetric triangle
Inputs:
| x | mfx | defuzz_mode | ||||
|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 0 | 1 | 0 | bisector |
Excel formula:
=DEFUZZ({0,1,2}, {0,1,0}, "bisector")
Expected output:
1
Example 3: Mean of maximum on a flat peak
Inputs:
| x | mfx | defuzz_mode | ||||||
|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 0 | 1 | 1 | 0 | mom |
Excel formula:
=DEFUZZ({0,1,2,3}, {0,1,1,0}, "mom")
Expected output:
1.5
Example 4: Largest of maximum on repeated peaks
Inputs:
| x | mfx | defuzz_mode | ||||||
|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 0.2 | 0.7 | 0.7 | 0.1 | lom |
Excel formula:
=DEFUZZ({0,1,2,3}, {0.2,0.7,0.7,0.1}, "lom")
Expected output:
2
Python Code
Show Code
import numpy as np
from skfuzzy import defuzz as fuzz_defuzz
def defuzz(x, mfx, defuzz_mode='centroid'):
"""
Defuzzify a membership function to return a crisp value.
See: https://pythonhosted.org/scikit-fuzzy/api/skfuzzy.html#skfuzzy.defuzz
This example function is provided as-is without any representation of accuracy.
Args:
x (list[list]): Array of independent universe variables.
mfx (list[list]): Array of fuzzy membership values corresponding to x.
defuzz_mode (str, optional): Defuzzification method to use. Valid options: Centroid, Bisector, Mean of Maximum, Min of Maximum, Max of Maximum. Default is 'centroid'.
Returns:
float: Defuzzified crisp result.
"""
try:
def to1d(arr):
if isinstance(arr, list):
flat = []
for row in arr:
row_list = row if isinstance(row, list) else [row]
for val in row_list:
try:
flat.append(float(val))
except (TypeError, ValueError):
continue
return np.array(flat)
return np.array([float(arr)])
x_arr = to1d(x)
mfx_arr = to1d(mfx)
if len(x_arr) == 0 or len(mfx_arr) == 0:
return "Error: Input arrays cannot be empty"
if len(x_arr) != len(mfx_arr):
return "Error: x and mfx must have the same number of elements"
result = fuzz_defuzz(x_arr, mfx_arr, mode=defuzz_mode)
return float(result)
except Exception as e:
return f"Error: {str(e)}"Online Calculator
Array of independent universe variables.
Array of fuzzy membership values corresponding to x.
Defuzzification method to use.