Source code for chemotools.baseline._non_negative
"""
The :mod:`chemotools.baseline._non_negative` module implements
a non-negative transformer.
"""
# Author: Pau Cabaneros
# License: MIT
from typing import Literal
import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
from sklearn.utils.validation import check_is_fitted, validate_data
[docs]
class NonNegative(TransformerMixin, OneToOneFeatureMixin, BaseEstimator):
"""
A transformer that sets all negative values to zero or to abs.
Parameters
----------
mode : Literal["zero", "abs"], optional, default="zero"
The mode to use for the non-negative values. Can be:
- *zero*: set all negative values to zero.
- *abs*: set all negative values to their absolute value.
Attributes
----------
n_features_in_ : int
The number of features in the input data.
Examples
--------
>>> from chemotools.baseline import NonNegative
>>> from chemotools.datasets import load_fermentation_train
>>> # Load sample data
>>> X, _ = load_fermentation_train()
>>> # Instantiate the transformer
>>> transformer = NonNegative(mode="zero")
NonNegative(mode="zero")
>>> transformer.fit(X)
>>> # Generate non-negative data
>>> X_non_negative = transformer.transform(X)
"""
def __init__(self, mode: Literal["zero", "abs"] = "zero"):
self.mode = mode
[docs]
def fit(self, X: np.ndarray, y=None) -> "NonNegative":
"""
Fit the transformer to the input data.
Parameters
----------
X : np.ndarray of shape (n_samples, n_features)
The input data to fit the transformer to.
y : None
Ignored to align with API.
Returns
-------
self : NonNegative
The fitted transformer.
"""
# Check that X is a 2D array and has only finite values
X = validate_data(
self, X, y="no_validation", ensure_2d=True, reset=True, dtype=np.float64
)
return self