zadeh package

Subpackages

Submodules

zadeh.domains module

class zadeh.domains.CategoricalDomain(name, values)[source]

Bases: zadeh.domains.Domain

centroid(set)[source]
get_ipywidget(**kwargs)[source]

Get a widget representing the domain

get_mesh()[source]

Get a mesh representing the domain

class zadeh.domains.Domain(name)[source]

Bases: object

A domain where diffuse sets are defined

defuzzify(set)[source]

Calculate a crisp number from the fuzzy set

evaluate_set(set)[source]

Get a pair of list with a mesh representing the domain and the evaluation of a membership function on it

get_ipywidget(**kwargs)[source]

Get a widget representing the domain

get_mesh()[source]

Get a mesh representing the domain

plot_set(set, **kwargs)[source]

Plot a fuzzy set

class zadeh.domains.FloatDomain(name, min, max, steps)[source]

Bases: zadeh.domains.Domain

bisector(set)[source]

Defuzzify with the bisector (value separating two portions of equal area under the membership function)

centroid(set)[source]

Defuzzify with the centroid (center of mass)

defuzzify(set)[source]

Calculate a crisp number from the fuzzy set

get_ipywidget(**kwargs)[source]

Get a widget representing the domain

get_mesh()[source]

Get a mesh representing the domain

lom(set)[source]

Defuzzify with the largest of maximum

mom(set)[source]

Defuzzify with the middle of maximum

som(set)[source]

Defuzzify with the smaller of maximum

zadeh.fis module

class zadeh.fis.FIS(variables, rules, target, defuzzification='centroid', aggregation='max', implication='min', AND='min', OR='max')[source]

Bases: object

A fuzzy inference system

batch_predict(X)[source]

Get the crisp output for a batch of inputs

Parameters

X (pd.DataFrame or np.array or list of list) – Input values. If pandas dataframe, must have a column for each of the variables with the same name. If array-like, order must be consistent with the variables.

Returns

An array with the predictions.

Return type

np.array

compile()[source]

Get a compiled version of the model

dict_to_ordered(values)[source]

Transform a dict of inputs into an array in the FIS order

static from_matlab(path)[source]

Import a MATLAB® model from a .fis file

get_1d_interactive(variable, continuous_update=False)[source]

Produce an interactive plot with the output as a function of a variable when the rest are fixed.

Parameters
  • variable (FuzzyVariable) – The independent variable.

  • continuous_update (bool) – Whether to continuously update with the widgets value.

get_2d_interactive(variable1, variable2, continuous_update=False)[source]

Produce an interactive plot with the output as a function of two variables when the rest are fixed.

Parameters
  • variable1 (FuzzyVariable) – The first independent variable.

  • variable2 (FuzzyVariable) – The second independent variable.

  • continuous_update (bool) – Whether to continuously update with the widgets value.

get_crisp_output(values)[source]

Get the output of the system as a crisp value

Parameters

values (dict of str) – A mapping from variables to their fuzzy values.

Returns

Centroid of the output of the system

get_interactive(continuous_update=False)[source]

Display an interactive plot with the fuzzy output of the FIS

Parameters

continuous_update (bool) – Whether to continuously update with the widgets value.

get_output(values)[source]

Get the output of the system as a fuzzy set

Parameters

values (dict of str) – A mapping from variables to their fuzzy values.

Returns

The fuzzy set

Return type

FuzzySet

static load(path)[source]

Load a FIS from the given path

plot_1d(variable, fixed_variables=None, axes=None)[source]

Produce a plot with the output as a function of a variable when the rest are fixed.

Parameters
  • variable (FuzzyVariable) – The independent variable.

  • fixed_variables (dict of str) – A mapping with fuzzy values of the rest of the variables.

  • axes (plt.Axes) – An existing axes instance to plot. If None, a new figure is created.

Returns

Axes for further tweaking

Return type

plt.Axes

plot_2d(variable1, variable2, fixed_variables=None, axes=None)[source]

Produce a plot with the output as a function of two variables when the rest are fixed.

Parameters
  • variable1 (FuzzyVariable) – The first independent variable.

  • variable2 (FuzzyVariable) – The second independent variable.

  • fixed_variables (dict of str) – A mapping with fuzzy values of the rest of the variables.

  • axes (plt.Axes) – An existing axes instance to plot. An 3D projection must have been set on it. If None, a new figure is created.

Returns

Axes for further tweaking

Return type

plt.Axes

plot_rules(values, color='k')[source]

Produce a plot which can be used to explain the behaviour of the system for the given set of values

Parameters
  • values (dict of str) – A mapping from variables to their values.

  • color (str) – Color used to plot rule activation

Returns

2-tuple of (fig, axes) for further tweaking

plot_rules_interactive(continuous_update=False)[source]

Create an interactive explorer for the plot_rules method

Parameters

continuous_update (bool) – Whether to continuously update with the widgets value.

save(path)[source]

Save the FIS definition to a path

zadeh.mparser module

Matlab .fis-like parser

zadeh.mparser.parse_mf(description)[source]

Parse a membership function

Parameters

description (str) – A line defining the membership function.

Returns

A fuzzy set defined by the membership function

Return type

FuzzySet

zadeh.mparser.parse_rule(rule, operation, inputs, output)[source]

Parse a line defining a fuzzy rule

Parameters
  • rule (str) – Line defining the rule

  • operation (str) – “1” for “and”, “2” for “or” (file format meaning).

  • inputs (list of FuzzyVariable) – The ordered list of inputs.

  • output (FuzzyVariable) – The output of the system

Returns

The description of the fuzzy rule

Return type

FuzzyRule

zadeh.mparser.parse_variable(variable, steps=100)[source]

Parse a section defining a fuzzy variable

Parameters
  • variable – Section of the file defining the variable.

  • steps (int) – The number of steps for FloatDomain.

Returns

A representation of the variable

Return type

FuzzyVariable

zadeh.mparser.read_mfis(path, steps=100)[source]

Parse a MATLAB® Fuzzy Inference System-like file

zadeh.rules module

class zadeh.rules.FuzzyAnd(proposition_list)[source]

Bases: zadeh.rules.FuzzyProposition

A fuzzy proposition of the form <p1> and <p2>

class zadeh.rules.FuzzyNot(proposition)[source]

Bases: zadeh.rules.FuzzyProposition

A fuzzy proposition of the form ‘not <p>’

class zadeh.rules.FuzzyNotValuation(variable, value)[source]

Bases: zadeh.rules.FuzzyProposition

An ‘elemental’ fuzzy proposition of the form <variable> is not <value>

While this could be described using the unitary negation operator, the class is provided for convenience

class zadeh.rules.FuzzyOr(proposition_list)[source]

Bases: zadeh.rules.FuzzyProposition

A fuzzy proposition of the form <p1> or <p2>

class zadeh.rules.FuzzyProposition[source]

Bases: object

A fuzzy-logic proposition

class zadeh.rules.FuzzyRule(antecedent, consequent, weight=1.0)[source]

Bases: object

A fuzzy rule of the form ‘if <antecedent> then <consequent>’, possibly with a weight in (0, 1]

class zadeh.rules.FuzzyRuleSet(rule_list)[source]

Bases: object

A set of fuzzy rules

static automatic(antecedent_var, consequent_var, weight=1.0, reverse=False)[source]
class zadeh.rules.FuzzyValuation(variable, value)[source]

Bases: zadeh.rules.FuzzyProposition

An elemental fuzzy proposition of the form ‘<variable> is <value>’

zadeh.rules.prod(xx)[source]

zadeh.server module

zadeh.sets module

class zadeh.sets.BellFuzzySet(a, b, c)[source]

Bases: zadeh.sets.FuzzySet

A fuzzy set defined by a generalized Bell MF

:math:`mu_{a,b,c}(x)= frac{1}{1+left|

rac{x-c}{a}right|^{2b}}`

class zadeh.sets.DiscreteFuzzySet(d)[source]

Bases: zadeh.sets.FuzzySet

A discrete fuzzy set (non-null in a discrete set of points)

class zadeh.sets.FuzzySet(mu=None)[source]

Bases: object

A fuzzy set

class zadeh.sets.FuzzySetAnd(sets, method=None)[source]

Bases: zadeh.sets.FuzzySet

An AND operation between Fuzzy sets

class zadeh.sets.FuzzySetNeg(set)[source]

Bases: zadeh.sets.FuzzySet

A negation operation on a Fuzzy set

class zadeh.sets.FuzzySetOr(sets, method=None)[source]

Bases: zadeh.sets.FuzzySet

An OR operation between Fuzzy sets

class zadeh.sets.FuzzySetScaled(set, scale)[source]

Bases: zadeh.sets.FuzzySet

A scaled Fuzzy sets

class zadeh.sets.Gaussian2FuzzySet(s1, a1, s2, a2)[source]

Bases: zadeh.sets.FuzzySet

A fuzzy set defined by two Gaussian functions

\[\begin{split}G^2_{s_1,a_1,s_2,a_2}(x) = \begin{cases} G_{s_1, a_1}(x), &\quad\text{if } x\leq a_1\\ 1, &\quad\text{if } a_1 \leq x \leq a_2\\ G_{s_2, a_2}(x), &\quad\text{if } x\geq a_2\\ \end{cases}\end{split}\]
Parameters
  • s1 – Width of the first Gaussian.

  • a1 – Start of the membership=1.0 plateau.

  • s2 – Width of the second Gaussian.

  • a2 – End of the membership=1.0 plateau.

class zadeh.sets.GaussianFuzzySet(s, a)[source]

Bases: zadeh.sets.FuzzySet

A fuzzy set defined by a Gaussian function

\[G_{s,a}(x) = \mathrm{e}^{-\frac{{(x - a)}^2}{2 s^2}}\]
Parameters
  • s – Width of the Gaussian.

  • a – Position of the peak of the Gaussian.

class zadeh.sets.PiFuzzySet(a, b, c, d)[source]

Bases: zadeh.sets.FuzzySet

A fuzzy set defined by a Pi-shaped function (a combination of S-shaped MF followed by a Z-shaped MF)

\[Z_{a,b,c,d}(x) = S_{a,b}(x)\cdot Z_{c,d}(x)\]
class zadeh.sets.SFuzzySet(a, b)[source]

Bases: zadeh.sets.FuzzySet

A fuzzy set defined by an S-shaped function.

\[\begin{split}S_{a,b}(x) = \begin{cases} 0, &\quad\text{if } x\leq a\\ 2\left(\frac{x-a}{b-a}\right)^2, &\quad\text{if } a \leq x\leq \frac{a+b}{2}\\ 1-2\left(\frac{x-b}{b-a}\right)^2, &\quad\text{if } \frac{a+b}{2} \leq x \leq b\\ 1, &\quad\text{if } x\geq b\\ \end{cases}\end{split}\]
class zadeh.sets.SigmoidalDifferenceFuzzySet(a1, c1, a2, c2)[source]

Bases: zadeh.sets.FuzzySet

A fuzzy set defined by the difference of two sigmoid functions clipped to [0, 1]

\(\sigma_{a_1,c_1,a_2,c_2}^\mathrm{d}(x)=\mathrm{clip}_{0,1}(\sigma_{a_1,c_1}(x) - \sigma_{a_2,c_2}(x))\)

Typical unimodal membership functions are defined setting the same sign for (a1, a2), and choosing (c1, c2) enough apart for both sigmoids reach ~1 in a common subset.

class zadeh.sets.SigmoidalFuzzySet(a, c)[source]

Bases: zadeh.sets.FuzzySet

A fuzzy set defined by a sigmoid function

\(\sigma_{a,c}(x)= \frac{1}{1+e^{\left(-a (x-c)\right)}}\)

If a>0, the sigmoid is increasing, otherwise decreasing. The magnitude of “a” defines the width of the transition, “c” defines its location.

class zadeh.sets.SigmoidalProductFuzzySet(a1, c1, a2, c2)[source]

Bases: zadeh.sets.FuzzySet

A fuzzy set defined by the product of two sigmoid functions

\(\sigma_{a_1,c_1,a_2,c_2}^\mathrm{p}(x)=\sigma_{a_1,c_1}(x)\cdot \sigma_{a_2,c_2}(x)\)

Typical unimodal membership functions are defined setting the opposite sign for (a1, a2), and choosing (c1, c2) enough apart for both sigmoids reach ~1 in a common subset.

class zadeh.sets.SingletonSet(x)[source]

Bases: zadeh.sets.FuzzySet

A singleton fuzzy set (Kronecker delta)

class zadeh.sets.TrapezoidalFuzzySet(a, b, c, d)[source]

Bases: zadeh.sets.FuzzySet

A fuzzy set defined by a trapezoidal function

class zadeh.sets.TriangularFuzzySet(a, b, c)[source]

Bases: zadeh.sets.FuzzySet

A fuzzy set defined by a triangular function

class zadeh.sets.ZFuzzySet(a, b)[source]

Bases: zadeh.sets.FuzzySet

A fuzzy set defined by a Z-shaped function

\[\begin{split}Z_{a,b}(x) = \begin{cases} 0, &\quad\text{if } x\leq a\\ 1-2\left(\frac{x-a}{b-a}\right)^2, &\quad\text{if } a \leq x\leq \frac{a+b}{2}\\ 2\left(\frac{x-b}{b-a}\right)^2, &\quad\text{if } \frac{a+b}{2} \leq x \leq b\\ 1, &\quad\text{if } x\geq b\\ \end{cases}\end{split}\]
zadeh.sets.prod(xx)[source]

zadeh.variables module

class zadeh.variables.FuzzyVariable(domain, values, name=None)[source]

Bases: object

A fuzzy variable

static automatic(name, min, max, steps, values, endpoints=True, value_names=None, width_factor=1.0, shape='gaussian')[source]

Automatically create a fuzzy variable with the number of values provided.

Parameters
  • name (str) – Name of the variable and of its domain.

  • min (float) – Minimum value of the domain

  • max (float) – Maximum value of the domain

  • steps (int or float) – Number of steps if int or step size if float.

  • values (int) – Number of values.

  • endpoints (bool) – Whether the start and ending points are considered.

  • value_names (list of str) – Names of values. If not provided or if length does not match, they will be automatically guessed when possible.

  • width_factor (float) – A scale factor for the width of the membership function defining the values.

  • shape (str) – Kind of sets used to define the values. Available options are: - gaussian: Gaussian sets. - triangular: Triangular sets. - trapezoidal: Trapezoidal sets. - spline: order-2 spline-based sets (S, Pi, Z). - sigmoidald: Sigmoidal functions, using the sigmoidal difference for non-endpoints. - sigmoidalp: Sigmoidal functions, using the sigmoidal product for non-endpoints.

Returns

The automatically generated fuzzy variable.

Return type

FuzzyVariable

plot(value=None)[source]

Plot the membership function for each of the values

Parameters

value (str) – A value to highlight. If so, the other values are shown dimmed and not in the legend

Module contents

zadeh - Python package to build fuzzy inference systems