Fuzzy Transformer
These handle the fuzzification of our data.
This module provides a scikit-learn compatible FuzzyTransformer for converting numerical features into fuzzy membership values and handling categorical features via one-hot encoding. It also includes helper functions for fuzzy logic operations.
- class zuffy.fuzzy_transformer.FuzzyTransformer(non_fuzzy: List[str] | None = None, oob_check: bool = False, tags: List[str] = ['low', 'med', 'high'], feature_names: List[str] | None = None, show_fuzzy_range: bool = True, verbose: bool = False)[source]
Bases:
BaseEstimator,TransformerMixinA scikit-learn compatible transformer that converts numerical features into fuzzy membership values using triangular membership functions. It also handles specified non-fuzzy (categorical) columns using one-hot encoding.
This transformer is designed to preprocess data for Fuzzy Pattern Tree models by converting raw numerical inputs into fuzzy memberships and handling nominal categorical features.
Parameters
- non_fuzzylist of str, default=None
A list of column names that should NOT be fuzzified. These columns will instead be one-hot encoded. If None or an empty list, all detected numerical columns will be fuzzified.
- oob_checkbool, default=False
If True, the transformer will check for out-of-bounds numerical values (values falling outside the min/max range observed during fit for fuzzified columns) and raise a ValueError. If False (default), such out-of-bounds values will have their fuzzy membership values automatically clamped to 0.0 by the trimf function without raising an error.
- tagslist of str, default=[‘low’, ‘med’, ‘high’]
A list of three strings to be used as suffixes for the generated fuzzy feature names (e.g., ‘low_feature_name’, ‘med_feature_name’). The order corresponds to the ‘low’, ‘medium’, and ‘high’ membership functions, respectively. Must contain exactly three strings.
- feature_nameslist of str, default=None
Optional list of input feature names. - If X passed to fit is a Pandas DataFrame, its column names will be
used, and this parameter will be ignored.
If X is a NumPy array, these names will be used to assign column names for internal DataFrame processing and output feature naming. The length must match X.shape[1].
- show_fuzzy_rangebool, default=True
If True, the names of the output fuzzy features will include the numerical range (e.g., ‘low feature_name (0.00 to 5.00)’). If False, only the tag and feature name will be used (e.g., ‘low feature_name’).
- verbosebool, default=False
If True, the transformer will print progress and debugging information during fit and transform operations.
Attributes
- n_features_in_int
The number of features seen during fit.
- feature_names_in_ndarray of str or None
Names of features seen during fit. Defined only when X has feature names that are all strings (e.g., a Pandas DataFrame), or when feature_names is explicitly provided in __init__ and X is a NumPy array.
- columns_pandas.Index
The column names of the input DataFrame (or names derived from feature_names if a NumPy array was provided) seen during fit. This attribute ensures consistency between fit and transform calls.
- fuzzy_bounds_dict
A dictionary storing the (a, b, c) parameters for the triangular membership functions for each numerical column that was fuzzified. Keys are original column names, values are tuples of floats.
- categorical_values_dict
A dictionary storing the unique categories for each column specified in non_fuzzy. This ensures consistent one-hot encoding during transform. Keys are original column names, values are lists of unique categories.
- feature_names_out_list of str
A list of names for the features generated after transformation. This attribute is available after the fit method has been called.
See Also
trimf : Triangular membership function. pandas.get_dummies : Convert categorical variable into dummy/indicator variables.
Notes
The transformer automatically identifies numerical columns for fuzzification. Columns specified in non_fuzzy (which are assumed categorical) are handled via one-hot encoding. If X is a NumPy array, it expects numerical data only unless non_fuzzy is used and feature_names is explicitly provided.
- _parameter_constraints: Dict[str, List[Any]] = {'feature_names': [<class 'list'>, None], 'non_fuzzy': [<class 'list'>, None], 'oob_check': [<class 'bool'>], 'show_fuzzy_range': [<class 'bool'>], 'tags': [<class 'list'>], 'verbose': [<class 'bool'>]}
- _sklearn_auto_wrap_output_keys = {'transform'}
- fit(X: DataFrame | ndarray, y: ndarray | None = None) FuzzyTransformer[source]
Fits the FuzzyTransformer by determining fuzzy bounds for numerical features and unique categories for categorical features.
This method analyzes the input data X to learn the necessary parameters for subsequent transformation.
Parameters
- Xpandas.DataFrame or numpy.ndarray of shape (n_samples, n_features)
The training input samples. If a NumPy array, it will be internally converted to a DataFrame to handle columns by name, using feature_names provided in __init__ or default names.
- yNone, default=None
Ignored. Present for API consistency as a scikit-learn transformer.
Returns
- selfFuzzyTransformer
The fitted transformer instance.
Raises
- ValueError
If X is a NumPy array and feature_names was not provided in __init__. If a column designated for fuzzification contains non-numeric data.
- transform(X: DataFrame | ndarray) ndarray[source]
Transforms the input data into fuzzy membership values and one-hot encoded categorical features based on the fit method’s learnings.
Parameters
- Xpandas.DataFrame or numpy.ndarray of shape (n_samples, n_features)
The input data to transform. It must have the same number of features and, if a DataFrame, the same column names and order as the data used during fit.
Returns
- X_transformednumpy.ndarray of shape (n_samples, n_transformed_features)
The transformed data, consisting of fuzzy membership values for numerical columns and one-hot encoded values for categorical columns.
Raises
- NotFittedError
If the transformer has not been fitted yet (i.e., fit has not been called).
- ValueError
If the number of features in X does not match n_features_in_, if DataFrame column names/order mismatch columns_, or if out-of-bounds numerical values are encountered and oob_check is True.
- zuffy.fuzzy_transformer.convert_to_numeric(df: DataFrame, target: str) Tuple[List[str], DataFrame][source]
Converts values in a specified target column of a DataFrame into integers using LabelEncoder.
This utility function is useful for preparing categorical target variables for machine learning models that require numerical inputs.
Parameters
- dfpandas.DataFrame
The input DataFrame.
- targetstr
The name of the column in the DataFrame to be converted.
Returns
- classesnumpy.ndarray
A NumPy array of the original unique class labels found in the target column, in the order they were encoded.
- dfpandas.DataFrame
The DataFrame with the specified target column converted to integer labels.
Raises
- ValueError
If the target column is not found in the DataFrame.
- zuffy.fuzzy_transformer.trimf(feature: ndarray, abc: Sequence[float]) ndarray[source]
Calculates the fuzzy membership values of a feature using the triangular membership function.
The triangular membership function is defined by three parameters [a, b, c], where ‘a’ and ‘c’ are the base points and ‘b’ is the peak point. Membership value is 0 for feature <= a or feature >= c, 1 for feature = b, and linearly interpolated between a and b, and b and c.
Parameters
- featurenumpy.ndarray
Crisp input values (e.g., a feature vector). Must be a 1D array or convertible to a 1D array.
- abcSequence[float], length 3
Parameters defining the triangular function: [a, b, c]. Parameters a and c are the base of the function and b is the peak. Requires a <= b <= c.
Returns
- ynumpy.ndarray
A 1D array of fuzzy membership values represented by the triangular membership function, with values in the range [0, 1].
Raises
- ValueError
If abc does not have exactly three elements or if a > b or b > c.