如何使用自定义变量类型创建列表类型的自定义agg和trans原语函数

时间:2019-08-22 07:47:23

标签: featuretools feature-engineering

关于功能工具的make_agg_primitives函数和make_trans_primitives,我有两个问题。

我的数据中包含由列表格式组成的值。

我想通过使用各种自定义功能来汇总产品列:

例如,

-我的审判-

第一个问题

input data

id products
1 [a, b, c, a]
1 [a, c, c, a]
2 [a, c, c, a]
2 [c,c,c,c,c,c]

output data1 

id products
1 [a, b, c, a, a, c, c, a]
2 [a,c,c,a,c,c,c,c,c,c]

关于第一个问题的代码

def sum_list(values):
return sum(values,[])


max_frequent_list= make_agg_primitive(function = sum_list,
                                  input_types = [List],
                                  return_type = List,
                                  description="sum of a list related instance")

第二个问题

input data

id products
1 [a, b, c, a, c, c, a]
2 [a, c, c, a,c,c,c,c,c,c]

output data
id product
1 a
2 c

关于第二个问题的代码

def max_frequent_list(values):
    def most_common(l):
        try:
            return mode(l)
        except StatisticsError as e:
            # will only return the first element if no unique mode found
            if 'no unique mode' in e.args[0]:
                return random.choice(l)
            # this is for "StatisticsError: no mode for empty data"
            # after calling mode([])
            raise
    return values.apply(lambda x: most_common(x))

max_frequent_list= make_trans_primitive(function = max_frequent_list,
                                  input_types = [List],
                                  return_type = String,
                                  description="common value of a list related instance")

0 个答案:

没有答案