【发布时间】:2021-04-14 15:32:37
【问题描述】:
从 v0.12.0 版本开始,FeatureTools 允许您为多输出原语分配自定义名称:https://github.com/alteryx/featuretools/pull/794。默认情况下,当您定义自定义多输出原语时,生成的特征的列名称会附加[0]、[1]、[2] 等。所以让我们说我有以下代码要输出多输出原语:
def sine_and_cosine_datestamp(column):
"""
Returns the Sin and Cos of the hour of datestamp
"""
sine_hour = np.sin(column.dt.hour)
cosine_hour = np.cos(column.dt.hour)
ret = [sine_hour, cosine_hour]
return ret
Sine_Cosine_Datestamp = make_trans_primitive(function = sine_and_cosine_datestamp,
input_types = [vtypes.Datetime],
return_type = vtypes.Numeric,
number_output_features = 2)
在 DFS 生成的数据框中,生成的两个列的名称将是 SINE_AND_COSINE_DATESTAMP(datestamp)[0] 和 SINE_AND_COSINE_DATESTAMP(datestamp)[1]。实际上,我希望列的名称能够反映对列进行的操作。所以我希望列名类似于SINE_AND_COSINE_DATESTAMP(datestamp)[sine] 和SINE_AND_COSINE_DATESTAMP(datestamp)[cosine]。显然,您必须使用generate_names 方法才能这样做。我在网上找不到任何东西来帮助我使用这种方法,而且我一直遇到错误。例如,当我尝试以下代码时:
def sine_and_cosine_datestamp(column, string = ['sine, cosine']):
"""
Returns the Sin and Cos of the hour of the datestamp
"""
sine_hour = np.sin(column.dt.hour)
cosine_hour = np.cos(column.dt.hour)
ret = [sine_hour, cosine_hour]
return ret
def sine_and_cosine_generate_names(self, base_feature_names):
return u'STRING_COUNT(%s, "%s")' % (base_feature_names[0], self.kwargs['string'])
Sine_Cosine_Datestamp = make_trans_primitive(function = sine_and_cosine_datestamp,
input_types = [vtypes.Datetime],
return_type = vtypes.Numeric,
number_output_features = 2,
description = "For each value in the base feature"
"outputs the sine and cosine of the hour, day, and month.",
cls_attributes = {'generate_names': sine_and_cosine_generate_names})
我收到了一个断言错误。更让我困惑的是,当我进入featuretools/primitives/base文件夹中的transform_primitve_base.py文件时,看到generate_names函数长这样:
def generate_names(self, base_feature_names):
n = self.number_output_features
base_name = self.generate_name(base_feature_names)
return [base_name + "[%s]" % i for i in range(n)]
在上面的函数中,您似乎无法生成自定义原语名称,因为它默认使用base_feature_names 和输出特征的数量。任何帮助将不胜感激。
【问题讨论】:
标签: python primitive featuretools