import pandas as pd,numpy as np
import tsfresh
from tsfresh.feature_extraction.feature_calculators import set_property

@set_property("fctype", "simple")
def zero_value_percentage(x):
    ratio = (x==0).sum()/x.shape[0]
    return ratio`

fc_parameters = {'mean':None, 'standard_deviation':None,  'zero_value_percentage':None}

df_features = tsfresh.extract_features(data[['id','year_month','order_qty']], column_id='id',                column_sort='year_month', default_fc_parameters=fc_parameters)    

Error: 'module 'tsfresh.feature_extraction.feature_calculators' has no attribute 'zero_value_percentage''

Utility of custom function is to count no. of non zero points in timeseries as a %.

The documentation mentions to "add the feature calculator to the tsfresh.feature_extraction.feature_calculators submodule" at the end of Step 2 in the link for it to work.

How does one do that?

Dont know how to proceed

2

There are 2 best solutions below

0
On

Step 1: Define the Custom Feature Calculator Function First, define your custom feature calculator function, which calculates the zero value percentage. You've already done this with the zero_value_percentage function.

def zero_value_percentage(x):
ratio = (x == 0).sum() / x.shape[0]
return ratio

Step 2: Register the Custom Feature Calculator To make your custom function available as a feature calculator in the tsfresh library, you need to register it with the feature_calculators submodule.

from tsfresh.feature_extraction import feature_calculators
feature_calculators.register_function(function=zero_value_percentage,function_name="zero_value_percentage", fctype="simple")

Step 3: Extract Features Using tsfresh Now that you have registered your custom feature calculator, you can use it with the tsfresh.extract_features function.

import pandas as pd
import tsfresh
df_features = tsfresh.extract_features(
 data[['id', 'year_month', 'order_qty']],
 column_id='id',
 column_sort='year_month',
 default_fc_parameters=fc_parameters
)

Note: Make sure that you have installed the latest version of tsfresh to access the register_function method.

0
On

you should use tsfresh custom features like in https://github.com/blue-yonder/tsfresh/issues/482#issuecomment-667203955

from tsfresh.feature_extraction.feature_calculators import set_property

@set_property("fctype", "simple")
def your_feature_calculator(x):
    """
    The description of your feature

    :param x: the time series to calculate the feature of
    :type x: pandas.Series
    :return: the value of this feature
    :return type: bool, int or float
    """
    # Calculation of feature as float, int or bool
    result = x[0]
    return result
setattr(tsfresh.feature_extraction.feature_calculators, 'your_feature_calculator', your_feature_calculator)

# custom_functions = [value_count_all, last, first, is_measured, mode, count]
# for func in custom_functions:
#     setattr(feature_calculators, func.__name__, func)

df_ts, y = load_robot_execution_failures()

params = {'your_feature_calculator':None}
my = extract_features(df_ts, column_id='id', column_sort='time', default_fc_parameters=params)
print(my)