scikit-fuzzy
scikit-fuzzy is a fuzzy logic toolkit for SciPy, providing a robust collection of independently developed and implemented fuzzy logic algorithms. It aims to offer a Pythonic alternative to closed-source fuzzy logic software. The library is currently at version 0.5.0 and receives periodic updates, indicating active maintenance and development.
Common errors
-
AttributeError: 'module' object has no attribute 'Antecedent'
cause The `Antecedent` class, along with other control system components, is located in the `skfuzzy.control` submodule, not directly in the `skfuzzy` top-level package.fixChange your import statement to `from skfuzzy import control as ctrl` and then use `ctrl.Antecedent(...)`. -
ValueError: Unexpected input: <variable_name>
cause This error typically occurs when initializing `Antecedent` or `Consequent` objects within `skfuzzy.control`. It signifies a mismatch in the variable name provided as input to `ControlSystemSimulation.input` or incorrect definition of the control system variables.fixDouble-check that the string names passed to `ctrl.Antecedent('name', ...)` and `ctrl.Consequent('name', ...)` exactly match the keys used when setting input values to a `ControlSystemSimulation` instance (e.g., `sim.input['name'] = value`). Also verify the type and range of the input provided to `Antecedent` or `Consequent` constructor matches expected `np.arange` or similar. -
AssertionError: fuzzy system python assertion error during input
cause While some related reports mention `fuzzywuzzy`, similar assertion errors in `scikit-fuzzy` often arise from incorrect definition of universe variables, membership functions, or fuzzy rules, leading to invalid input during computation or inference. This can include non-overlapping membership functions or improper ranges.fixReview the definitions of your universe variables (`np.arange`), membership functions (`fuzz.trimf`, `fuzz.gaussmf`, etc.), and fuzzy rules. Ensure that membership functions cover the expected range of inputs and that rules are logically consistent. Pay close attention to the input values provided to the `compute()` method of a `ControlSystemSimulation`.
Warnings
- breaking scikit-fuzzy adheres to the IEEE standard for rounding, which can lead to slight numerical differences compared to MATLAB's rounding behavior (e.g., 2.5 rounds to 2, 3.5 rounds to 4). If re-implementing algorithms from MATLAB, expect minor inconsistencies due to this.
- breaking The fuzzy control system API (within `skfuzzy.control`) underwent significant changes and improvements in earlier major releases (e.g., 0.3.0 introduced a new API). Older implementations of fuzzy control systems might require updates to use the new `Antecedent`, `Consequent`, `ControlSystem`, and `ControlSystemSimulation` classes.
- gotcha Classes and functions related to fuzzy control systems (e.g., `Antecedent`, `Consequent`, `ControlSystem`) are *not* directly available in the main `skfuzzy` namespace. They must be explicitly imported from the `skfuzzy.control` submodule.
- gotcha There have been reports of compatibility issues with Python 3.12, often surfacing through underlying dependency conflicts. While `scikit-fuzzy` aims for broad Python support, specific environment configurations with Python 3.12 might encounter problems.
Install
-
pip install -U scikit-fuzzy -
conda install -c conda-forge scikit-fuzzy
Imports
- skfuzzy
import skfuzzy
import skfuzzy as fuzz
- control
import skfuzzy.control
from skfuzzy import control as ctrl
- numpy
import numpy as np
- pyplot
from matplotlib import pyplot as plt
Quickstart
import numpy as np
import skfuzzy as fuzz
import matplotlib.pyplot as plt
# Generate universe variables
x = np.arange(0, 11, 1) # Points from 0 to 10
# Generate fuzzy membership functions
low = fuzz.trimf(x, [0, 0, 5])
medium = fuzz.trimf(x, [0, 5, 10])
high = fuzz.trimf(x, [5, 10, 10])
# Visualize these universes and membership functions
plt.figure()
plt.plot(x, low, 'b', linewidth=1.5, label='Low')
plt.plot(x, medium, 'g', linewidth=1.5, label='Medium')
plt.plot(x, high, 'r', linewidth=1.5, label='High')
plt.title('Fuzzy Membership Functions')
plt.ylabel('Membership value')
plt.xlabel('Universe Variable')
plt.legend()
plt.grid(True)
plt.show()