代码之家  ›  专栏  ›  技术社区  ›  Tomas Greif

在Featuretools中计算多个训练窗口中的功能

  •  1
  • Tomas Greif  · 技术社区  · 8 年前

    我有一张有顾客和交易的桌子。有没有办法获得在过去3/6/9/12个月内过滤的功能?我想自动生成功能:

    • 过去3个月的交易次数
    • ....
    • 过去12个月的交易次数
    • ...

    我试过用 training_window =["1 month", "3 months"], ,但它似乎不会为每个窗口返回多个功能。

    例子:

    import featuretools as ft
    es = ft.demo.load_mock_customer(return_entityset=True)
    
    window_features = ft.dfs(entityset=es,
       target_entity="customers",
       training_window=["1 hour", "1 day"],
       features_only = True)
    
    window_features
    

    我是否必须单独处理各个窗口,然后合并结果?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Max Kanter    8 年前

    如前所述,在Featuretools 0.2.1中,必须为每个训练窗口分别构建特征矩阵,然后合并结果。举个例子,你可以这样做:

    import pandas as pd
    import featuretools as ft
    es = ft.demo.load_mock_customer(return_entityset=True)
    cutoff_times = pd.DataFrame({"customer_id": [1, 2, 3, 4, 5],
                                 "time": pd.date_range('2014-01-01 01:41:50', periods=5, freq='25min')})
    features = ft.dfs(entityset=es,
                      target_entity="customers",
                      agg_primitives=['count'],
                      trans_primitives=[],
                      features_only = True)
    fm_1 = ft.calculate_feature_matrix(features, 
                                       entityset=es, 
                                       cutoff_time=cutoff_times,
                                       training_window='1h', 
                                       verbose=True)
    
    fm_2 = ft.calculate_feature_matrix(features, 
                                       entityset=es, 
                                       cutoff_time=cutoff_times,
                                       training_window='1d', 
                                       verbose=True)
    new_df = fm_1.reset_index()
    new_df = new_df.merge(fm_2.reset_index(), on="customer_id", suffixes=("_1h", "_1d"))
    

    然后,新的数据帧将如下所示:

    customer_id COUNT(sessions)_1h  COUNT(transactions)_1h  COUNT(sessions)_1d COUNT(transactions)_1d
    1           1                   17                      3                 43
    2           3                   36                      3                 36
    3           0                   0                       1                 25
    4           0                   0                       0                 0
    5           1                   15                      2                 29
    
    推荐文章