首页 文章

featuretools的截止时间和训练窗口

提问于
浏览
1

假设我有两个数据集(对应于我的实体集中的两个实体):

第一个:客户(cust_id,name,birthdate,customer_since)
第二个:预订(booking_id,service,chargeamount,booking_date)

现在我想创建一个包含从 all customers (no matter since when they are customer) but only bookings from the last two years 构建的功能的数据集 .

我如何使用“last_time_index”?我可以只将“last_time_index”设置为一个实体吗?在这种情况下,仅适用于预订实体,因为我想要所有客户,但不是所有预订 .

如果使用此代码创建功能:

feature_matrix, features = ft.dfs(entityset=es,
                              target_entity="customers",
                              cutoff_time= pd.to_datetime('30/05/2018'),
                              training_window = ft.Timedelta(2*365,"d"),
                              agg_primitives=["count"],
                              trans_primitives=["time_since","year"],
                              cutoff_time_in_index = True)

1 回答

  • 1

    实体的 time_index 指定实例第一次有效使用 . 这样,您在设置时间索引时所做的选择可能会影响最终结果 . 根据您设置 time_index 的方式,可以使用 ft.dfs 与您的示例中的设置完全匹配,以获得所需的输出 . 这是一个类似于您描述的数据的玩具示例:

    bookings_df = pd.DataFrame()
    bookings_df['booking_id'] = [1, 2, 3, 4]
    bookings_df['cust_id'] = [1, 1, 2, 5]
    bookings_df['booking_date'] = pd.date_range('1/1/2014', periods=4, freq='Y')
    
    customer_df = pd.DataFrame()
    customer_df['cust_id'] = [1, 2, 5]
    customer_df['customer_since']  = pd.to_datetime(['2014-01-01', '2016-01-01', '2017-01-01'])
    
    es = ft.EntitySet('Bookings')
    es.entity_from_dataframe('bookings', bookings_df, 'booking_id', time_index='booking_date')
    es.entity_from_dataframe('customers', customer_df, 'cust_id')
    
    es.add_relationship(ft.Relationship(es['customers']['cust_id'], es['bookings']['cust_id']))
    

    在过去的四年里,我们每年举办一次 bookings_df 活动 . 数据框如下所示:

    booking_id  cust_id  booking_date
    0    1           1        2014-12-31
    1    2           1        2015-12-31
    2    3           2        2016-12-31
    3    4           5        2017-12-31
    

    请注意,我们有 notcustomers 设置时间索引,这意味着所有客户数据始终有效 . 在没有 training_window 参数的情况下运行DFS将返回

    YEAR(customer_since)   COUNT(bookings)
    cust_id     
    1         2014                   2.0
    2         2016                   1.0
    5         2017                   1.0
    

    通过添加两年的 training_window (如您的示例所示),我们仅使用前四个预订中的两个来查看结果:

    YEAR(customer_since)   COUNT(bookings)
    cust_id     
    1         2014                   0.0
    2         2016                   1.0
    5         2017                   1.0
    

相关问题