<s> from base import ComboFeature , Feature <EOL> from pandas import DataFrame , Series , concat <EOL> class ComboMap ( ComboFeature ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , features , name = None , fillna = <NUM_LIT:0> ) : <EOL> super ( ComboMap , self ) . __init__ ( features ) <EOL> self . fillna = fillna <EOL> def _combine ( self , datas , function ) : <EOL> data = datas [ <NUM_LIT:0> ] <EOL> data = data . astype ( '<STR_LIT>' ) <EOL> for d in datas [ <NUM_LIT:1> : ] : <EOL> if isinstance ( d , DataFrame ) : <EOL> if len ( d . columns ) == <NUM_LIT:1> : <EOL> d = d [ d . columns [ <NUM_LIT:0> ] ] <EOL> else : <EOL> raise NotImplementedError <EOL> cols = [ '<STR_LIT>' % ( c , d . name ) for c in data . columns ] <EOL> data = function ( data , d . astype ( '<STR_LIT>' ) , axis = <NUM_LIT:0> ) <EOL> data = data . fillna ( self . fillna ) <EOL> data . columns = cols <EOL> return data <EOL> class Add ( ComboMap ) : <EOL> def _combine_apply ( self , datas , fitted_feature ) : <EOL> return self . _combine ( datas , DataFrame . add ) <EOL> class Divide ( ComboMap ) : <EOL> def _combine_apply ( self , datas , fitted_feature ) : <EOL> return self . _combine ( datas , DataFrame . div ) <EOL> class Multiply ( ComboMap ) : <EOL> def _combine_apply ( self , datas , fitted_feature ) : <EOL> return self . _combine ( datas , DataFrame . mul ) <EOL> class Sub ( ComboMap ) : <EOL> def _combine_apply ( self , datas , fitted_feature ) : <EOL> return self . _combine ( datas , DataFrame . sub ) <EOL> class Interactions ( ComboFeature ) : <EOL> def _combine_apply ( self , datas , fitted_feature ) : <EOL> cols = [ ] <EOL> colnames = [ ] <EOL> data = concat ( datas , axis = <NUM_LIT:1> ) <EOL> n = len ( data . columns ) <EOL> for i in range ( n ) : <EOL> d1 = data [ data . columns [ i ] ] <EOL> for j in range ( i + <NUM_LIT:1> , n ) : <EOL> d2 = data [ data . columns [ j ] ] <EOL> d = d1 . mul ( d2 ) <EOL> colnames . append ( '<STR_LIT>' % ( d1 . name , d2 . name ) ) <EOL> cols . append ( d ) <EOL> return concat ( cols , keys = colnames , axis = <NUM_LIT:1> ) <EOL> class DimensionReduction ( ComboFeature ) : <EOL> def __init__ ( self , feature , decomposer = None ) : <EOL> super ( DimensionReduction , self ) . __init__ ( feature ) <EOL> self . decomposer = decomposer <EOL> def _prepare ( self , prep_datas ) : <EOL> prep_data = concat ( prep_datas , axis = <NUM_LIT:1> ) <EOL> self . decomposer . fit ( prep_data . values ) <EOL> return self . decomposer <EOL> def _combine_apply ( self , datas , fitted_feature ) : <EOL> data = concat ( datas , axis = <NUM_LIT:1> ) <EOL> decomposer = fitted_feature . prepped_data <EOL> decomp = decomposer . transform ( data ) <EOL> df = DataFrame ( decomp , <EOL> columns = [ '<STR_LIT>' % i for i in range ( decomp . shape [ <NUM_LIT:1> ] ) ] , <EOL> index = data . index ) <EOL> return df </s>
