Back to the Machine Learning fundamentals: How to write code for Model deployment (Part 3/3)
class Encoder(BaseEstimator, TransformerMixin): | |
""" A transformer that returns DataFrame | |
with variable encoded. | |
Parameters | |
---------- | |
encoding_meta : list, default=None | |
""" | |
def __init__(self, encoding_meta=None): | |
if not isinstance(encoding_meta, dict): | |
logging.error('The config file is corrupted in encoding_meta key!') | |
sys.exit(1) | |
else: | |
self.encoding_meta = encoding_meta | |
# We have fit method cause Sklearn Pipeline | |
def fit(self, X, y=None): | |
return self | |
def transform(self, X): | |
X = X.copy() | |
for var, meta in self.encoding_meta.items(): | |
if var not in X.columns.values.tolist(): | |
pass | |
X[var] = X[var].map(meta) | |
return X |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment