Created
January 23, 2017 16:36
-
-
Save stavinsky/c0411ccfd60b5c51f81060b504dc33ed to your computer and use it in GitHub Desktop.
Python sklearn pipline helpers
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.base import TransformerMixin | |
class TransformWrapper(TransformerMixin): | |
"""simple wrapper for instruments like LabelEncoder. | |
It is usefull if we want to encode many features at one time. | |
They have a little bit different interface compairing to OneHotEncoder for example""" | |
def __init__(self, enc): | |
self.enc = enc | |
def fit(self, X, y=None): | |
self.d = {} | |
for col in X.columns: | |
self.d[col] = self.enc() | |
self.d[col].fit(X[col].values) | |
return self | |
def transform(self, X, y=None): | |
l = list() | |
for col in X.columns: | |
l.append(self.d[col].transform(X[col].values).reshape(-1,1)) | |
result = np.hstack(l) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment