Skip to content

feature_selection

Bases: object

Base class for feature selection.

This class provides an abstract base for implementing feature selection algorithms. It supports incremental feature selection with stopping criteria based on thresholds.

Attributes:

Name Type Description
name str

The name of the feature selection method.

n_feature int

The number of features to select.

incremental bool

Whether to perform incremental feature selection.

incremental_stop_threshold float

The threshold for stopping incremental feature selection.

t_threshold int

A time or iteration threshold for incremental selection.

Methods:

Name Description
get_n_feature

Get the number of features to select.

set_n_feature

Set the number of features to select.

forward

Apply feature selection and return the reduced features.

fit_transform

Fit the feature selection model and return the reduced features.

fit

Abstract method to fit the feature selection model to the input data.

transform

Abstract method to transform the input data using the fitted model.

Source code in tinybig/koala/machine_learning/feature_selection/feature_selection.py
class feature_selection(object):
    """
        Base class for feature selection.

        This class provides an abstract base for implementing feature selection algorithms. It supports
        incremental feature selection with stopping criteria based on thresholds.

        Attributes
        ----------
        name : str
            The name of the feature selection method.
        n_feature : int
            The number of features to select.
        incremental : bool
            Whether to perform incremental feature selection.
        incremental_stop_threshold : float
            The threshold for stopping incremental feature selection.
        t_threshold : int
            A time or iteration threshold for incremental selection.

        Methods
        -------
        get_n_feature()
            Get the number of features to select.
        set_n_feature(n_feature)
            Set the number of features to select.
        forward(X: Union[np.ndarray, torch.Tensor], device: str = 'cpu', *args, **kwargs)
            Apply feature selection and return the reduced features.
        fit_transform(X: Union[np.ndarray, torch.Tensor], device: str = 'cpu', *args, **kwargs)
            Fit the feature selection model and return the reduced features.
        fit(X: Union[np.ndarray, torch.Tensor], device: str = 'cpu', *args, **kwargs)
            Abstract method to fit the feature selection model to the input data.
        transform(X: Union[np.ndarray, torch.Tensor], device: str = 'cpu', *args, **kwargs)
            Abstract method to transform the input data using the fitted model.
    """

    def __init__(self, name: str = 'feature_selection', n_feature: int = None,  incremental: bool = True, incremental_stop_threshold: float = 0.01, t_threshold: int = 100, *args, **kwargs):
        """
            Initialize the feature selection class.

            Parameters
            ----------
            name : str, optional
                The name of the feature selection method. Default is 'feature_selection'.
            n_feature : int, optional
                The number of features to select. Default is None.
            incremental : bool, optional
                Whether to perform incremental feature selection. Default is True.
            incremental_stop_threshold : float, optional
                The threshold for stopping incremental feature selection. Default is 0.01.
            t_threshold : int, optional
                A time or iteration threshold for incremental selection. Default is 100.
            *args, **kwargs
                Additional arguments for customization.
        """

        self.name = name
        self.n_feature = n_feature
        self.incremental = incremental
        self.incremental_stop_threshold = incremental_stop_threshold
        self.t_threshold = t_threshold

    def get_n_feature(self):
        """
            Get the number of features to select.

            Returns
            -------
            int
                The number of features to select.
        """
        return self.n_feature

    def set_n_feature(self, n_feature):
        """
            Get the number of features to select.

            Returns
            -------
            int
                The number of features to select.
        """
        self.n_feature = n_feature

    def __call__(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
        """
            Apply the feature selection model.

            Parameters
            ----------
            X : Union[np.ndarray, torch.Tensor]
                The input data for feature selection.
            device : str, optional
                The device to use for computation ('cpu' or 'cuda'). Default is 'cpu'.
            *args, **kwargs
                Additional arguments for the feature selection process.

            Returns
            -------
            Union[np.ndarray, torch.Tensor]
                The reduced features after selection.
        """
        return self.forward(X=X, device=device, *args, **kwargs)

    def forward(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
        """
            Apply feature selection and return the reduced features.

            Parameters
            ----------
            X : Union[np.ndarray, torch.Tensor]
                The input data for feature selection.
            device : str, optional
                The device to use for computation ('cpu' or 'cuda'). Default is 'cpu'.
            *args, **kwargs
                Additional arguments for the feature selection process.

            Returns
            -------
            Union[np.ndarray, torch.Tensor]
                The reduced features after selection.
        """
        if isinstance(X, torch.Tensor):
            input_X = X.detach().cpu().numpy()
        else:
            input_X = X

        X_reduced = self.fit_transform(X=input_X, device=device, *args, **kwargs)

        return torch.tensor(X_reduced) if isinstance(X, torch.Tensor) and not isinstance(X_reduced, torch.Tensor) else X_reduced

    def fit_transform(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
        """
            Fit the feature selection model and return the reduced features.

            Parameters
            ----------
            X : Union[np.ndarray, torch.Tensor]
                The input data for fitting and transformation.
            device : str, optional
                The device to use for computation ('cpu' or 'cuda'). Default is 'cpu'.
            *args, **kwargs
                Additional arguments for the feature selection process.

            Returns
            -------
            Union[np.ndarray, torch.Tensor]
                The reduced features after fitting and transformation.
        """
        if isinstance(X, torch.Tensor):
            input_X = X.detach().cpu().numpy()
        else:
            input_X = X

        self.fit(X=input_X, device=device, *args, **kwargs)
        X_reduced = self.transform(X=input_X, device=device, *args, **kwargs)

        return torch.tensor(X_reduced) if isinstance(X, torch.Tensor) and not isinstance(X_reduced, torch.Tensor) else X_reduced

    @abstractmethod
    def fit(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
        """
            Fit the feature selection model to the input data.

            Parameters
            ----------
            X : Union[np.ndarray, torch.Tensor]
                The input data for fitting.
            device : str, optional
                The device to use for computation ('cpu' or 'cuda'). Default is 'cpu'.
            *args, **kwargs
                Additional arguments for fitting the feature selection model.
        """
        pass

    @abstractmethod
    def transform(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
        """
            Transform the input data using the fitted feature selection model.

            Parameters
            ----------
            X : Union[np.ndarray, torch.Tensor]
                The input data to transform.
            device : str, optional
                The device to use for computation ('cpu' or 'cuda'). Default is 'cpu'.
            *args, **kwargs
                Additional arguments for the transformation process.
        """
        pass

__call__(X, device='cup', *args, **kwargs)

Apply the feature selection model.

Parameters:

Name Type Description Default
X Union[ndarray, Tensor]

The input data for feature selection.

required
device str

The device to use for computation ('cpu' or 'cuda'). Default is 'cpu'.

'cup'
*args

Additional arguments for the feature selection process.

()
**kwargs

Additional arguments for the feature selection process.

()

Returns:

Type Description
Union[ndarray, Tensor]

The reduced features after selection.

Source code in tinybig/koala/machine_learning/feature_selection/feature_selection.py
def __call__(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
    """
        Apply the feature selection model.

        Parameters
        ----------
        X : Union[np.ndarray, torch.Tensor]
            The input data for feature selection.
        device : str, optional
            The device to use for computation ('cpu' or 'cuda'). Default is 'cpu'.
        *args, **kwargs
            Additional arguments for the feature selection process.

        Returns
        -------
        Union[np.ndarray, torch.Tensor]
            The reduced features after selection.
    """
    return self.forward(X=X, device=device, *args, **kwargs)

__init__(name='feature_selection', n_feature=None, incremental=True, incremental_stop_threshold=0.01, t_threshold=100, *args, **kwargs)

Initialize the feature selection class.

Parameters:

Name Type Description Default
name str

The name of the feature selection method. Default is 'feature_selection'.

'feature_selection'
n_feature int

The number of features to select. Default is None.

None
incremental bool

Whether to perform incremental feature selection. Default is True.

True
incremental_stop_threshold float

The threshold for stopping incremental feature selection. Default is 0.01.

0.01
t_threshold int

A time or iteration threshold for incremental selection. Default is 100.

100
*args

Additional arguments for customization.

()
**kwargs

Additional arguments for customization.

()
Source code in tinybig/koala/machine_learning/feature_selection/feature_selection.py
def __init__(self, name: str = 'feature_selection', n_feature: int = None,  incremental: bool = True, incremental_stop_threshold: float = 0.01, t_threshold: int = 100, *args, **kwargs):
    """
        Initialize the feature selection class.

        Parameters
        ----------
        name : str, optional
            The name of the feature selection method. Default is 'feature_selection'.
        n_feature : int, optional
            The number of features to select. Default is None.
        incremental : bool, optional
            Whether to perform incremental feature selection. Default is True.
        incremental_stop_threshold : float, optional
            The threshold for stopping incremental feature selection. Default is 0.01.
        t_threshold : int, optional
            A time or iteration threshold for incremental selection. Default is 100.
        *args, **kwargs
            Additional arguments for customization.
    """

    self.name = name
    self.n_feature = n_feature
    self.incremental = incremental
    self.incremental_stop_threshold = incremental_stop_threshold
    self.t_threshold = t_threshold

fit(X, device='cup', *args, **kwargs) abstractmethod

Fit the feature selection model to the input data.

Parameters:

Name Type Description Default
X Union[ndarray, Tensor]

The input data for fitting.

required
device str

The device to use for computation ('cpu' or 'cuda'). Default is 'cpu'.

'cup'
*args

Additional arguments for fitting the feature selection model.

()
**kwargs

Additional arguments for fitting the feature selection model.

()
Source code in tinybig/koala/machine_learning/feature_selection/feature_selection.py
@abstractmethod
def fit(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
    """
        Fit the feature selection model to the input data.

        Parameters
        ----------
        X : Union[np.ndarray, torch.Tensor]
            The input data for fitting.
        device : str, optional
            The device to use for computation ('cpu' or 'cuda'). Default is 'cpu'.
        *args, **kwargs
            Additional arguments for fitting the feature selection model.
    """
    pass

fit_transform(X, device='cup', *args, **kwargs)

Fit the feature selection model and return the reduced features.

Parameters:

Name Type Description Default
X Union[ndarray, Tensor]

The input data for fitting and transformation.

required
device str

The device to use for computation ('cpu' or 'cuda'). Default is 'cpu'.

'cup'
*args

Additional arguments for the feature selection process.

()
**kwargs

Additional arguments for the feature selection process.

()

Returns:

Type Description
Union[ndarray, Tensor]

The reduced features after fitting and transformation.

Source code in tinybig/koala/machine_learning/feature_selection/feature_selection.py
def fit_transform(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
    """
        Fit the feature selection model and return the reduced features.

        Parameters
        ----------
        X : Union[np.ndarray, torch.Tensor]
            The input data for fitting and transformation.
        device : str, optional
            The device to use for computation ('cpu' or 'cuda'). Default is 'cpu'.
        *args, **kwargs
            Additional arguments for the feature selection process.

        Returns
        -------
        Union[np.ndarray, torch.Tensor]
            The reduced features after fitting and transformation.
    """
    if isinstance(X, torch.Tensor):
        input_X = X.detach().cpu().numpy()
    else:
        input_X = X

    self.fit(X=input_X, device=device, *args, **kwargs)
    X_reduced = self.transform(X=input_X, device=device, *args, **kwargs)

    return torch.tensor(X_reduced) if isinstance(X, torch.Tensor) and not isinstance(X_reduced, torch.Tensor) else X_reduced

forward(X, device='cup', *args, **kwargs)

Apply feature selection and return the reduced features.

Parameters:

Name Type Description Default
X Union[ndarray, Tensor]

The input data for feature selection.

required
device str

The device to use for computation ('cpu' or 'cuda'). Default is 'cpu'.

'cup'
*args

Additional arguments for the feature selection process.

()
**kwargs

Additional arguments for the feature selection process.

()

Returns:

Type Description
Union[ndarray, Tensor]

The reduced features after selection.

Source code in tinybig/koala/machine_learning/feature_selection/feature_selection.py
def forward(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
    """
        Apply feature selection and return the reduced features.

        Parameters
        ----------
        X : Union[np.ndarray, torch.Tensor]
            The input data for feature selection.
        device : str, optional
            The device to use for computation ('cpu' or 'cuda'). Default is 'cpu'.
        *args, **kwargs
            Additional arguments for the feature selection process.

        Returns
        -------
        Union[np.ndarray, torch.Tensor]
            The reduced features after selection.
    """
    if isinstance(X, torch.Tensor):
        input_X = X.detach().cpu().numpy()
    else:
        input_X = X

    X_reduced = self.fit_transform(X=input_X, device=device, *args, **kwargs)

    return torch.tensor(X_reduced) if isinstance(X, torch.Tensor) and not isinstance(X_reduced, torch.Tensor) else X_reduced

get_n_feature()

Get the number of features to select.

Returns:

Type Description
int

The number of features to select.

Source code in tinybig/koala/machine_learning/feature_selection/feature_selection.py
def get_n_feature(self):
    """
        Get the number of features to select.

        Returns
        -------
        int
            The number of features to select.
    """
    return self.n_feature

set_n_feature(n_feature)

Get the number of features to select.

Returns:

Type Description
int

The number of features to select.

Source code in tinybig/koala/machine_learning/feature_selection/feature_selection.py
def set_n_feature(self, n_feature):
    """
        Get the number of features to select.

        Returns
        -------
        int
            The number of features to select.
    """
    self.n_feature = n_feature

transform(X, device='cup', *args, **kwargs) abstractmethod

Transform the input data using the fitted feature selection model.

Parameters:

Name Type Description Default
X Union[ndarray, Tensor]

The input data to transform.

required
device str

The device to use for computation ('cpu' or 'cuda'). Default is 'cpu'.

'cup'
*args

Additional arguments for the transformation process.

()
**kwargs

Additional arguments for the transformation process.

()
Source code in tinybig/koala/machine_learning/feature_selection/feature_selection.py
@abstractmethod
def transform(self, X: Union[np.ndarray, torch.Tensor], device: str = 'cup', *args, **kwargs):
    """
        Transform the input data using the fitted feature selection model.

        Parameters
        ----------
        X : Union[np.ndarray, torch.Tensor]
            The input data to transform.
        device : str, optional
            The device to use for computation ('cpu' or 'cuda'). Default is 'cpu'.
        *args, **kwargs
            Additional arguments for the transformation process.
    """
    pass