Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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.
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!')
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():
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