使用 featuretools dfs 计算时间窗配置文件

calculate time-windowed profiles with featuretools dfs

我无法理解 cutoff_dates 概念。 我真正想要的是在 window 之前计算不同的功能,比如说 60 天前(没有当前交易),cutoff_dates 看起来像示例中的硬编码日期。 我正在为每一行使用时间索引(下面的 A_time),并且根据这里的文档 what_is_cutoff_datetime :

The time index is defined as the first time that any information from a row can be used. If a cutoff time is specified when calculating features, rows that have a later value for the time index are automatically ignored.

所以不清楚我是否不输入截止日期,该特征是否会计算到时间索引值。

这是我的实体集定义:

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

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

感谢提问。您可以使用 DFS 中的训练 window 基于时间 window 计算特征。您还可以通过设置 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 使用训练 window 来计算基于时间 window 的特征。在此示例中,我们将训练 window 设置为 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,则每个客户的所有交易都将包含在计算中。如果这有帮助,请告诉我。