gpt4 book ai didi

python - 带有附加参数的自定义聚合原语?

转载 作者:太空宇宙 更新时间:2023-11-04 04:41:04 24 4
gpt4 key购买 nike

转换原语可以很好地处理附加参数。这是一个例子

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 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_trans_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)
count_the_feat = StringCount(es['transactions']['product_id'], string="5")
fm, fd = ft.dfs(
entityset=es,
target_entity='transactions',
max_depth=1,
features_only=False,
seed_features=[count_the_feat])

输出:

                product_id  STRING_COUNT(product_id, "5")
transaction_id
1 5 1
2 4 0
3 3 0
4 3 0
5 4 0

但是,如果我像这样修改并制作成聚合原语:

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)
count_the_feat = StringCount(es['transactions']['product_id'], string="5")

我收到以下错误:

TypeError: new_class_init() missing 1 required positional argument: 'parent_entity'

功能工具是否支持带有附加参数的自定义聚合原语?

最佳答案

此处的问题是您的种子功能缺少参数。对于聚合原语,您需要指定要聚合的实体。在这种情况下,将聚合种子特征的构造更改为

count_the_feat = StringCount(es['transactions']['product_id'], es['sessions'], string="5")

将创建特征

sessions.STRING_COUNT(product_id, "5")

如预期。该功能将给出字符串“5”出现在每个 session ID 中的频率。

关于python - 带有附加参数的自定义聚合原语?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50636819/

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