使用自定义变量类型创建列表类型的自定义基元函数

2024-09-29 06:35:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个关于featuretools的make_agg_premitives函数的问题。你知道吗

在我的数据中,有些值是由列表格式组成的。你知道吗

例如

    id       products 
    a         ['a', 'b', 'c']
    b         ['a','c']
    a         ['a','c']

我想使用各种自定义函数聚合products列:

def len_lists(values):
   return len(a)

len_ = make_agg_primitive(function = len_lists,
                           input_types = [?????],
                           return_type = ft.variable_types.Numeric,
                           description="length of a list related instance")

Tags: 数据函数id列表makelenreturndef
2条回答

可以使用featuretools创建自定义变量类型,该类型可与自定义基元一起使用,以生成所需的变换特征。你知道吗

Note: The operation that you want to do is actually a transform primitive, not an aggregation primitive.

使用您的示例,让我们创建一个自定义列表类型

from featuretools.variable_types import Variable

class List(Variable):
    type_string = "list"

现在,让我们使用新的列表类型创建一个自定义转换原语,并为包含列表变量类型的简单entityset生成特性。你知道吗

from featuretools.primitives import make_trans_primitive
from featuretools.variable_types import Numeric
import pandas as pd
import featuretools as ft

def len_list(values):
    return values.str.len()

LengthList = make_trans_primitive(function = len_list,
                                  input_types = [List],
                                  return_type = Numeric,
                                  description="length of a list related instance")

# Create a simple entityset containing list data
data = pd.DataFrame({"id": [1, 2, 3],
                     "products": [ ['a', 'b', 'c'], ['a','c'], ['b'] ]})

es = ft.EntitySet(id="data")
es = es.entity_from_dataframe(entity_id="customers",
                              dataframe=data,
                              index="id",
                              variable_types={
                                   'products': List # Use the custom List type
                              })

feature_matrix, features = ft.dfs(entityset=es,
                                  target_entity="customers",
                                  agg_primitives=[],
                                  trans_primitives=[LengthList],
                                  max_depth=2)

现在可以查看生成的特征,其中包括使用自定义变换原语的特征

feature_matrix.head()
    LEN_LIST(products)
id
1                    3
2                    2
3                    1

谢谢你!多亏了你,我要做各种各样的功能!你知道吗

在代码中 定义长度列表(值): 返回值.str.len()

值的格式是dataframe,对吗?你知道吗

相关问题 更多 >