Skip to main content
Version: 0.5

Creating Feature 3

In this section you will create and test the transaction_amount_is_high feature. The feature returns whether the transaction amount is greater than 100 dollars.

In your local feature repository, open the file features/on_demand_features/transaction_amount_is_high.py. In the file, uncomment the following code, which is a definition of the Feature View.

from tecton import RequestSource, on_demand_feature_view
from tecton.types import Bool, Field, Float64

transaction_request = RequestSource(schema=[Field("amt", Float64)])

output_schema = [Field("transaction_amount_is_high", Bool)]


@on_demand_feature_view(
sources=[transaction_request],
mode="python",
schema=output_schema,
description="Whether the transaction amount is considered high (over $100)",
)
def transaction_amount_is_high(transaction_request):
result = {}
result["transaction_amount_is_high"] = int(transaction_request["amt"] >= 100)
return result

In your terminal, run tecton apply to apply this Feature View to your workspace.

info

The code in the sections below, with the exception of the Test the Feature View section, is found in the code that you uncommented in the file above (features/transaction_amount_is_high.py).

This Feature View is an @on_demand_feature_view, which runs a transformation that uses request data from the application that calls the Feature View. The following code defines the transformation function:

def transaction_amount_is_high(transaction_request):
return {"transaction_amount_is_high": transaction_request["amt"] > 100}

The input to the transformation​

The transaction_request input contains the transaction amount. transaction_request is created using request_schema that contains amt (the amount):

request_schema = [Field("amt", Float64)]
transaction_request = RequestSource(schema=request_schema)

The output from the transformation​

The output schema contains one field:

output_schema = [Field('transaction_amount_is_high', Bool)]

This field appears as the feature name when getting training or serving data.

The transformation body​

The body of the transformation contains only a return statement that determines whether the transaction amount is greater than one-hundred dollars:

return {"transaction_amount_is_high": transaction_request["amt"] > 100}

Test the feature​

In your notebook, get the feature view from the workspace.

fv = ws.get_feature_view("transaction_amount_is_high")

Call the run method of the feature view and pass the amount as input:

offline_features = fv.run(transaction_request={"amt": 50})
print(offline_features)

Sample Output:

{'transaction_amount_is_high': False}

This is the simplest type of On-Demand Feature View, since it's only input is supplied by the calling application. In the next section, you will explore a more complicated On-Demand Feature View.

Was this page helpful?

Happy React is loading...