Python 具有附加参数的自定义聚合原语?
transform原语可以与其他参数配合使用。这里有一个例子Python 具有附加参数的自定义聚合原语?,python,machine-learning,data-science,feature-engineering,featuretools,Python,Machine Learning,Data Science,Feature Engineering,Featuretools,transform原语可以与其他参数配合使用。这里有一个例子 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()
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'
featuretools是否支持带有附加参数的自定义聚合原语?这里的问题是您的种子功能缺少一个参数。对于聚合原语,需要指定要聚合的实体。在这种情况下,将聚合种子功能的构造更改为
count_the_feat = StringCount(es['transactions']['product_id'], es['sessions'], string="5")
将创建该功能
sessions.STRING_COUNT(product_id, "5")
正如所料。该功能将给出每个会话id显示字符串“5”的频率。如果我还想在agg原语中输入不同的功能,该怎么办?它看起来是这样的:
count\u the_feat=StringCount(es['transactions'['product\u id'],es['sessions'],es['transactions']['amount'],es['sessions'],string=“5”)
我可以在尝试使用多个输入创建agg原语时重现该错误。同时,您可以分别指定每个聚合,然后将它们放在一起(或者,先合并列,然后聚合)。例如,double\u agg=StringCount(es['transactions']['product\u id'],es['sessions'],string=“5”)*Mean(es['transactions']['amount'],es['sessions'])
@Jeff我认为这是一个非常有趣的问题,但是我很难想出一个不能通过组合agg和trans原语来处理的示例。语法StringCount(es['transactions']['product\u id']、es['transactions']['amount']、es['sessions']、string=“5”)
向我暗示,您将“执行f(product\u id
,amount
),然后按会话进行聚合”。然后,我们的目标是找到一个transform原语无法处理的f
。您将如何组合agg和/或trans原语来查找产品ID 1和3上交易金额之间的相关性(例如np.correlate)。感谢您的帮助@SethAh,备份了一个步骤:错误似乎是由于没有将输入变量作为列表传递而导致的。这是make_agg_primitive
和make_trans_primitive
之间的一个细微差别。在添加额外的括号--StringCount([es['transactions']['product_id']、es['transactions']['amount']、es['sessions']、string=“5”)