A feature function is a function that computes one or more feature values from input data.
It is possible to write a notebook that computes features that are stored in feature groups. However, by factoring your feature computations into functions, they can be independently tested, included in both a feature pipeline and an online inference pipeline (if they compute on-demand features), reused in different feature pipelines, and independently developed.
In a customer churn prediction model, a feature function might be created to calculate the average amount of time between a customer's purchases. This feature could be useful in predicting whether a customer is likely to churn, as customers who make more frequent purchases are less likely to churn. The feature function would take as input the customer's purchase history and return the average time between purchases as a feature value. The example is taken from the Hamilton micro-framework that helps structure and execute features as feature functions.
In the above code snippet, the feature function takes as input a purchase history, which is a list of tuples containing the timestamp and amount of each purchase. The function first converts the purchase history to a pandas dataframe, then calculates the time between each purchase and the average time between purchases. The average time between purchases is returned as a feature value that can be used in a machine learning model.