【问题标题】:How do you implement a weighted sum transform primitive in Featuretools?您如何在 Featuretools 中实现加权和变换原语?
【发布时间】:2019-11-06 12:01:48
【问题描述】:

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

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

i 的滚动周期为 6 个月......


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

使用来自here的实体和关系的数据和初始设置

    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]) 

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

SUM(loans.WEIGHT_TIME_UNTIL(loan_start))

但不是

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),我得到了更有希望的结果功能:

loan_amount_ * WEIGHT_TIME_UNTIL(loan_start)

还有

loan_amount * WEIGHT_TIME_UNTIL(loan_start)

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

【问题讨论】:

  • 嗨,你能把你的跟进作为一个新问题发布吗?另外,你能分享你获得的功能的完整输出吗?
  • ok max 我使用的数据你有吧?!

标签: featuretools


【解决方案1】:

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

这是一个计算滚动和的示例,可以使用适当的 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

【讨论】:

  • 感谢您分享代码。实际上,当我越来越习惯功能工具时,我发现我的问题并没有针对我的目标功能。但不管怎样,上面的类就是make_trans_primitive 的作用?
  • 是的,它类似于make_trans_primitive,但使用类方法更容易定义windowmin_periods 等参数
猜你喜欢
  • 2019-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
相关资源
最近更新 更多