1. 程式人生 > 其它 >特徵工程 python 批量生成變數名

特徵工程 python 批量生成變數名

features = []

diff_windowns = [1,3,6,12]
groups = ['sum','mean','std','max','min','count']

for d in diff_windowns:
    exec("""last_{}_month = df[df['diff_days']<={}].groupby('ACCOUNT')""".format(d,d))
    #last_3_month = df[df['diff_days']<=d].groupby('ACCOUNT')
    exec("""last_{}_month_gp = groupby_feature(last_{}_month)
""".format(d,d)) #features.append() for i,g in enumerate(groups): exec("""last_{}_month_{} = last_{}_month_gp[{}]""".format(d,g,d,i)) #根據不同特徵構造重新命名列名 exec("""last_{}_month_{}.columns = [c + '_last_{}_month_{}' for c in last_{}_month_{}.columns]""".format(d,g,d,g,d,g))
#jupyter 裡面檢視生成的變數
使用命令 %who_ls

[...
...
...
 'last_12_month',
 'last_12_month_count',
 'last_12_month_gp',
 'last_12_month_max',
 'last_12_month_mean',
 'last_12_month_min',
 'last_12_month_std',
 'last_12_month_sum',
 'last_1_month',
 'last_1_month_count',
 'last_1_month_gp',
 'last_1_month_max
', 'last_1_month_mean', 'last_1_month_min', 'last_1_month_std', 'last_1_month_sum', 'last_3_month', 'last_3_month_count', 'last_3_month_gp', 'last_3_month_max', 'last_3_month_mean', 'last_3_month_min', 'last_3_month_std', 'last_3_month_sum', 'last_6_month', 'last_6_month_count', 'last_6_month_gp', 'last_6_month_max', 'last_6_month_mean', 'last_6_month_min', 'last_6_month_std', 'last_6_month_sum', ....]