【问题标题】:FeatureTools: Can there be multiple columns in time_index parameters?FeatureTools:time_index 参数中可以有多个列吗?
【发布时间】:2020-01-29 02:06:33
【问题描述】:

我在数据帧中有 2 个时间参数,即 start_date 和 end_date,当从数据帧创建实体集时,它们都是时间参数。

在指定time_index的同时,我们可以指定2个不同的列吗?

我不想创建一个合并两列的新列,因为我想为两列获取 agg_primitives,例如“time_since_first”、“time_since_last”、“avg_time_between”、“trend”。

请告诉我。

【问题讨论】:

    标签: python python-3.x datetimeindex featuretools


    【解决方案1】:

    这可以通过将第二个时间列指定为DatetimeTimeIndex 来完成。这是一个使用演示数据集的示例。 ​

    import featuretools as ft
    ​
    df = ft.demo.load_mock_customer(return_single_table=True)
    df = df.filter(regex='amount|customer|time')
    

    ​ 我创建了第二个时间列。 ​

    df['transaction_time_2'] = df['transaction_time']
    df.head()
    
         transaction_time  amount  customer_id  transaction_time_2
    0 2014-01-01 00:00:00  127.64            2 2014-01-01 00:00:00
    1 2014-01-01 00:09:45   57.39            2 2014-01-01 00:09:45
    2 2014-01-01 00:14:05   69.45            2 2014-01-01 00:14:05
    3 2014-01-01 02:33:50  123.19            2 2014-01-01 02:33:50
    4 2014-01-01 02:37:05   64.47            2 2014-01-01 02:37:05
    

    ​ 然后,我创建一个实体集。我使用variable_types 参数将我的第二个时间列设置为DatetimeTimeIndex 变量类型。 ​

    es = ft.EntitySet()
    es.entity_from_dataframe(
        'transactions',
        df,
        time_index='transaction_time',
        index='id',
        make_index=True,
        variable_types={
            'transaction_time_2': ft.variable_types.DatetimeTimeIndex,
        }
    )
    es.normalize_entity('transactions', 'customers', index='customer_id')
    

    ​ 最后,我计算特征矩阵。我们可以看到基于时间的原语应用于两个时间列。 ​

    fm, fd = ft.dfs(
        target_entity='customers',
        entityset=es,
        agg_primitives=[
            "time_since_first",
            "time_since_last",
            "avg_time_between",
            "trend",
        ],
        trans_primitives=[],
    )
    ​
    print(fm.iloc[0].to_string())
    
    TIME_SINCE_FIRST(transactions.transaction_time)      1.822703e+08
    TIME_SINCE_FIRST(transactions.transaction_time_2)    1.822703e+08
    TIME_SINCE_LAST(transactions.transaction_time)       1.822401e+08
    TIME_SINCE_LAST(transactions.transaction_time_2)     1.822401e+08
    AVG_TIME_BETWEEN(transactions.transaction_time)      3.285326e+02
    AVG_TIME_BETWEEN(transactions.transaction_time_2)    3.285326e+02
    TREND(transactions.amount, transaction_time)        -5.251887e+01
    TREND(transactions.amount, transaction_time_2)      -5.251887e+01
    

    ​ 让我知道这是否有帮助。

    【讨论】:

    • 感谢这帮助并解决了问题。但是有一个疑问:与规范化实体相比,您为什么不创建一个新关系?两者有什么区别?
    • 我很高兴听到这个消息。使用normalize_entity 自动创建关系。我建议在文档中查看this example
    猜你喜欢
    • 2016-03-25
    • 1970-01-01
    • 2014-05-11
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多