gpt4 book ai didi

python - 未在 featuretools 中为我的实体集设置生成功能

转载 作者:行者123 更新时间:2023-11-30 09:05:25 24 4
gpt4 key购买 nike

当我尝试在实体集之间创建关系(使用我自己的数据)时遇到问题。没有错误,但它只是没有为我的实体之一(“prods”实体)创建功能,尽管一切都应该连接得很好。

我无法共享我的数据,但我使用一些模拟数据创建了一个最小的示例:

import pandas as pd
import featuretools as ft

创建模拟数据

cust = pd.DataFrame([[1,50],[2,60]], 
columns=['CUST_ID','AGE'])#

orders = pd.DataFrame([[1,1,50,33.0],[2,1,60,20],[3,2,66,999.9]],
columns=['ORD_ID','CUST_ID','QTY','PRICE'])

order_items = pd.DataFrame([[1,1,1,2,3.0],[2,2,2,8,5.0],[3,2,1,2,3.0],[4,3,3,2,3.0]],
columns=['ORD_ITM_ID','ORD_ID','PROD_ID','QTY','PRICE'])

prods = pd.DataFrame([[1,3.0],[2,5.0],[3,3.0]],
columns=['PROD_ID','PRICE'])

定义实体集

es = ft.EntitySet('test')

## Adding Customers Entity

es.entity_from_dataframe(dataframe=cust,
entity_id='cust',
index='CUST_ID')

## Adding Orders Entity
es.entity_from_dataframe(dataframe=orders,
entity_id='orders',
index='ORD_ID')

## Adding Order Items Entity
es.entity_from_dataframe(dataframe=order_items,
entity_id='order_items',
index='ORD_ITM_ID')

## Adding Products Entity
es.entity_from_dataframe(dataframe=prods,
entity_id='prods',
index='PROD_ID')

建立关系

customer_relationship = ft.Relationship(es["cust"]["CUST_ID"],
es["orders"]["CUST_ID"])


orderitems_relationship = ft.Relationship(es["orders"]["ORD_ID"],
es["order_items"]["ORD_ID"])


products_relationship = ft.Relationship(es["prods"]["PROD_ID"],
es["order_items"]["PROD_ID"])

### Add Relationships
es = es.add_relationship(customer_relationship)
es = es.add_relationship(orderitems_relationship)
es = es.add_relationship(products_relationship)

生成特征

feature_defs = ft.dfs(entityset=es,
target_entity="cust",
agg_primitives=["count", "sum"],
verbose = True,
features_only = True)
## Show features
feature_defs

输出:

Built 7 features
[<Feature: AGE>,
<Feature: COUNT(order_items)>,
<Feature: SUM(orders.QTY)>,
<Feature: SUM(orders.PRICE)>,
<Feature: SUM(order_items.QTY)>,
<Feature: COUNT(orders)>,
<Feature: SUM(order_items.PRICE)>]

这也应该向我显示产品变量的功能,但事实并非如此。

所以我期望 SUM 能够对每个客户的产品价格进行求和。相反,什么也没有。

最终,我想为有趣的值创建功能。但由于产品变量没有显示,因此添加有趣的值也不起作用。

## Get All Product IDs
interesting_products = es["prods"].df.PROD_ID.unique()

es["prods"]["PROD_ID"].interesting_values=interesting_products


feature_defs = ft.dfs(entityset=es,
target_entity="cust",
agg_primitives=["count", "sum"],
where_primitives=["count", "sum"],
verbose = True,
features_only = True)
## Show features
feature_defs

输出:

Built 7 features
[<Feature: AGE>,
<Feature: COUNT(order_items)>,
<Feature: SUM(orders.QTY)>,
<Feature: SUM(orders.PRICE)>,
<Feature: SUM(order_items.QTY)>,
<Feature: COUNT(orders)>,
<Feature: SUM(order_items.PRICE)>]

希望有人能帮忙:)

最佳答案

产品未显示的原因是因为从它创建的任何特征的深度都是 3。您可以使用 max_depth 控制 ft.dfs 中的深度参数是这样的

feature_defs = ft.dfs(entityset=es,
target_entity="cust",
agg_primitives=["count", "sum"],
verbose = True,
max_depth=3, # add max_depth
features_only = True)

现在返回的功能是

[<Feature: AGE>,
<Feature: SUM(order_items.QTY)>,
<Feature: SUM(order_items.PRICE)>,
<Feature: SUM(orders.PRICE)>,
<Feature: SUM(orders.QTY)>,
<Feature: COUNT(order_items)>,
<Feature: COUNT(orders)>,
<Feature: SUM(order_items.prods.PRICE)>]

您可以在最后使用产品价格看到 SUM(order_items.prods.PRICE)

要使 where 子句发挥作用,请将有趣的值添加到 order_items 实体中。

interesting_products = es["prods"].df.PROD_ID.unique()
es["order_items"]["PROD_ID"].interesting_values=interesting_products
feature_defs = ft.dfs(entityset=es,
target_entity="cust",
agg_primitives=["count", "sum"],
where_primitives=["count", "sum"],
verbose=True,
max_depth=3,
features_only=True)

这会创建 20 个功能,您可以在下面看到

[<Feature: AGE>,
<Feature: SUM(order_items.QTY)>,
<Feature: SUM(order_items.PRICE)>,
<Feature: SUM(orders.PRICE)>,
<Feature: SUM(orders.QTY)>,
<Feature: COUNT(order_items)>,
<Feature: COUNT(orders)>,
<Feature: SUM(order_items.prods.PRICE WHERE PROD_ID = 2)>,
<Feature: SUM(order_items.QTY WHERE PROD_ID = 2)>,
<Feature: SUM(order_items.QTY WHERE PROD_ID = 3)>,
<Feature: SUM(order_items.prods.PRICE)>,
<Feature: COUNT(order_items WHERE PROD_ID = 2)>,
<Feature: SUM(order_items.prods.PRICE WHERE PROD_ID = 1)>,
<Feature: SUM(order_items.PRICE WHERE PROD_ID = 3)>,
<Feature: COUNT(order_items WHERE PROD_ID = 1)>,
<Feature: COUNT(order_items WHERE PROD_ID = 3)>,
<Feature: SUM(order_items.prods.PRICE WHERE PROD_ID = 3)>,
<Feature: SUM(order_items.QTY WHERE PROD_ID = 1)>,
<Feature: SUM(order_items.PRICE WHERE PROD_ID = 2)>,
<Feature: SUM(order_items.PRICE WHERE PROD_ID = 1)>]

关于python - 未在 featuretools 中为我的实体集设置生成功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53067099/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com