You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Featuretools中是否支持带额外参数的自定义聚合基元?

问题解析:Featuretools支持带额外参数的自定义聚合基元吗?

答案是肯定支持!你遇到的TypeError: new_class_init() missing 1 required positional argument: 'parent_entity'报错,本质是聚合基元的使用逻辑和转换基元不同,和额外参数本身无关。

为什么会报错?

聚合基元的核心作用是跨实体聚合数据(比如从子实体transactions聚合到父实体customers),而转换基元只是在同一个实体的列上做转换。所以创建聚合基元特征时,必须明确指定parent_entity(要聚合到哪个父实体)——这是转换基元不需要的参数,也是你代码里缺失的关键部分。

修正后的完整代码

我们基于你的代码调整,补上parent_entity参数,并匹配正确的DFS使用方式:

def string_count(column, string=None):
    ''' ..note:: this is a naive implementation used for clarity '''
    assert string is not None, "string to count needs to be defined"
    counts = [str(element).lower().count(string) for element in column]
    return sum(counts)

def string_count_generate_name(self):
    return u"STRING_COUNT(%s, %s)" % (self.base_features[0].get_name(), '"' + str(self.kwargs['string']) + '"')

StringCount = make_agg_primitive(
    function=string_count,
    input_types=[Categorical],
    return_type=Numeric,
    cls_attributes={ "generate_name": string_count_generate_name })

es = ft.demo.load_mock_customer(return_entityset=True)
# 关键:创建聚合特征时必须指定parent_entity
count_the_feat = StringCount(es['transactions']['product_id'], 
                             parent_entity=es['customers'], 
                             string="5")

# 使用DFS时,目标实体要设为父实体customers,或者包含聚合特征作为seed_features
fm, fd = ft.dfs(
    entityset=es,
    target_entity='customers',
    max_depth=1,
    features_only=False,
    seed_features=[count_the_feat])

预期输出

运行后会得到每个客户对应的product_id中包含"5"的总次数,类似这样:

STRING_COUNT(transactions.product_id, "5")
customer_id                                           
1                                                    1
2                                                    0

核心要点总结

  • 转换基元:仅作用于同一实体的列,无需指定父实体
  • 聚合基元:用于子实体到父实体的跨实体聚合,必须指定parent_entity参数
  • 额外参数(比如你的string)在两种基元中都完全支持,只要在创建特征时正确传入即可

内容的提问来源于stack exchange,提问作者Jeff Hernandez

火山引擎 最新活动