【问题标题】:Featuretools relationship with non unique join keyFeaturetools 与非唯一连接键的关系
【发布时间】:2019-02-27 00:41:55
【问题描述】:

假设我有两张表,一张包含有关客户的元数据,字段为 customer_id,而事件表记录自网站点击流事件,字段为 customer_iddate。显然,第二个表可能有几个非唯一事件(不幸的是,日期实际上只是一个日期而不是时间戳)。

当尝试创建 https://docs.featuretools.com/loading_data/using_entitysets.html 时失败:

Index is not unique on dataframe (Entity transactions)

我怎样才能让它独一无二或让它发挥作用?

【问题讨论】:

    标签: python feature-extraction feature-engineering featuretools


    【解决方案1】:

    如果您的表没有可用作唯一索引的列,您可以让 featuretools 自动创建一个。调用EntitySet.entity_from_dataframe(...) 时,只需将数据框中当前不存在的列名提供给index 参数并设置make_index=True。这将自动创建一个具有唯一值的列。

    例如,在下面的代码中,event_id 索引是自动创建的

    import pandas as pd
    import featuretools as ft
    
    df = pd.DataFrame({"customer_id": [0, 1, 0, 1, 1],
                       "date": [pd.Timestamp("1/1/2018"), pd.Timestamp("1/1/2018"),
                                pd.Timestamp("1/1/2018"), pd.Timestamp("1/2/2018"),
                                pd.Timestamp("1/2/2018")],
                       "event_type": ["view", "purchase", "view", "cancel", "purchase"]})
    
    es = ft.EntitySet(id="customer_events")                
    es.entity_from_dataframe(entity_id="events",
                             dataframe=df,
                             index="event_id",
                             make_index=True,
                             time_index="date")
    
    print(es["events"])
    

    在事件实体中,您可以看到 event_id 现在是一个变量,即使它不在原始数据帧中

    Entity: events
      Variables:
        event_id (dtype: index)
        date (dtype: datetime_time_index)
        customer_id (dtype: numeric)
        event_type (dtype: categorical)
      Shape:
        (Rows: 5, Columns: 4)
    

    【讨论】:

      猜你喜欢
      • 2019-02-27
      • 2019-01-21
      • 2013-09-29
      • 2011-11-27
      • 2020-06-09
      • 2012-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多