Skip to content

Instantly share code, notes, and snippets.

@stavinsky
Created January 23, 2017 16:36
Show Gist options
  • Save stavinsky/c0411ccfd60b5c51f81060b504dc33ed to your computer and use it in GitHub Desktop.
Save stavinsky/c0411ccfd60b5c51f81060b504dc33ed to your computer and use it in GitHub Desktop.
Python sklearn pipline helpers
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