Created
August 16, 2024 17:30
-
-
Save timotta/db6b037d75c921d707e1c3f4f50aa690 to your computer and use it in GitHub Desktop.
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
from sklearn.preprocessing import OneHotEncoder | |
from sklearn.base import BaseEstimator, TransformerMixin | |
class OneHotEconderByColumn(TransformerMixin, BaseEstimator): | |
def __init__(self, *, columns): | |
self.columns = columns | |
self.ohe = OneHotEncoder(drop="first", sparse=False) | |
def fit(self, X, y=None): | |
self.ohe.fit(X[self.columns]) | |
return self | |
def transform(self, X): | |
enconded_values = self.ohe.transform(X[self.columns]) | |
encoded_df = pd.DataFrame( | |
enconded_values, columns=self.ohe.get_feature_names_out() | |
) | |
return pd.concat([X.drop(columns=self.columns), encoded_df], axis=1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment