Created
August 1, 2020 13:48
-
-
Save IvanNardini/4f104ca1a784f725ab4b09aab9f32cf2 to your computer and use it in GitHub Desktop.
Back to the Machine Learning fundamentals: How to write code for Model deployment (Part 3/3)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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