Created
April 19, 2021 21:47
-
-
Save gvyshnya/782162ec091dc5298061ac25c16a1552 to your computer and use it in GitHub Desktop.
FW Add Interaction feature snippet
This file contains hidden or 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
intxn_vars = [('cont3', 'cont7'),('cont3', 'cont8'),('cont3', 'cont9'),('cont3', 'cont10'),('cont4', 'cont5'), | |
('cont4', 'cont6'),('cont4', 'cont9'),('cont4', 'cont10')] | |
def FE_create_interaction_vars(df, intxn_vars): | |
""" | |
This handy function creates interaction variables among pairs of numeric vars you send in. | |
Your input must be a dataframe and a list of tuples. Each tuple must contain a pair of variables. | |
All variables must be numeric. Double check your input before sending them in. | |
""" | |
df = df.copy(deep=True) | |
for (each_intxn1,each_intxn2) in intxn_vars: | |
new_col = each_intxn1 + '_x_' + each_intxn2 | |
try: | |
df[new_col] = df[each_intxn1] * df[each_intxn2] | |
except: | |
continue | |
return df | |
train = FE_create_interaction_vars(train, intxn_vars) | |
test = FE_create_interaction_vars(test, intxn_vars) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment