teacher.fuzzy#
The teacher.fuzzy module provides methods and classes
to represent fuzzy elements. This includes fuzzification procedures
to transform regular datasets into fuzzy datasets, as well as
classes to represent the Fuzzy Sets and Fuzzy Variables.
Fuzzification Functions#
These functions provide an interface to fuzzify a dataset that is
preloaded into a pandas.DataFrame. These functions are:
get_fuzzy_points()This function provides three methods of discretization to divide a
pandas.DataFrameinto triangular fuzzy sets. These three methods are:Equal width: Divides each variable into triangular sets of equal width.
Equal frequency: Divides each variable into triangular sets of equal frequency.
Entropy: Divides each variable using the fuzzy partitioning based on fuzzy entropy used here
get_fuzzy_variables()This function takes a list of points that define triangular fuzzy sets and returns the adequate
FuzzyVariableobjects. These points can be extracted via theget_fuzzy_points()method or can be introduced manually given they are obtained through other means (i.e.: an expert).dataset_membership()This function takes a dataset and a set of fuzzy variables and returns a dictionary with the pertenence of each instance of the dataset to the different fuzzy sets of each variable.
Classes#
FuzzyVariableThis class represents a fuzzy variable that has a name, a membership function and some Fuzzy Sets that can be either discrete or continuous.
FuzzyDiscreteSetThis class represents a discrete fuzzy set, whose membership function is either 1 if the variable takes this value or 0 if it does not.
FuzzyContinuousSetThis class represents a triangular continuous fuzzy set, whose membership function varies depending on how close the variable is from the peak of the set.
- teacher.fuzzy.get_fuzzy_points(discretize_method, df_numerical_columns, X, y=None, sets=0, point_variables=None, max_depth=0, th=None, debug=False)[source]#
Obtain the peak of the fuzzy triangles of the continuous variables of a dataset.
- Parameters:
discretize_method (str, {'equal_freq', 'equal_width', 'entropy'}) – Function used to get the divisions.
df_numerical_columns (list) – Ordered numerical columns of the input samples.
X (array-like, of shape (n_samples, n_features)) – Training input samples. Must only have numerical columns.
y (array-like of shape (n_samples,)) – Target values (class labels) as integers or strings.
sets (int) – Number of fuzzy sets that the variable will be divided into.
point_variables (set, None by default) – Set of the variables to be considered point variables to return a list with the point value.
debug (boolean, False by default) – Debugging flag
- Returns:
Dictionary with the format {feature_name : [start, peak, end]} for each feature in df_numerical_columns
- Return type:
- Raises:
ValueError – Discretize method not supported
- teacher.fuzzy.get_fuzzy_variables(continuous_fuzzy_points, discrete_fuzzy_values, order, continuous_labels=None, discrete_labels=None, point_set_method='point_set')[source]#
Build the fuzzy variables given the points of the triangles that define them, as well as the values of the discrete variables.
- Parameters:
continuous_fuzzy_points (dict) – Dictionary with format {feature: [peak_1, peak_2, …]} with the name of the features and the peaks of the triangles of each fuzzy set.
discrete_fuzzy_values (dict) – Dictionary with format {feature : [value_1, value_2, …]} with the name of the features and the unique values thatthe discrete variable can take
order (dict) – Dictionary with format {name : position} where each name is the label of the fuzzy variable and the position relative to an input dataset
continuous_labels (dict, optional) – Dictionary with format {feature : [label_1, label_2, …]} with the name the continuous variable and the labels of the fuzzy sets associated to the peaks peak_1, peak_2, …
discrete_labels (dict, optional) – Dictionary with format {feature : [label_1, label_2, …]} with the name the discrete variable and the labels of the fuzzy sets associated to the values value_1, value_2, …
point_set (str, 'point_set' by default) – Method to generate the point sets. Defaults to point_set
- Returns:
Ordered list of all the fuzzy variables
- Return type:
- teacher.fuzzy.dataset_membership(X, fuzzy_variables)[source]#
Compute the membership of the values of all the instances of a dataset to each Fuzzy Set of the different Fuzzy Variables.
- Parameters:
X (array-like, of shape (n_samples, n_features)) – The input samples of which to obtain the membership.
fuzzy_variables (list[FuzzyVariable]) – List of the fuzzy variables to compute the membership. Must be ordered according to the features of X.
- Returns:
Dictionary with format {variable: {set_1: pert_1, …}, …} with all the variables in fuzzy_variables and the pertenence degree to all the corresponding sets.
- Return type:
- class teacher.fuzzy.FuzzyVariable(name: str, fuzzy_sets: list)[source]#
Dataclass that represents a fuzzy variable by assigning a name and a list of fuzzy sets
- membership(variable)[source]#
Compute the membership degree of the variable to the different fuzzy sets of the variable
- Parameters:
variable (array-like, of shape (n_features)) – The variable to compute the membership degree
- Returns:
{set_1: pert_1, …} for each fuzzy set of the variable
- Return type:
- class teacher.fuzzy.FuzzyDiscreteSet(name: str, value: str)[source]#
Dataclass that represents a fuzzy continuous set by assigning a name that represents the value of this set.
- membership(variable)[source]#
Take the values of a variable and returns an array with the membership of those values to the Fuzzy Set
- Parameters:
variable (numpy.ndarray) – Array with values of the variable
- intersection(other)[source]#
Intersect two fuzzy sets of the same type
- Parameters:
other (FuzzySet) – Set to intersect with the current object
- Returns:
Degree of intersection
- Return type:
- Raises:
ValueError – If the set is not of the same subtype
- simmilarity(other)[source]#
Compute the similarity between two fuzzy sets of the same type
- Parameters:
other (FuzzySet) – Set to compute the similarity with the current object
- Returns:
Degree of similarity
- Return type:
- Raises:
ValueError – If the set is not of the same subtype
- class teacher.fuzzy.FuzzyContinuousSet(name: str, fuzzy_points: list, point_set: bool = False)[source]#
Dataclass that represents a fuzzy continuous set by assigning a name and a list of points that represent the triangles
- membership(variable)[source]#
Take the values of a variable and returns an array with the membership of those values to the Fuzzy Set
- Parameters:
variable (numpy.ndarray) – Array with values of the variable
- intersection(other)[source]#
Intersect two fuzzy sets of the same type
- Parameters:
other (FuzzySet) – Set to intersect with the current object
- Returns:
Degree of intersection
- Return type:
- Raises:
ValueError – If the set is not of the same subtype
- simmilarity(other)[source]#
Compute the similarity between two fuzzy sets of the same type
- Parameters:
other (FuzzySet) – Set to compute the similarity with the current object
- Returns:
Degree of similarity
- Return type:
- Raises:
ValueError – If the set is not of the same subtype