【问题标题】:How to implement custom naming for multioutput primitives in FeatureTools如何在 FeatureTools 中为多输出基元实现自定义命名
【发布时间】: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


    【解决方案1】:

    感谢您的提问!此功能没有得到很好的记录。

    您的代码的主要问题是string_count_generate_name 应该返回一个字符串列表,每列一个。

    看起来您正在改编文档中的 StringCount 示例——我认为对于这个原语,始终使用“正弦”和“余弦”作为自定义名称并删除可选来自sine_and_cosine_datestampstring 参数。我还更新了功能名称文本以匹配您想要的文本。

    这些变化之后:

    def sine_and_cosine_datestamp(column):
        """
        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):
        template = 'SINE_AND_COSINE_DATESTAMP(%s)[%s]'
        return [template % (base_feature_names[0], string) for string in ['sine', 'cosine']]
    

    这会创建像 SINE_AND_COSINE_DATESTAMP(order_date)[sine] 这样的特征列名称。无需对实际的 make_trans_primitive 调用进行任何更改。

    在上面的函数中,您似乎无法生成自定义原始名称,因为它默认使用 base_feature_names 和输出特征的数量。

    这是转换原语的默认generate_names 函数。由于我们将此自定义生成名称函数分配给 Sine_Cosine_Datestamp ,因此不会使用默认值。

    希望对您有所帮助,如果您仍有疑问,请告诉我!

    【讨论】:

    • 啊,是的,哈哈,你注意到我确实改编了StringCount 的例子。也感谢您如此清晰易懂地回答我的问题。我花了很多时间梳理 Featuretools 中的 .py 文件。再次感谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 2016-05-26
    • 2012-01-22
    • 2010-11-05
    相关资源
    最近更新 更多