Я пытаюсь создать настраиваемые примитивные функции скользящая сумма с помощью инструментов функций, и ниже приведен код: -
class RollingSumOnDatetime(TransformPrimitive):
"""Calculates the rolling sum on a Datetime time index column.
Description:
Given a list of values and a Datetime time index, return the rolling sum.
"""
name = "rolling_sum_on_datetime"
input_types = [Numeric, DatetimeTimeIndex]
return_type = Numeric
uses_full_entity = True
description_template = "the rolling sum of {} on {}"
def __init__(self, window=None,on=None):
self.window = window
self.on = on
def get_function(self):
def rolling_sum(to_roll, on_column):
"""method is passed a pandas series"""
# create a DataFrame that has the both columns in it
df = pd.DataFrame({to_roll.name: to_roll, on_column.name: on_column})
rolled_df = df.rolling(window=self.window, on=on_column.name).sum()
return rolled_df[to_roll.name]
return rolling_sum
feature_matrix, feature_defs = ft.dfs(
entityset=es,
n_jobs=10,
target_entity = "contracts",
agg_primitives=agg_prim,
trans_primitives=trans_prim,
groupby_trans_primitives=[
RollingSumOnDatetime(window = "5D", on=es["days"]["datetime"])
],
max_depth=2,
drop_contains=["contract_id", "merchant_id"],
)
Первая часть кода - это настраиваемый примитив, а во второй части я вызываю функцию Это дает ошибку:
ValueError: setting an array element with a sequence.






Вам необходимо удалить on=es["days"]["datetime"] при передаче примитива в groupby_trans_primitives. Это не параметр в __init__ из RollingSumOnDatetime, и поэтому не применим.
Вот минимальный воспроизводимый пример:
from featuretools.primitives import AggregationPrimitive, TransformPrimitive
from featuretools.variable_types import Numeric, DatetimeTimeIndex
class RollingSumOnDatetime(TransformPrimitive):
"""Calculates the rolling sum on a Datetime time index column.
Description:
Given a list of values and a Datetime time index, return the rolling sum.
"""
name = "rolling_sum_on_datetime"
input_types = [Numeric, DatetimeTimeIndex]
return_type = Numeric
uses_full_entity = True
description_template = "the rolling sum of {} on {}"
def __init__(self, window=None):
self.window = window
def get_function(self):
def rolling_sum(to_roll, on_column):
"""method is passed a pandas series"""
#create a DataFrame that has the both columns in it
df = pd.DataFrame({to_roll.name:to_roll, on_column.name:on_column})
rolled_df = df.rolling(window=self.window, on=on_column.name).sum()
return rolled_df[to_roll.name]
return rolling_sum
import featuretools as ft
es = ft.demo.load_mock_customer(return_entityset=True)
feature_matrix, feature_defs = ft.dfs(
entityset=es,
target_entity = "transactions",
agg_primitives=[],
trans_primitives=[],
groupby_trans_primitives=[
RollingSumOnDatetime(window = "5D")
]
)
feature_defs
Если распечатать feature_defs, то получим:
[<Feature: session_id>,
<Feature: amount>,
<Feature: product_id>,
<Feature: ROLLING_SUM_ON_DATETIME(amount, transaction_time, window=5D) by product_id>,
<Feature: ROLLING_SUM_ON_DATETIME(amount, transaction_time, window=5D) by session_id>,
<Feature: products.brand>,
<Feature: sessions.customer_id>,
<Feature: sessions.device>,
<Feature: sessions.customers.zip_code>,
<Feature: ROLLING_SUM_ON_DATETIME(amount, sessions.session_start, window=5D) by product_id>,
<Feature: ROLLING_SUM_ON_DATETIME(amount, sessions.session_start, window=5D) by session_id>,
<Feature: ROLLING_SUM_ON_DATETIME(amount, sessions.session_start, window=5D) by sessions.customer_id>]
Пропустил по части в в этом
Пожалуйста, отправьте минимальный воспроизводимый пример.