<s> """<STR_LIT>""" <EOL> import copy <EOL> import pandas as pd <EOL> from sklearn . base import BaseEstimator , TransformerMixin <EOL> from patsy . highlevel import dmatrix <EOL> from category_encoders . ordinal import OrdinalEncoder <EOL> __author__ = '<STR_LIT>' <EOL> def polynomial_coding ( X_in , cols = None ) : <EOL> """<STR_LIT>""" <EOL> X = copy . deepcopy ( X_in ) <EOL> if cols is None : <EOL> cols = X . columns . values <EOL> pass_thru = [ ] <EOL> else : <EOL> pass_thru = [ col for col in X . columns . values if col not in cols ] <EOL> bin_cols = [ ] <EOL> for col in cols : <EOL> mod = dmatrix ( "<STR_LIT>" % ( col , ) , X ) <EOL> for dig in range ( len ( mod [ <NUM_LIT:0> ] ) ) : <EOL> X [ col + '<STR_LIT>' % ( dig , ) ] = mod [ : , dig ] <EOL> bin_cols . append ( col + '<STR_LIT>' % ( dig , ) ) <EOL> X = X . reindex ( columns = bin_cols + pass_thru ) <EOL> return X <EOL> class PolynomialEncoder ( BaseEstimator , TransformerMixin ) : <EOL> """<STR_LIT:U+0020>""" <EOL> def __init__ ( self , verbose = <NUM_LIT:0> , cols = None ) : <EOL> """<STR_LIT>""" <EOL> self . verbose = verbose <EOL> self . cols = cols <EOL> def fit ( self , X , y = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self <EOL> def transform ( self , X ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( X , pd . DataFrame ) : <EOL> X = pd . DataFrame ( X ) <EOL> return polynomial_coding ( X , cols = self . cols ) </s>
