【问题标题】:calculate time-windowed profiles with featuretools dfs使用 featuretools dfs 计算时间窗轮廓
【发布时间】:2021-02-24 04:29:00
【问题描述】:

我无法理解 cutoff_dates 概念。 我真正想要的是通过一个时间窗口计算不同的特征,比如 60 天前(没有当前事务),cutoff_dates 看起来像示例中的硬编码日期。 我正在为每一行使用时间索引(下面的 A_time),并根据此处what_is_cutoff_datetime 中的文档:

时间索引定义为第一次可以使用行中的任何信息。如果在计算特征时指定了截止时间,则时间索引值较晚的行将被自动忽略。

所以不清楚我是否不把截止日期计算到时间索引值之前。

这是我的实体集定义:

es = ft.EntitySet('payment')
es = es.entity_from_dataframe(entity_id='tableA',
                           dataframe=tableA_dfpd,
                           index='paymentIndex',
                           time_index='A_time')


es.normalize_entity(base_entity_id='tableA',
               new_entity_id='tableB',
               index='B_index',
               additional_variables=['B_x','B_time'],
               make_time_index='B_time')
               
es.normalize_entity(base_entity_id='tableA',
               new_entity_id='tableC',
               index='C_index',
               additional_variables=["C_x","C_date"],
               make_time_index="C_date")

es.normalize_entity(base_entity_id='tableA',
               new_entity_id='tableD',
               index='D_index',
               additional_variables=["D_x"],
               make_time_index=False)
               
Entityset: payment
  Entities:
    tableA [Rows: 310083, Columns: 8]
    tableB [Rows: 30296, Columns: 3]
    tableC [Rows: 206565, Columns: 3]
    tableD [Rows: 18493, Columns: 2]
  Relationships:
    tableA.B_index -> tableB.B_index
    tableA.C_index -> tableC.C_index
    tableA.D_index -> tableD.D_index

我如何准确地进行窗口计算?我是否需要通过截止日期?到dfs方法? 我想使用基于 A_time 变量的所有窗口计算,直到当前交易的 60 天窗口,所以实际上每笔交易的截止日期是该交易的 time_A 值。,不是吗?

【问题讨论】:

    标签: featuretools


    【解决方案1】:

    感谢您的提问。您可以使用 DFS 中的训练窗口基于时间窗口计算要素。您还可以通过设置include_cutoff_time=False 在截止时间排除交易。我将使用这个交易数据集来举例说明。

    import featuretools as ft
    
    df = ft.demo.load_mock_customer(return_single_table=True)
    df = df[['transaction_id', 'transaction_time', 'customer_id', 'amount']]
    df.sort_values(['customer_id', 'transaction_time'], inplace=True)
    df.head()
    
     transaction_id    transaction_time  customer_id  amount
                290 2014-01-01 00:44:25            1   21.35
                275 2014-01-01 00:45:30            1  108.11
                101 2014-01-01 00:46:35            1  112.53
                 80 2014-01-01 00:47:40            1    6.29
                484 2014-01-01 00:48:45            1   47.95
    

    首先,我们为交易和客户创建一个实体集。

    es = ft.EntitySet()
    
    es.entity_from_dataframe(
        entity_id='transactions',
        index='transaction_id',
        time_index='transaction_time',
        dataframe=df,
    )
    
    es.normalize_entity(
        base_entity_id='transactions',
        new_entity_id='customers',
        index='customer_id',
    )
    
    es.add_last_time_indexes()
    
    Entityset: None
      Entities:
        transactions [Rows: 500, Columns: 4]
        customers [Rows: 5, Columns: 2]
      Relationships:
        transactions.customer_id -> customers.customer_id
    

    然后,我们为每个客户在每笔交易中创建一个截止时间。

    cutoff_time = df[['customer_id', 'transaction_time']]
    cutoff_time['time'] = cutoff_time.pop('transaction_time')
    cutoff_time.head()
    
     customer_id                time
               1 2014-01-01 00:44:25
               1 2014-01-01 00:45:30
               1 2014-01-01 00:46:35
               1 2014-01-01 00:47:40
               1 2014-01-01 00:48:45
    

    现在,我们可以使用训练窗口运行 DFS,以根据时间窗口计算特征。在本例中,我们将训练窗口设置为 1 小时。这将包括每位客户截止时间前 1 小时内的所有交易。

    默认情况下,截止时间的交易也包含在计算中。我们可以通过设置include_cutoff_time=False来排除这些交易。

    fm, fd = ft.dfs(
        target_entity='customers',
        entityset=es,
        cutoff_time=cutoff_time,
        include_cutoff_time=False,
        cutoff_time_in_index=True,
        training_window='1h',
        trans_primitives=[],
        agg_primitives=['sum'],
        verbose=True,
    )
    
    fm.sort_index().head() 
    
                                     SUM(transactions.amount)
    customer_id time                                         
    1           2014-01-01 00:44:25                      0.00
                2014-01-01 00:45:30                     21.35
                2014-01-01 00:46:35                    129.46
                2014-01-01 00:47:40                    241.99
                2014-01-01 00:48:45                    248.28
    

    如果截止时间未传递给 DFS,则每个客户的所有交易都包含在计算中。让我知道这是否有帮助。

    【讨论】:

      猜你喜欢
      • 2021-01-27
      • 2014-11-20
      • 2018-08-30
      • 2023-03-21
      • 2018-08-19
      • 2019-12-01
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多