Documentation

FuzzySystem fis

A python framework to build Fuzzy Inference Systems

Raul Navarro-Almanza<rnavarro@uabc.edu.mx>

class FuzzySystem.fis.FuzzyInferenceSystem(rules, points=350, inputs=None, outputs=None, and_op='min', or_op='max', verbose=False)[source]

Class to perform the inference process using fuzzy logic

Parameters
  • rules – List of rules to conform the knowledge base

  • points – Number of points to perform the evaluation

  • inputs – dictionary of inputs

  • outputs – dictionary of outputs

  • and_op – Conjunction operator

  • or_op – Union operator

  • verbose – flag to print information

add_rule(rule)[source]

Add a rule to the existing knowledge base

Parameters

rule (FuzzyRule) – The rule to append to the system

eval(inputs, verbose=None)[source]

Evaluates the fuzzy inference system

Parameters
  • inputs (number, list, pandas data frame) – Inputs to evaluate.

  • verbose – flog to print information

Returns

Output object that contains the fuzzy set of the evaluated consequent

get_matrix_rules(negatives=True)[source]

Generates the matrix with the associations in the rules

Parameters

negatives – If is True, the negated antecedents are multiplied by -1

Returns

Matrix with the rule associations.

get_structure()[source]

Create a json representation of the FIS structure

Returns

Dictionary with all the elements in the FIS

property inputs

Dictionary with the system’s inputs

property matrix_rules

Matrix with the relationships between antecedents and consequent

property outputs

Dictionary with the system’s outputs

show_rules()[source]

Shows in natural language the knowledge base

FuzzySystem membership_function

class FuzzySystem.membership_function.Cauchymf(params, universe=None, name=None, complement=False)[source]

Class to define a Caouchy Membership Function

Parameters
  • params – List of design values of the membership function

  • universe – Range of domain limits

  • name – Name of the membership function

  • complement – If is True, the complement evaluation is performed

compute(x)[source]

Perform the evaluation of the membership function

Parameters

x (number, list, numpy array) – Input

Returns

evaluation result

set_params(params)[source]

Set the new parameters to design the membership function

Parameters

params – list of membership function design parameters

property spread

Calculates the spread of the membership function

Returns

(float) spread

class FuzzySystem.membership_function.GBellmf(params, universe=None, name=None, complement=False)[source]

Class to define a Generalized Gaussian Bell Membership Function

Parameters
  • params – List of design values of the membership function

  • universe – Range of domain limits

  • name – Name of the membership function

  • complement – If is True, the complement evaluation is performed

compute(x)[source]

Perform the evaluation of the membership function

Parameters

x (number, list, numpy array) – Input

Returns

evaluation result

set_params(params)[source]

Set the new parameters to design the membership function

Parameters

params – list of membership function design parameters

property spread

Calculates the spread of the membership function

Returns

(float) spread

class FuzzySystem.membership_function.Gaussmf(params, universe=None, name=None, complement=False)[source]

Class to define a Gaussian Membership Function

Parameters
  • params – List of design values of the membership function

  • universe – Range of domain limits

  • name – Name of the membership function

  • complement – If is True, the complement evaluation is performed

area()[source]

Calculates the area of the membership function

Returns

(float) area

centroid()[source]

Calculates the centroid of the membership function

Returns

(float) centroid

compute(x)[source]

Perform the evaluation of the membership function

Parameters

x (number, list, numpy array) – Input

Returns

evaluation result

set_params(params)[source]

Set the new parameters to design the membership function

Parameters

params – list of membership function design parameters

property spread

Calculates the spread of the membership function

Returns

(float) spread

class FuzzySystem.membership_function.Logmf(params, universe=None, name=None, complement=False)[source]

Class to define a Logistic Membership Function

Parameters
  • params – List of design values of the membership function

  • universe – Range of domain limits

  • name – Name of the membership function

  • complement – If is True, the complement evaluation is performed

compute(x)[source]

Perform the evaluation of the membership function

Parameters

x (number, list, numpy array) – Input

Returns

evaluation result

set_params(params)[source]

Set the new parameters to design the membership function

Parameters

params – list of membership function design parameters

property spread

Calculates the spread of the membership function

Returns

(float) spread

class FuzzySystem.membership_function.MembershipFunction(params, universe=None, name=None, complement=False)[source]

Abstract class to define a Membership Function

Parameters
  • params – List of design values of the membership function

  • universe – Range of domain limits

  • name – Name of the membership function

  • complement – If is True, the complement evaluation is performed

property area

Calculates the area of the membership function

Returns

(float) area

property centroid

Calculates the centroid of the membership function

Returns

(float) centroid

complement()[source]

Performs the complement evaluation to the membership function

Returns

self

compute(x)[source]

Perform the evaluation of the membership function

Parameters

x (number, list, numpy array) – Input

Returns

evaluation result

eval(x)[source]

Evaluates the membership function given a input x

Parameters

x (number, list.) – input

Returns

resulted value(s) of the evaluation

name = 'Membership Function'

Default reference name

set_params(params)[source]

Set the new parameters to design the membership function

Parameters

params – list of membership function design parameters

show(points=350, axes=None, fmt='-', kwargs={})[source]

Plots the membership function

Parameters
  • points – number of points to evaluate the membership function

  • axes (plt.axes) – for external plotting.

  • fmt – style format of the line

  • kwargs – additional values for plt

property spread

Calculates the spread of the membership function

Returns

(float) spread

class FuzzySystem.membership_function.Sigmoidmf(params, universe=None, name=None, complement=False)[source]

Class to define a Sigmoid Membership Function

Parameters
  • params – List of design values of the membership function

  • universe – Range of domain limits

  • name – Name of the membership function

  • complement – If is True, the complement evaluation is performed

compute(x)[source]

Perform the evaluation of the membership function

Parameters

x (number, list, numpy array) – Input

Returns

evaluation result

set_params(params)[source]

Set the new parameters to design the membership function

Parameters

params – list of membership function design parameters

property spread

Calculates the spread of the membership function

Returns

(float) spread

class FuzzySystem.membership_function.Tanhmf(params, universe=None, name=None, complement=False)[source]

Class to define a Tanh Membership Function

Parameters
  • params – List of design values of the membership function

  • universe – Range of domain limits

  • name – Name of the membership function

  • complement – If is True, the complement evaluation is performed

compute(x)[source]

Perform the evaluation of the membership function

Parameters

x (number, list, numpy array) – Input

Returns

evaluation result

set_params(params)[source]

Set the new parameters to design the membership function

Parameters

params – list of membership function design parameters

property spread

Calculates the spread of the membership function

Returns

(float) spread

class FuzzySystem.membership_function.Trapmf(params, universe=None, name=None, complement=False)[source]

Class to define a Trapezoidal Membership Function

Parameters
  • params – List of design values of the membership function

  • universe – Range of domain limits

  • name – Name of the membership function

  • complement – If is True, the complement evaluation is performed

area()[source]

Calculates the area of the membership function

Returns

(float) area

centroid()[source]

Calculates the centroid of the membership function

Returns

(float) centroid

compute(x)[source]

Perform the evaluation of the membership function

Parameters

x (number, list, numpy array) – Input

Returns

evaluation result

set_params(params)[source]

Set the new parameters to design the membership function

Parameters

params – list of membership function design parameters

property spread

Calculates the spread of the membership function

Returns

(float) spread

class FuzzySystem.membership_function.Trimf(params, universe=None, name=None, complement=False)[source]

Class to define a Triangular Membership Function

Parameters
  • params – List of design values of the membership function

  • universe – Range of domain limits

  • name – Name of the membership function

  • complement – If is True, the complement evaluation is performed

area()[source]

Calculates the area of the membership function

Returns

(float) area

centroid()[source]

Calculates the centroid of the membership function

Returns

(float) centroid

compute(x)[source]

Perform the evaluation of the membership function

Parameters

x (number, list, numpy array) – Input

Returns

evaluation result

set_params(params)[source]

Set the new parameters to design the membership function

Parameters

params – list of membership function design parameters

property spread

Calculates the spread of the membership function

Returns

(float) spread

FuzzySystem.membership_function.divide(x, y)[source]

Perform deviation with clip value [, MAX float]

Parameters
  • x – value x

  • y – value y

Returns

x/y, if y equals 0, then return the biggest float value

FuzzySystem fuzzyset

class FuzzySystem.fuzzyset.FuzzySet(name, mf, fs_operator='min')[source]

A class for modeling Fuzzy Sets

Parameters
  • name (str) – label assigned to the fuzzy set

  • mf (MembershipFunction) – membership function to associate with the linguistic value

  • fs_operator (str) – firing strength operator. “min” or “prod”

complement()[source]

Performs the complement operation to the fuzzy set

Returns

self

cut(firing_strength, and_op=None)[source]

Establishes a firing strength value

Parameters
  • firing_strength (float) – value in [0,1]

  • and_op (str) – conjunction operation. “min” or “prod”

Returns

self

eval(x, fs_operator=None)[source]

Evaluate the fuzzy set given an input

Parameters
  • x (list, numpy array, dictionary or pandas data frame) – inputs values

  • fs_operator (str) – firing strength operator. “min” or “prod”

Returns

The membership function evaluation

show(points=350, axes=None)[source]

Plots the fuzzy set’s membership function

Parameters
  • points (int) – number of samples to evaluate the membership function

  • axes (plt.axes) – for outside plotting

property universe

Universe range, [min, max]

FuzzySystem fuzzyvariable

class FuzzySystem.fuzzyvariable.FuzzyVariable(name, fuzzysets, universe=None)[source]

A class for modeling Fuzzy Variables

Parameters
  • name (str) – Label to fuzzy variable domain

  • fuzzysets – List of fuzzy sets which the fuzzy variable can take as values

  • universe – Range that delimits the inferior and superior domain limits.

discrete_universe(points=350, universe=None)[source]

Creates a list of numbers between the universe limits range

Parameters
  • points – number of points to generate

  • universe – range of the universe limits

Returns

Array of numbers in the universe

eval(x)[source]

Evaluates the fuzzy variable given some input

Parameters

x (number, list, dictionary, pandas data frame) – Input values

Returns

Array of tuples with the name and evaluation of each fuzzy set [(name, eval),…]

get(name)[source]

Recall a fuzzy set object given a name

Parameters

name – name of the fuzzy set to get

Returns

FuzzySet object

show(points=350, axes=None, format_strings='-')[source]

Plots all fuzzy set’s membership function in the fuzzy variable

Parameters
  • points (int) – number of samples to evaluate the membership function

  • axes (plt.axes) – for outside plotting

  • format_strings – line style

FuzzySystem fuzzyrule

class FuzzySystem.fuzzyrule.Antecedent(proposition=None, conector=None)[source]

A class to create conditional term of a IF-THEN fuzzy rule

Parameters
  • proposition – [List of proposition] the operation FuzzyVariable[fuzzy_set_name] creates a proposition.

  • conector – [str] defines the type or logical relationship between propositions

add(other)[source]

Adds some proposition to the actual antecedent

Parameters

other – [Proposition] the proposition to add

eval(x, and_op='min', or_op='max')[source]

Performs the evaluation of the antecedents

Parameters
  • x – [dict, list, pandas Data Frame] input values

  • and_op – [str] type of disjunction operation. “min” or “prod”

  • or_op – [str] type of conjunction operation. “max” or “sum”

Returns

[float] the antecedent firing strength

property fuzzy_variables

Gets the fuzzy variable in the propositions in form of dict

get(variable)[source]

Gets the related fuzzy set given a name of a fuzzy variable

Parameters

variable – [str] fuzzy variable name in the antecedent

Returns

[FuzzySet] A fuzzy set if exits

show()[source]

prints the antecedent in natural language

class FuzzySystem.fuzzyrule.Consequent(proposition=None)[source]

A class to create consequent term of a IF-THEN fuzzy rule

Parameters

proposition – [List of proposition] the operation FuzzyVariable[fuzzy_set_name] creates a proposition.

add(other)[source]

Adds some proposition to the actual consequent

Parameters

other – [Proposition] the proposition to add

eval(x, and_op=None)[source]

Performs the evaluation of the consequent

Parameters
  • x – [float] firing strength value

  • and_op – [str] type of disjunction operation for the implication. “min” or “prod”

Returns

[FuzztSet] the resulted fuzzy set with a firing strength value

property fuzzy_variables

Gets the fuzzy variable in the propositions in form of dict

get(variable)[source]

Gets the related fuzzy set given a name of a fuzzy variable

Parameters

variable – [str] fuzzy variable name in the antecedent

Returns

[FuzzySet] A fuzzy set if exits

show()[source]

prints the antecedent in natural language

class FuzzySystem.fuzzyrule.FuzzyRule(antecedent=None, consequent=None, conector=None, and_op='min', or_op='max', weight=1)[source]

A class for modeling fuzzy rules

Parameters
  • antecedent – [Antecedent] an antecedent object that conforms the rule

  • consequent – [list of Consequent] a list of consequent objects that conforms the rule

  • and_op – [str] type of disjunction operation. “min” or “prod”

  • or_op – [str] type of conjunction operation. “max” or “sum”

  • weight – [float] weight value for the rule

eval(x, and_op=None, or_op=None, verbose=False)[source]

Performs the implication process in the fuzzy rule

Parameters
  • x – [float, array, dict, pandas Data Frame] input values

  • and_op – [str] type of disjunction operation. “min” or “prod”

  • or_op – [str] type of conjunction operation. “max” or “sum”

  • verbose – print process information

Returns

[list of Output] outputs of the implication process

property inputs

Gets the list of all fuzzy variables in the antecedent

Returns

dictionary of the fuzzy variables

property outputs

Gets the list of all fuzzy variables in the consequent

Returns

dictionary of the fuzzy variables or functions

show()[source]

Prints the rule in natural language

class FuzzySystem.fuzzyrule.TSKConsequent(function, params=None, label=None)[source]

A class to design consequent models type Sugeno

Parameters
  • function – [str, callable] type of output function. “linear” or “constant”

  • params – [array] coefficients array

  • label – [str] given name to the consequent

add(other)[source]

Adds some output consequent

Parameters

other – [TSKConsequent] the new output to add

eval(x)[source]

Evaluates the TSKConsequent functions

Parameters

x – [array, float] inputs to evaluate the TSKConsequent

Returns

resulted values of the functions evaluations

get_params()[source]

:return coefficients of the function

set_params(params)[source]

Establishes new parameters to be feed to the function

Parameters

params – [array] coefficients value

FuzzySystem.fuzzyrule.constant_function(x, params)[source]

Performs a constant evaluation. Gives params value as output

Parameters
  • x – [float, array] inputs values

  • params – [float] constant value

Returns

constant value given by params

FuzzySystem.fuzzyrule.linear_function(x, params)[source]

Performs the dot product between inputs and a set of coefficients

Parameters
  • x – [array] inputs

  • params – [array] coefficients

Returns

[float] resulted dot product operation

FuzzySystem output

class FuzzySystem.output.Output(fuzzy_output, universe=None, type='Mamdani')[source]

A class to represent the output of the FIS evaluation

Parameters
  • fuzzy_output – the resulted list of consequent evaluations

  • universe – range of the domain limits

  • type (str) – fuzzy inference system type. “Mamdani” or “Sugeno”

property fuzzysets

Gets the consequent fuzzy sets

get_array(nout=0)[source]

Recalls an specific output

Parameters

nout – index of the output to recall

Returns

Output object

get_outputs(nout=0)[source]

Gets the list of output’ names

Parameters

nout – number of a specific output to get their name

Returns

List of names

static output_toDict(output)[source]

Converts the output list to a dictionary

Parameters

output – a Output object to convert

Returns

Dictionary of the output

show(defuzzifier=None, points=350, axes=None, label=True, nout=0)[source]

Plots the output of a given evaluated FIS

Parameters
  • defuzzifier (Defuzzifier) – (optional) shows the crisp value of the output given a defuzzifier

  • points – number of points to evaluate the output area

  • axes – for external plotting

  • label – label to show in the figure

  • nout – index of a specific output

FuzzySystem defuzzifier

class FuzzySystem.defuzzifier.CenterOfSets(output, universe=None, samples=350, nout=0, multiple_instances=True)[source]

A class for Center of Sets method defuzzification

compute(fs)[source]

abstract method. Performs the defuzzification process

Parameters

fs – [firing strength] firing strength values

Returns

crisp values

name = 'Center of Sets'

Default reference name

class FuzzySystem.defuzzifier.Centroid(output, universe=None, samples=350, nout=0, multiple_instances=True)[source]

A class for Centroid method defuzzification

compute(fs)[source]

abstract method. Performs the defuzzification process

Parameters

fs – [firing strength] firing strength values

Returns

crisp values

name = 'Centroid'

Defaulf reference name

class FuzzySystem.defuzzifier.Defuzzifier(output, universe=None, samples=350, nout=0, multiple_instances=True)[source]

Abstract class to design defuzzifiers

Parameters
  • output – [Output] an output resulted of the implication process

  • universe – [list] range of the domain limits [min, max]

  • samples – [int] number of samples used to calculate the defuzzification

  • nout – [int] index of the output to defuzzify

  • multiple_instances – [bool] flag to define if the Output corresponds to multiple outputs

compute(fs)[source]

abstract method. Performs the defuzzification process

Parameters

fs – [firing strength] firing strength values

Returns

crisp values

eval()[source]

Performs the defuzzification process

Returns

the crisp value

class FuzzySystem.defuzzifier.FirstOfMaximum(output, universe=None, samples=350, nout=0, multiple_instances=True)[source]

A class for First of Maximum method defuzzification

compute(fs)[source]

abstract method. Performs the defuzzification process

Parameters

fs – [firing strength] firing strength values

Returns

crisp values

name = 'First Of Maximum'

Default reference name

class FuzzySystem.defuzzifier.Heights(output, universe=None, samples=350, nout=0, multiple_instances=True)[source]

A class for Heights method defuzzification

compute(fs)[source]

abstract method. Performs the defuzzification process

Parameters

fs – [firing strength] firing strength values

Returns

crisp values

name = 'Heights'

Default reference name

class FuzzySystem.defuzzifier.LastOfMaximum(output, universe=None, samples=350, nout=0, multiple_instances=True)[source]

A class for Last of Maximum method defuzzification

compute(fs)[source]

abstract method. Performs the defuzzification process

Parameters

fs – [firing strength] firing strength values

Returns

crisp values

name = 'Least Of Maximum'

Default reference name

class FuzzySystem.defuzzifier.MeanOfMaximum(output, universe=None, samples=350, nout=0, multiple_instances=True)[source]

A class for Mean of Maximum method defuzzification

compute(fs)[source]

abstract method. Performs the defuzzification process

Parameters

fs – [firing strength] firing strength values

Returns

crisp values

name = 'Mean Of Maximum'

Default reference name

class FuzzySystem.defuzzifier.ModifiedHeights(output, universe=None, samples=350, nout=0, multiple_instances=True)[source]

A class for Modified Heights method defuzzification

compute(fs)[source]

abstract method. Performs the defuzzification process

Parameters

fs – [firing strength] firing strength values

Returns

crisp values

name = 'Modified Heights'

Default reference name

class FuzzySystem.defuzzifier.TSKDefuzzifier(output)[source]

A class to perform the weighted average of the function outputs

Parameters

output – [Output] the output object to perform the defuzzification

eval()[source]

Evaluates and computes de crisp value

Returns

output crisp value

name = 'TSK Defuzzifier'

Default reference name

FuzzySystem nonsingleton

class FuzzySystem.nonsingleton.NonSingleton(mf=None, values=None)[source]

A Class for representing Non-singleton inputs to the system

Parameters
  • mf (MembershipFunction) – Membership Function to map the input

  • values – Actual crisp numbers to evaluate the input

eval()[source]

Performs the Non-singleton evaluation

Returns

Resulted values from the function evaluation characterized by a MembershipFunction object

FuzzySystem fuzzy_operations

FuzzySystem.fuzzy_operations.algebraic_prod(x, y=None)[source]

Performs algebraic product between x and y, or x if y is None

Parameters
  • x – [array] values

  • y – [array, optional] values

Returns

array of values. x*y

FuzzySystem.fuzzy_operations.algebraic_sum(x, y=None)[source]

Performs algebraic sum between x and y, or x if y is None

Parameters
  • x – [array] values

  • y – [array, optional] values

Returns

array of values. x + y - x*y

FuzzySystem.fuzzy_operations.intersection(x, y, type='min')[source]

Performs intersection between x and y

Parameters
  • x – [array] values

  • y – [array] values

  • type – [str] union operator. “min” or “prod”

Returns

array of values

FuzzySystem.fuzzy_operations.maximum(x, y=None)[source]

Performs the maximum between x and y, or x if y is None

Parameters
  • x – [array] values

  • y – [array, optional] values

Returns

array of values. max(x,y)

FuzzySystem.fuzzy_operations.minimum(x, y=None)[source]

Performs the minimum between x and y, or x if y is None

Parameters
  • x – [array] values

  • y – [array, optional] values

Returns

array of values. min(x,y)

FuzzySystem.fuzzy_operations.union(x, y, type='max')[source]

Performs union between x and y

Parameters
  • x – [array] values

  • y – [array] values

  • type – [str] union operator. “max” or “sum”

Returns

array of values

FuzzySystem utils

FuzzySystem.utils.format_inputs(x, inputs=None, data_columns=None, verbose=False)[source]

Adapts a given input to a dictionary of input values

Parameters
  • x (number, list) – input values

  • inputs – list of features names

  • data_columns – list of features names from a Pandas Data Frame object

  • verbose – if True, prints status information

Returns

Dictionary of inputs

FuzzySystem.utils.fuzzy_similarity(A, B, points=100, method='sim1')[source]

Performs the fuzzy similarity measure between two fuzzy sets.

Parameters
  • A – Fuzzy set A

  • B – Fuzzy set B

  • points – number of points to evaluate each fuzzy set

  • method – type of similitude to calculate. sim1 to sim12

Returns

value that represent their similitude

FuzzySystem.utils.get_fuzzy_operators(and_op='min', or_op='max')[source]

Return the functions to perform the disjunction and conjunction operation

Parameters
  • and_op – type of operator. “min” or “prod”

  • or_op – type of operator. “max” or “sum”

Returns

A list of functions to perform disjunction and conjunction, respectively

FuzzySystem matlab_importer

FuzzySystem.matlab_importer.import_fis_matlab(file)[source]

Function to import a matlab type fis file to create an FuzzySystem object

Parameters

file – [str] path to the file to import

Returns

[FuzzySystem] a FuzzySystem equivalent to the input fis configuration file

Indices and tables