【问题标题】:How to normalize the entity having multiple values for the one feature in featuretools?如何规范化特征工具中一个特征具有多个值的实体?
【发布时间】:2021-03-01 16:50:28
【问题描述】:

下面是一个例子:

buy_log_df = pd.DataFrame(
    [
        ["2020-01-02", 0, 1, 2, 2],
        ["2020-01-02", 1, 1, 1, 3],
        ["2020-01-02", 2, 2, 1, 1],
        ["2020-01-02", 3, 3, 3, 1],
    ],
    columns=['date', 'sale_id', 'customer_id', "item_id", "quantity"]
)

item_df = pd.DataFrame(
    [
        [1, 100],
        [2, 200],
        [3, 300],
    ],
    columns=['item_id', 'price']
)

item_df2 = pd.DataFrame(
    [
        [1, '1 3 10'],
        [2, '1 3'],
        [3, '2 5'],
    ],
    columns=['item_id', 'tags']
)

正如您在此处看到的,item_df 中的每个项目都有多个标签值作为一个特征。

这是我尝试过的:

item_df2 = pd.concat([item_df2, item_df2['tags'].str.split(expand=True)], axis=1)
item_df2 = pd.melt(
    item_df2,
    id_vars=['item_id'],
    value_vars=[0,1,2],
    value_name="tags"
)
tag_log_df = item_df2[item_df2['tags'].notna()].drop("variable", axis=1,).sort_values("item_id")
tag_log_df

>>>

   item_id tags
0        1    1
3        1    3
6        1   10
1        2    1
4        2    3
2        3    2
5        3    5

看起来我无法规范化这个项目实体(来自 buy_log 实体),因为它在表中有多个重复的 item_ids。

我在设计实体集时如何处理这种情况?

【问题讨论】:

    标签: featuretools


    【解决方案1】:

    感谢您的提问。要处理多个标签值,您可以在构建实体集之前将标签规范化为数据框。

    buy_log_df

           date  sale_id  customer_id  item_id  quantity
     2020-01-02        0            1        2         2
     2020-01-02        1            1        1         3
     2020-01-02        2            2        1         1
     2020-01-02        3            3        3         1
    

    item_df

     item_id  price
           1    100
           2    200
           3    300
    

    tag_log_df

     item_id tags
           1    1
           1    3
           1   10
           2    1
           2    3
           3    2
           3    5
    

    使用标准化数据,您可以构建实体集。

    es = ft.EntitySet()
    
    es.entity_from_dataframe(
        entity_id='buy_log',
        dataframe=buy_log_df,
        index='sale_id',
        time_index='date',
    )
    
    es.entity_from_dataframe(
        entity_id='item',
        dataframe=item_df,
        index='item_id',
    )
    
    es.entity_from_dataframe(
        entity_id='tag_log',
        dataframe=tag_log_df,
        index='tag_log_id',
        make_index=True,
    )
    
    parent = es['item']['item_id']
    child = es['buy_log']['item_id']
    es.add_relationship(ft.Relationship(parent, child))
    
    child = es['tag_log']['item_id']
    es.add_relationship(ft.Relationship(parent, child))
    

    【讨论】:

    • 感谢您的回答。但是还有一个问题:如果包含项目价格buy_log_dfitem_df 最初不存在,我可以使用normalize_entity()item es 创建item es 吗?对我来说,看起来我可以将normalize_entity() 用于 1:N 关系,将add_relationship() 用于上面类似标签的情况。我对吗? (是的,这个问题与this question有关)
    • 正确,在这种情况下,您可以使用normalize_entity()buy_log创建item
    猜你喜欢
    • 1970-01-01
    • 2019-01-30
    • 2022-04-26
    • 2019-05-30
    • 1970-01-01
    • 2021-11-09
    • 2018-11-15
    • 1970-01-01
    • 2022-10-07
    相关资源
    最近更新 更多