如何在Featuretools中为具有相同ID和时间索引的行创建功能?

问题描述

我有一个这样的数据框

data = {'Customer':['C1','C1','C2','C3','C3'],'NumOfItems':[3,2,4,5,6,10,14],'PurchaseTime':["2014-01-01","2014-01-02","2014-01-03","2014-01-01","2014-01-03"]
       }
df = pd.DataFrame(data)
df

我想创建一个功能,例如,这是到目前为止每个客户的最大值:

'MaxPerID(NumOfItems)':[3,3,14] #the output i want

所以我设置了EntitySet并将其标准化...

es = ft.EntitySet(id="customer_data")
es = es.entity_from_dataframe(entity_id="customer",dataframe=df,index='index',time_index="PurchaseTime",make_index=True)

es = es.normalize_entity(base_entity_id="customer",new_entity_id="sessions",index="Customer")

但是创建特征矩阵不会产生我想要的结果。

feature_matrix,features = ft.dfs(entityset=es,target_entity="customer",agg_primitives = ["max"],max_depth = 3                                      
                                 )
feature_matrix.head

sessions.MAX(customer.NumOfItems)  
index                                                                         
0                                      4                                    
3                                      6                                    
6                                     14                                    
1                                      4                                    
4                                      6                                    
7                                     14                                    
2                                      4                                    
5                                      6                                    
8                                     14                                    

返回的功能是所有客户每天的最高价值(按时间排序),但是,如果我在没有time_index = "PurchaseTime"的情况下运行相同的代码,则结果仅是特定客户

    sessions.MAX(customer.NumOfItems)  \
index                                                                       
0                    4   
1                    4   
2                    4   
3                    6   
4                    6   
5                    6   
6                   14   
7                   14   
8                   14   
                             

我希望将两者结合起来:到目前为止,特定客户的最大值。 这可能吗?我尝试与es['customer']['Customer'].interesting_values =['C1','C3']合作,但没有成功。我还尝试修改新的规范化实体并为此编写自己的图元。

我是Featuretool的新手,所以将不胜感激。

This Question is similar to mine but the solution has no time_index and is creating the new features on the normalized entity

解决方法

感谢您的提问。您可以通过使用group by transform原语来获得预期的输出。

fm,fd = ft.dfs(
    entityset=es,target_entity="customer",groupby_trans_primitives=['cum_max'],)

您应该获得每个客户的商品数量的累计最大值。

column = 'CUM_MAX(NumOfItems) by Customer'
actual = fm[[column]].sort_values(column)
expected = {'MaxPerID(NumOfItems)': [3,3,4,5,6,10,14]}
actual.assign(**expected)
       CUM_MAX(NumOfItems) by Customer  MaxPerID(NumOfItems)
index
0                                  3.0                     3
1                                  3.0                     3
2                                  4.0                     4
3                                  5.0                     5
4                                  5.0                     5
5                                  6.0                     6
6                                 10.0                    10
7                                 10.0                    10
8                                 14.0                    14

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...