import copy
from typing import Tuple, Union
import numpy as np
import pandas as pd
from ...utils.constants import NUMERICAL
from ...utils.results import ValidationResult
[docs]
class FEPhaseEncoder:
def __init__(self, dataset):
self.dataset = dataset
self.key = "data_fe_phase_encoder"
self.fitted_ = False
[docs]
def run(self,
features: Union[str, Tuple] = None,
dataset: str = "main",
n_thresholds: int = 5,
include_original: bool = False):
"""Generates smooth threshold-proximity encoding via phase differences.
For each feature, learns percentile thresholds and encodes each value as its
cosine similarity to each threshold in normalized phase space. Avoids hard binning.
Parameters
----------
features : str or tuple, default=None
Features to encode. If None, all numerical features are used.
dataset : {"main", "train", "test"}, default="main"
Dataset used to fit the transformer.
n_thresholds : int, default=5
Number of threshold points per feature (evenly spaced percentiles).
include_original : bool, default=False
If True, prepend original features to output.
"""
from .gaml.phase import PhaseEncoder
inputs = locals()
inputs.pop('self', None)
if features is None:
features = [fn for fn, ft in zip(self.dataset.all_feature_names,
self.dataset.all_feature_types)
if ft == NUMERICAL]
if isinstance(features, str):
features = [features]
self.all_feature_names_in_ = copy.copy(self.dataset.all_feature_names)
self.feature_names_in_ = list(features)
data = self.dataset.get_data(dataset=dataset)
feature_indices = [self.dataset.all_feature_names.index(fn) for fn in features]
X = data[:, feature_indices].astype(np.float32)
self.gaml_transformer_ = PhaseEncoder(n_thresholds=n_thresholds,
include_original=include_original)
self.gaml_transformer_.fit(X)
gaml_names = self.gaml_transformer_.get_feature_names()
self.feature_names_out_ = gaml_names
self.fitted_ = True
result = ValidationResult(key=self.key,
data=self.dataset.name,
inputs=inputs,
value={"n_features_out": len(self.feature_names_out_),
"feature_names_out": self.feature_names_out_})
return result