featuretools:手动推导dfs生成的特征?

featuretools: manual derivation of the features generated by dfs?

代码示例:

import featuretools as ft
es = ft.demo.load_mock_customer(return_entityset=True)

# Normalized one more time
es = es.normalize_entity(
    new_entity_id="device",
    base_entity_id="sessions",
    index="device", 
)
feature_matrix, feature_defs = ft.dfs(
    entityset=es,
    target_entity="customers",
    agg_primitives=["std",],
    groupby_trans_primitives=['cum_count'],
    max_depth=2
)

我想更深入地研究 STD(sessions.CUM_COUNT(device) by customer_id) 功能:

我尝试手动生成此功能,但结果不同:

df = ft.demo.load_mock_customer(return_single_table=True)

a = df.groupby("customer_id")['device'].cumcount()
a.name = "cumcount_device"
a = pd.concat([df, a], axis=1)
b = a.groupby("customer_id")['cumcount_device'].std()

>>> b
customer_id
1   36.517
2   26.991
3   26.991
4   31.610
5   22.949
Name: cumcount_device, dtype: float64

我错过了什么?

感谢提问。计算需要基于会话中的数据框。

df = es['sessions'].df
cumcount = df['device'].groupby(df['customer_id']).cumcount()
std = cumcount.groupby(df['customer_id']).std()
std.round(3).loc[feature_matrix.index]
customer_id
5    1.871
4    2.449
1    2.449
3    1.871
2    2.160
dtype: float64

您应该得到与 DFS 相同的输出。