Module: fuzzymath

Fuzzy mathematics subpackage, containing essential mathematical operations for fuzzy sets and universe variables.

skfuzzy.fuzzymath.cartadd(x, y) Cartesian addition of fuzzy membership vectors using the algebraic method.
skfuzzy.fuzzymath.cartprod(x, y) Cartesian product of two fuzzy membership vectors.
skfuzzy.fuzzymath.classic_relation(a, b) Determine the classic relation matrix, R, between two fuzzy sets.
skfuzzy.fuzzymath.continuous_to_discrete(a, ...) Converts a continuous-time system to its equivalent discrete-time version.
skfuzzy.fuzzymath.contrast(arr[, amount, ...]) General contrast booster or diffuser of normalized array-like data.
skfuzzy.fuzzymath.fuzzy_add(x, a, y, b) Add fuzzy set a to fuzzy set b.
skfuzzy.fuzzymath.fuzzy_and(x, mfx, y, mfy) Fuzzy AND operator, a.k.a.
skfuzzy.fuzzymath.fuzzy_compare(q) Determine the comparison matrix, c, based on the fuzzy pairwise comparison matrix, q, using Shimura’s special relativity formula.
skfuzzy.fuzzymath.fuzzy_div(x, a, y, b) Divide fuzzy set b into fuzzy set a.
skfuzzy.fuzzymath.fuzzy_min(x, a, y, b) Find minimum between fuzzy set a fuzzy set b.
skfuzzy.fuzzymath.fuzzy_mult(x, a, y, b) Multiplies fuzzy set a and fuzzy set b.
skfuzzy.fuzzymath.fuzzy_not(mfx) Fuzzy NOT operator, a.k.a.
skfuzzy.fuzzymath.fuzzy_or(x, mfx, y, mfy) Fuzzy OR operator, a.k.a.
skfuzzy.fuzzymath.fuzzy_similarity(ai, b[, mode]) The fuzzy similarity between set ai and observation set b.
skfuzzy.fuzzymath.fuzzy_sub(x, a, y, b) Subtract fuzzy set b from fuzzy set a.
skfuzzy.fuzzymath.inner_product(a, b) Inner product (dot product) of two fuzzy sets.
skfuzzy.fuzzymath.interp10(x) Utility function which conducts linear interpolation of any rank-1 array.
skfuzzy.fuzzymath.interp_membership(x, xmf, xx) Find the degree of membership u(xx) for a given value of x = xx.
skfuzzy.fuzzymath.interp_universe(x, xmf, y) Find interpolated universe value(s) for a given fuzzy membership value.
skfuzzy.fuzzymath.maxmin_composition(s, r) The max-min composition t of two fuzzy relation matrices.
skfuzzy.fuzzymath.maxprod_composition(s, r) The max-product composition t of two fuzzy relation matrices.
skfuzzy.fuzzymath.modus_ponens(a, b, ap[, c]) Generalized modus ponens deduction to make approximate reasoning in a rules-base system.
skfuzzy.fuzzymath.outer_product(a, b) Outer product of two fuzzy sets.
skfuzzy.fuzzymath.partial_dmf(x, mf_name, ...) Calculate the partial derivative of a specified membership function.
skfuzzy.fuzzymath.relation_min(a, b) Determine fuzzy relation matrix R using Mamdani implication for the fuzzy antecedent a and consequent b inputs.
skfuzzy.fuzzymath.relation_product(a, b) Determine the fuzzy relation matrix, R, using product implication for the fuzzy antecedent a and the fuzzy consequent b.
skfuzzy.fuzzymath.sigmoid(x, power[, split]) Intensify grayscale values in an array using a sigmoid function.

cartadd

skfuzzy.fuzzymath.cartadd(x, y)[source]

Cartesian addition of fuzzy membership vectors using the algebraic method.

Parameters:

x : 1D array or iterable

First fuzzy membership vector, of length M.

y : 1D array or iterable

Second fuzzy membership vector, of length N.

Returns:

z : 2D array

Cartesian addition of x and y, of shape (M, N).

cartprod

skfuzzy.fuzzymath.cartprod(x, y)[source]

Cartesian product of two fuzzy membership vectors. Uses min().

Parameters:

x : 1D array or iterable

First fuzzy membership vector, of length M.

y : 1D array or iterable

Second fuzzy membership vector, of length N.

Returns:

z : 2D array

Cartesian product of x and y, of shape (M, N).

classic_relation

skfuzzy.fuzzymath.classic_relation(a, b)[source]

Determine the classic relation matrix, R, between two fuzzy sets.

Parameters:

a : 1D array or iterable

First fuzzy membership vector, of length M.

b : 1D array or iterable

Second fuzzy membership vector, of length N.

Returns:

R : 2D array

Classic relation matrix between a and b, shape (M, N)

Notes

The classic relation is defined as:

r = [a x b] U [(1 - a) x ones(1, N)],

where x represents a cartesian product and N is len(b).

continuous_to_discrete

skfuzzy.fuzzymath.continuous_to_discrete(a, b, sampling_rate)[source]

Converts a continuous-time system to its equivalent discrete-time version.

Parameters:

a : (N, N) array of floats

State variable coefficients describing the continuous-time system.

b : (N,) or (N, 1) array of floats

Constant coefficients describing the continuous-time system. Can be either a rank-1 array or a rank-2 array of shape (N, 1).

sampling_rate : float

Rate in Hz at which the continuous-time system is to be sampled.

Returns:

phi : (N, N) array of floats

Variable coefficients describing the discrete-time system.

gamma : (N,) or (N, 1) array of floats

Constant coefficients describing the discrete-time system. Shape of this output maintains the shape passed as b.

contrast

skfuzzy.fuzzymath.contrast(arr, amount=0.2, split=0.5, normalize=True)[source]

General contrast booster or diffuser of normalized array-like data.

Parameters:

arr : ndarray

Input array (of floats on range [0, 1] if normalize=False). If values exist outside this range, with normalize=True the image will be normalized for calculation.

amount : float or length-2 iterable of floats

Controls the exponential contrast mechanism for values above and below split in I. If positive, the curve provides added contrast; if negative, the curve provides reduced contrast.

If provided as a lenth-2 iterable of floats, they control the regions (below, above) split separately.

split : float

Positive scalar, on range [0, 1], determining the midpoint of the exponential contrast. Default of 0.5 is reasonable for well-exposed images.

normalize : bool, default True

Controls normalization to the range [0, 1].

Returns:

focused : ndarray

Contrast adjusted, normalized, floating-point image on range [0, 1].

Notes

The result of this algorithm is like applying a Curves adjustment in the GIMP or Photoshop.

Algorithm for curves adjustment at a given pixel, x, is given by:

       | split * (x/split)^below,                        0 <= x <= split
y(x) = |
       | 1 - (1-split) * ((1-x) / (1-split))^above,   split < x <= 1.0

fuzzy_add

skfuzzy.fuzzymath.fuzzy_add(x, a, y, b)[source]

Add fuzzy set a to fuzzy set b.

Parameters:

x : 1d array, length N

Universe variable for fuzzy set a.

a : 1d array, length N

Fuzzy set for universe x.

y : 1d array, length M

Universe variable for fuzzy set b.

b : 1d array, length M

Fuzzy set for universe y.

Returns:

z : 1d array

Output variable.

mfz : 1d array

Fuzzy membership set for variable z.

Notes

Uses Zadeh’s Extension Principle as described in Ross, Fuzzy Logic with Engineering Applications (2010), pp. 414, Eq. 12.17.

If these results are unexpected and your membership functions are convex, consider trying the skfuzzy.dsw_* functions for fuzzy mathematics using interval arithmetic via the restricted Dong, Shah, and Wong method.

fuzzy_and

skfuzzy.fuzzymath.fuzzy_and(x, mfx, y, mfy)[source]

Fuzzy AND operator, a.k.a. the intersection of two fuzzy sets.

Parameters:

x : 1d array

Universe variable for fuzzy membership function mfx.

mfx : 1d array

Fuzzy membership function for universe variable x.

y : 1d array

Universe variable for fuzzy membership function mfy.

mfy : 1d array

Fuzzy membership function for universe variable y.

Returns:

z : 1d array

Universe variable for union of the two provided fuzzy sets.

mfz : 1d array

Fuzzy AND (intersection) of mfx and mfy.

fuzzy_compare

skfuzzy.fuzzymath.fuzzy_compare(q)[source]

Determine the comparison matrix, c, based on the fuzzy pairwise comparison matrix, q, using Shimura’s special relativity formula.

Parameters:

q : 2d array, (N, N)

Fuzzy pairwise comparison matrix.

Returns:

c : 2d array, (N, N)

Comparison matrix.

fuzzy_div

skfuzzy.fuzzymath.fuzzy_div(x, a, y, b)[source]

Divide fuzzy set b into fuzzy set a.

Parameters:

x : 1d array, length N

Universe variable for fuzzy set a.

a : 1d array, length N

Fuzzy set for universe x.

y : 1d array, length M

Universe variable for fuzzy set b.

b : 1d array, length M

Fuzzy set for universe y.

Returns:

z : 1d array

Output variable.

mfz : 1d array

Fuzzy membership set for variable z.

Notes

Uses Zadeh’s Extension Principle from Ross, Fuzzy Logic w/Engineering Applications, (2010), pp.414, Eq. 12.17.

If these results are unexpected and your membership functions are convex, consider trying the skfuzzy.dsw_* functions for fuzzy mathematics using interval arithmetic via the restricted Dong, Shah, and Wong method.

fuzzy_min

skfuzzy.fuzzymath.fuzzy_min(x, a, y, b)[source]

Find minimum between fuzzy set a fuzzy set b.

Parameters:

x : 1d array, length N

Universe variable for fuzzy set a.

a : 1d array, length N

Fuzzy set for universe x.

y : 1d array, length M

Universe variable for fuzzy set b.

b : 1d array, length M

Fuzzy set for universe y.

Returns:

z : 1d array

Output variable.

mfz : 1d array

Fuzzy membership set for variable z.

Notes

Uses Zadeh’s Extension Principle from Ross, Fuzzy Logic w/Engineering Applications, (2010), pp.414, Eq. 12.17.

If these results are unexpected and your membership functions are convex, consider trying the skfuzzy.dsw_* functions for fuzzy mathematics using interval arithmetic via the restricted Dong, Shah, and Wong method.

fuzzy_mult

skfuzzy.fuzzymath.fuzzy_mult(x, a, y, b)[source]

Multiplies fuzzy set a and fuzzy set b.

Parameters:

x : 1d array, length N

Universe variable for fuzzy set a.

A : 1d array, length N

Fuzzy set for universe x.

y : 1d array, length M

Universe variable for fuzzy set b.

b : 1d array, length M

Fuzzy set for universe y.

Returns:

z : 1d array

Output variable.

mfz : 1d array

Fuzzy membership set for variable z.

Notes

Uses Zadeh’s Extension Principle from Ross, Fuzzy Logic w/Engineering Applications, (2010), pp.414, Eq. 12.17.

If these results are unexpected and your membership functions are convex, consider trying the skfuzzy.dsw_* functions for fuzzy mathematics using interval arithmetic via the restricted Dong, Shah, and Wong method.

fuzzy_not

skfuzzy.fuzzymath.fuzzy_not(mfx)[source]

Fuzzy NOT operator, a.k.a. complement of a fuzzy set.

Parameters:

mfx : 1d array

Fuzzy membership function.

Returns:

mfz : 1d array

Fuzzy NOT (complement) of mfx.

Notes

This operation does not require a universe variable, because the complement is defined for a single set. The output remains defined on the same universe.

fuzzy_or

skfuzzy.fuzzymath.fuzzy_or(x, mfx, y, mfy)[source]

Fuzzy OR operator, a.k.a. union of two fuzzy sets.

Parameters:

x : 1d array

Universe variable for fuzzy membership function mfx.

mfx : 1d array

Fuzzy membership function for universe variable x.

y : 1d array

Universe variable for fuzzy membership function mfy.

mfy : 1d array

Fuzzy membership function for universe variable y.

Returns:

z : 1d array

Universe variable for intersection of the two provided fuzzy sets.

mfz : 1d array

Fuzzy OR (union) of mfx and mfy.

fuzzy_similarity

skfuzzy.fuzzymath.fuzzy_similarity(ai, b, mode='min')[source]

The fuzzy similarity between set ai and observation set b.

Parameters:

ai : 1d array

Fuzzy membership function of set ai.

b : 1d array

Fuzzy membership function of set b.

mode : string

Controls the method of similarity calculation. * 'min' : Computed by array minimum operation. * 'avg' : Computed by taking the array average.

Returns:

s : float

Fuzzy similarity.

fuzzy_sub

skfuzzy.fuzzymath.fuzzy_sub(x, a, y, b)[source]

Subtract fuzzy set b from fuzzy set a.

Parameters:

x : 1d array, length N

Universe variable for fuzzy set a.

A : 1d array, length N

Fuzzy set for universe x.

y : 1d array, length M

Universe variable for fuzzy set b.

b : 1d array, length M

Fuzzy set for universe y.

Returns:

z : 1d array

Output variable.

mfz : 1d array

Fuzzy membership set for variable z.

Notes

Uses Zadeh’s Extension Principle from Ross, Fuzzy Logic w/Engineering Applications, (2010), pp.414, Eq. 12.17.

If these results are unexpected and your membership functions are convex, consider trying the skfuzzy.dsw_* functions for fuzzy mathematics using interval arithmetic via the restricted Dong, Shah, and Wong method.

inner_product

skfuzzy.fuzzymath.inner_product(a, b)[source]

Inner product (dot product) of two fuzzy sets.

Parameters:

a : 1d array or iterable

Fuzzy membership function.

b : 1d array or iterable

Fuzzy membership function.

Returns:

y : float

Fuzzy inner product value, on range [0, 1]

interp10

skfuzzy.fuzzymath.interp10(x)[source]

Utility function which conducts linear interpolation of any rank-1 array. Result will have 10x resolution.

Parameters:

x : 1d array, length N

Input array to be interpolated.

Returns:

y : 1d array, length 10 * N + 1

Linearly interpolated output.

interp_membership

skfuzzy.fuzzymath.interp_membership(x, xmf, xx)[source]

Find the degree of membership u(xx) for a given value of x = xx.

Parameters:

x : 1d array

Independent discrete variable vector.

xmf : 1d array

Fuzzy membership function for x. Same length as x.

xx : float

Discrete singleton value on universe x.

Returns:

xxmf : float

Membership function value at xx, u(xx).

Notes

For use in Fuzzy Logic, where an interpolated discrete membership function u(x) for discrete values of x on the universe of x is given. Then, consider a new value x = xx, which does not correspond to any discrete values of x. This function computes the membership value u(xx) corresponding to the value xx using linear interpolation.

interp_universe

skfuzzy.fuzzymath.interp_universe(x, xmf, y)[source]

Find interpolated universe value(s) for a given fuzzy membership value.

Parameters:

x : 1d array

Independent discrete variable vector.

xmf : 1d array

Fuzzy membership function for x. Same length as x.

y : float

Specific fuzzy membership value.

Returns:

xx : list

List of discrete singleton values on universe x whose membership function value is y, u(xx[i])==y. If there are not points xx[i] such that u(xx[i])==y it returns an empty list.

Notes

For use in Fuzzy Logic, where a membership function level y is given. Consider there is some value (or set of values) xx for which u(xx) == y is true, though xx may not correspond to any discrete values on x. This function computes the value (or values) of xx such that u(xx) == y using linear interpolation.

maxmin_composition

skfuzzy.fuzzymath.maxmin_composition(s, r)[source]

The max-min composition t of two fuzzy relation matrices.

Parameters:

s : 2d array, (M, N)

Fuzzy relation matrix #1.

r : 2d array, (N, P)

Fuzzy relation matrix #2.

Returns:

T ; 2d array, (M, P) :

Max-min composition, defined by T = s o r.

maxprod_composition

skfuzzy.fuzzymath.maxprod_composition(s, r)[source]

The max-product composition t of two fuzzy relation matrices.

Parameters:

s : 2d array, (M, N)

Fuzzy relation matrix #1.

r : 2d array, (N, P)

Fuzzy relation matrix #2.

Returns:

t : 2d array, (M, P)

Max-product composition matrix.

modus_ponens

skfuzzy.fuzzymath.modus_ponens(a, b, ap, c=None)[source]

Generalized modus ponens deduction to make approximate reasoning in a rules-base system.

Parameters:

a : 1d array

Fuzzy set a on universe x

b : 1d array

Fuzzy set b on universe y

ap : 1d array

New fuzzy fact a’ (a prime, not transpose)

c : 1d array, OPTIONAL

Keyword argument representing fuzzy set c on universe y. Default = None, which will use np.ones() instead.

Returns:

R : 2d array

Full fuzzy relation.

bp : 1d array

Fuzzy conclusion b’ (b prime)

outer_product

skfuzzy.fuzzymath.outer_product(a, b)[source]

Outer product of two fuzzy sets.

Parameters:

a : 1d array or iterable

Fuzzy membership function.

b : 1d array or iterable

Fuzzy membership function.

Returns:

y : float

Fuzzy outer product value, on range [0, 1]

partial_dmf

skfuzzy.fuzzymath.partial_dmf(x, mf_name, mf_parameter_dict, partial_parameter)[source]

Calculate the partial derivative of a specified membership function.

Parameters:

x : float

input variable.

mf_name : string

Membership function name as a string. The following are supported: * 'gaussmf' : parameters 'sigma' or 'mean' * 'gbellmf' : parameters 'a', 'b', or 'c' * 'sigmf' : parameters 'b' or 'c'

mf_parameter_dict : dict

A dictionary of {param : key-value, ...} pairs for a particular membership function as defined above.

partial_parameter : string

Name of the parameter against which we take the partial derivative.

Returns:

d : float

Partial derivative of the membership function with respect to the chosen parameter, at input point x.

Notes

Partial derivatives of fuzzy membership functions are only meaningful for continuous functions. Triangular, trapezoidal designs have no partial derivatives to calculate. The following

relation_min

skfuzzy.fuzzymath.relation_min(a, b)[source]

Determine fuzzy relation matrix R using Mamdani implication for the fuzzy antecedent a and consequent b inputs.

Parameters:

a : 1d array

Fuzzy antecedent variable of length M.

b : 1d array

Fuzzy consequent variable of length N.

Returns:

R : 2d array

Fuzzy relation between a and b, of shape (M, N).

relation_product

skfuzzy.fuzzymath.relation_product(a, b)[source]

Determine the fuzzy relation matrix, R, using product implication for the fuzzy antecedent a and the fuzzy consequent b.

Parameters:

a : 1d array

Fuzzy antecedent variable of length M.

b : 1d array

Fuzzy consequent variable of length N.

Returns:

R : 2d array

Fuzzy relation between a and b, of shape (M, N).

sigmoid

skfuzzy.fuzzymath.sigmoid(x, power, split=0.5)[source]

Intensify grayscale values in an array using a sigmoid function.

Parameters:

x : ndarray

Input vector or image array. Should be pre-normalized to range [0, 1]

p : float

Power of the intensification (p > 0). Experiment with small, decimal values and increase as necessary.

split : float

Threshold for intensification. Values above split will be intensified, while values below split will be deintensified. Note range for split is (0, 1). Default of 0.5 is reasonable for many well-exposed images.

Returns:

y : ndarray, same size as x

Output vector or image with contrast adjusted.

Notes

The sigmoid used herein is defined as:

y = 1 / (1 + exp(- exp(- power * (x-split))))