首页 > 解决方案 > 您如何在 Featuretools 中实现加权和变换原语?

问题描述

我试图弄清楚如何为 Featuretools 实现加权和求和原语。权重应取决于 time_since_last 像

cum_sum (amount) = sum_{i} exp( -a_{i} ) * amount_{i}

i滚动 6 个月的周期在哪里......


在上面你找到原始问题。经过一段时间的尝试和错误,我想出了这个代码来达到我的目的:

从这里使用实体和关系的数据和初始设置

    def weight_time_until(array, time):
        diff = pd.DatetimeIndex(array) - time
        s = np.floor(diff.days/365/0.5)
        aWidth = 9
        a = math.log(0.1) / ( -(aWidth -1) )

        w = np.exp(-a*s) 

        return w

    WeightTimeUntil = make_trans_primitive(function=weight_time_until,
                                     input_types=[Datetime],
                                     return_type=Numeric,
                                     uses_calc_time=True,
                                     description="Calc weight using time until the cutoff time",
                                     name="weight_time_until")


features, feature_names = ft.dfs(entityset = es, target_entity = 'clients', 
                                 agg_primitives = ['sum'],
                                 trans_primitives = [WeightTimeUntil, MultiplyNumeric]) 

当我在上面做时,我接近了我想要的功能,但最后我没有做对,我不明白。所以我得到了功能

总和(贷款。WEIGHT_TIME_UNTIL(贷款开始))

但不是

SUM(loans.loan_amount * loan.WEIGHT_TIME_UNTIL(loan_start))

我在这里错过了什么???


我进一步尝试......

我的猜测是类型错过匹配!但“类型”是相同的。无论如何,我尝试了以下方法:

1) es["loans"].convert_variable_type("loan_amount",ft.variable_types.Numeric) 2) 贷款["loan_amount_"] = 贷款["loan_amount"]*1.0

对于 (1) 以及 (2),我得到了更有希望的结果功能:

贷款金额_ * WEIGHT_TIME_UNTIL(贷款开始)

并且

贷款金额 * WEIGHT_TIME_UNTIL(贷款开始)

但只有当我有目标价值 = 贷款而不是客户时,这实际上不是我的意图。

标签: featuretools

解决方案


这个原语目前不存在。但是,您可以创建自己的自定义原语来完成此计算。

这是一个计算滚动和的示例,可以使用适当的 pandas 或 python 方法对其进行更新以进行加权和

from featuretools.primitives import TransformPrimitive
from featuretools.variable_types import Numeric

class RollingSum(TransformPrimitive):
    """Calculates the rolling sum.

    Description:
        Given a list of values, return the rolling sum.
    """

    name = "rolling_sum"
    input_types = [Numeric]
    return_type = Numeric
    uses_full_entity = True

    def __init__(self, window=1, min_periods=None):
        self.window = window
        self.min_periods = min_periods

    def get_function(self):
        def rolling_sum(values):
            """method is passed a pandas series"""
            return values.rolling(window=self.window, min_periods=self.min_periods).sum()

        return rolling_sum

推荐阅读