Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customer Transformers Not Working #1360

Open
habeeb3579 opened this issue Jan 13, 2025 · 1 comment
Open

Customer Transformers Not Working #1360

habeeb3579 opened this issue Jan 13, 2025 · 1 comment

Comments

@habeeb3579
Copy link

TPOT does not work with custom transformers. When I ran to optimization below, I got the warning that "Warning: Normalization2 is not available and will not be used by TPOT."

class Normalization2(BaseEstimator, TransformerMixin):
def init(self, method='minmax', feature_range=(0, 1)):
self.method = method
self.feature_range = feature_range
self.min_ = None
self.max_ = None

def fit(self, spectrum):
    if self.method == 'minmax':
        self.min_ = np.min(spectrum)
        self.max_ = np.max(spectrum)
    elif self.method == 'zscore':
        self.mean = np.mean(spectrum)
        self.std = np.std(spectrum)
    else:
        raise ValueError("Unsupported normalization method. Choose 'minmax' or 'zscore'.")
    return self

def transform(self, spectrum):
    if self.method == 'minmax':
        if self.min_ is None or self.max_ is None:
            raise ValueError("Normalization instance is not fitted yet.")
        range_min, range_max = self.feature_range
        return ((spectrum - self.min_) / (self.max_ - self.min_)) * (range_max - range_min) + range_min
    elif self.method == 'zscore':
        if self.mean is None or self.std is None:
            raise ValueError("Normalization instance is not fitted yet.")
        return (spectrum - self.mean) / self.std
        
 config = {
'Normalization2': {  # Wrapper for Normalization
    'method': ['minmax'],
    'feature_range': [(0, 1)],
},
'sklearn.linear_model.Ridge': {  # Replace PLSRegression with Ridge
    'alpha': [0.1, 1.0, 10.0],  # Regularization strength
    'solver': ['auto', 'svd', 'cholesky', 'lsqr', 'sag', 'saga'],  # Solvers to optimize
},

}

spot = TPOTRegressor(
generations=100,
population_size=100,
cv=3
config_dict=config,
verbosity=2,
n_jobs=-1,
max_eval_time_mins=10
)

@perib
Copy link
Contributor

perib commented Jan 13, 2025

TPOT can only import from modules. To get this to work you first have to put your custom class in a module. For example

mymodule (folder):
    __init__.py
    myclassfile.py
myscript.py

where myclassfile.py contains your custom class. __init__.py imports the file. Then you change the config dictionary to reference this class.

For example

myclassfile.py

from sklearn.base import BaseEstimator, TransformerMixin
from tpot import TPOTRegressor

class Normalization2(BaseEstimator, TransformerMixin):
    def init(self, method='minmax', feature_range=(0, 1)):
        self.method = method
        self.feature_range = feature_range
        self.min_ = None
        self.max_ = None

    def fit(self, spectrum):
        if self.method == 'minmax':
            self.min_ = np.min(spectrum)
            self.max_ = np.max(spectrum)
        elif self.method == 'zscore':
            self.mean = np.mean(spectrum)
            self.std = np.std(spectrum)
        else:
            raise ValueError("Unsupported normalization method. Choose 'minmax' or 'zscore'.")
        return self

    def transform(self, spectrum):
        if self.method == 'minmax':
            if self.min_ is None or self.max_ is None:
                raise ValueError("Normalization instance is not fitted yet.")
            range_min, range_max = self.feature_range
            return ((spectrum - self.min_) / (self.max_ - self.min_)) * (range_max - range_min) + range_min
        elif self.method == 'zscore':
            if self.mean is None or self.std is None:
                raise ValueError("Normalization instance is not fitted yet.")
            return (spectrum - self.mean) / self.std
        

init.py

from .myclassfile import Normalization2

myscript.py

...
config = {
'mymodule.Normalization2': {  # Wrapper for Normalization
    'method': ['minmax'],
    'feature_range': [(0, 1)],
},
'sklearn.linear_model.Ridge': {  # Replace PLSRegression with Ridge
    'alpha': [0.1, 1.0, 10.0],  # Regularization strength
    'solver': ['auto', 'svd', 'cholesky', 'lsqr', 'sag', 'saga'],  # Solvers to optimize
},

}
...

(This is not necessary in TPOT2 which uses a different API)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants