Skip to main content
Version: 0.5

Creating Feature 5

In this topic, you will create and test the fifth feature, transaction_distance_from_home. The feature returns the user's distance from their home, given their current location.

This feature depends on the user_home_location Feature View, which you already defined. When transaction_distance_from_home is run, the user's home location-- the lat(latitude) and long(longitude) features from user_home_location-- are passed as input.

The transaction_distance_from_home feature definition​

In your local feature repository, open the file features/on_demand_features/transaction_distance_from_home.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 String, Timestamp, Float64, Field
from features.batch_features.user_home_location import user_home_location

request_schema = [
Field("merch_lat", Float64),
Field("merch_long", Float64),
]
request = RequestSource(schema=request_schema)
output_schema = [Field("dist_km", Float64)]


@on_demand_feature_view(
sources=[request, user_home_location],
mode="python",
schema=output_schema,
description="How far a transaction is from the user's home",
)
def transaction_distance_from_home(request, user_home_location):
from math import sin, cos, sqrt, atan2, radians

user_lat = radians(user_home_location["lat"])
user_long = radians(user_home_location["long"])
transaction_lat = radians(request["merch_lat"])
transaction_long = radians(request["merch_long"])

# approximate radius of earth in km
R = 6373.0

dlon = transaction_long - user_long
dlat = transaction_lat - user_lat

a = sin(dlat / 2) ** 2 + cos(user_lat) * cos(transaction_lat) * sin(dlon / 2) ** 2
c = 2 * atan2(sqrt(a), sqrt(1 - a))

distance = R * c
return {"dist_km": distance}

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 Testing the Feature View section, is found in the code that you uncommented in the file above (features/on_demand_features/transaction_distance_from_home.py).

The input to the transformation​

The transformation accepts two inputs:

  • request: The merch_lat (latitude) and merch_long (longitude) of the user's current location.
  • user_home_location: A Feature View that contains the lat (latitude) and long(longitude) of the user's home. This Feature View is defined in /features/batch_features/user_home_location.py.

request is created using request_schema that contains merch_lat and merch_long:

request_schema = [
Field("merch_lat", Float64),
Field("merch_long", Float64),
]
request = RequestSource(schema=request_schema)

The transformation body​

The transformation body calculates the user's current distance from their home location. Note how the variables user_lat, user_long, transaction_lat and transaction_long are set to read values from the function inputs. Understanding how the calculation works is not important.

The output​

The output schema contains one field. This field appears as the feature name when getting training or serving data.

output_schema = [Field('dist_km', Float64)]

Testing the Feature View​

fv = ws.get_feature_view("transaction_distance_from_home")

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

offline_features = fv.run(
user_home_location={"lat": 40.04, "long": 35.77},
request={"merch_lat": 38.85, "merch_long": 36.35},
)
print(offline_features)

Example output:

{'dist_km': 141.42769212941386}

Was this page helpful?

Happy React is loading...