Build your first dataset¶
A dataset is a denormalised view that joins selected features for an entity. It is always a view, never a table, so it stays consistent with the temporal features underneath.
import dataclasses
import getpass
import os
import tdfs4ds # importing it first also loads a .env file, if you have one
import teradataml as tdml
# 1. Open the Teradata session. Host and user come from the environment (TDFS4DS_TD_HOST,
# TDFS4DS_TD_USERNAME), the password from TDFS4DS_TD_PASSWORD or a prompt: nothing secret
# is written on this page.
host = os.environ["TDFS4DS_TD_HOST"]
user = os.environ["TDFS4DS_TD_USERNAME"]
password = os.environ.get("TDFS4DS_TD_PASSWORD") or getpass.getpass("Teradata password: ")
tdml.create_context(host=host, username=user, password=password)
# 2. Pick your industry. This is the ONLY line to change: retail, banking, healthcare, pharma,
# manufacturing, assembly, energy or transportation. Every name below comes from S.
S = tdfs4ds.tutorial_scenario("retail")
# 3. Connect to the feature store (created when missing) and choose a data domain. disposable=True
# records that this domain is an experiment, so the last page removes it in one call.
DATABASE = os.environ.get("TDFS4DS_TD_DATABASE") or user
tdfs4ds.connect(database=DATABASE, create_if_missing=True)
tdfs4ds.create_data_domain(f"TUTORIAL_{S.name.upper()}", disposable=True) # an experiment: removable in one call
# 4. The tutorial data: three tables written once; a second run keeps them.
try:
S = tdfs4ds.create_tutorial_data(S.name, DATABASE)
except ValueError:
S = dataclasses.replace(S, database=DATABASE)
def register_entity_features():
"""Page 1 in one call: (re)register the entity features and return the process id."""
sql = f"""
SELECT {S.entity}, COUNT(*) AS nb_events, SUM({S.amount}) AS total_amount,
AVG({S.amount}) AS avg_amount, COUNT(DISTINCT {S.partner}) AS nb_partners
FROM {S.table('events')}
GROUP BY 1"""
return tdfs4ds.upload_features(
df=None, sql_query=sql, target_view_name=f"{S.database}.{S.name}_entity_features",
entity_id=[S.entity], feature_names=["nb_events", "total_amount", "avg_amount", "nb_partners"],
replace=True, document=False, optimize_query=False, return_process_id=True)
You should see the same few log lines as in First feature. A dataset needs features: register them again if this is a fresh session (the call is idempotent).
You should see a process id.
1 · Discover what is available¶
Output with scenario='retail' (the order may differ): the four features under the entity column.
ENTITY_NAME FEATURE_NAME
0 CustomerID avg_amount
1 CustomerID nb_partners
2 CustomerID nb_events
3 CustomerID total_amount
2 · Build the view¶
Each feature here is computed by one process, so the feature names alone are enough:
dataset = tdfs4ds.build_dataset(
entity_id=[S.entity],
selected_features=["nb_events", "total_amount", "avg_amount", "nb_partners"],
view_name=f"{S.database}.{S.name}_entity_dataset",
comment="Entity features, current state",
)
print(dataset.shape)
print(dataset.head(3))
Output with scenario='retail': one row per customer that has events, five columns (the entity id and the four features).
(1979, 5)
CustomerID total_amount avg_amount nb_events nb_partners
0 100940 8177.49 355.543043 23 20
1 100002 6874.99 80.882235 85 47
2 100471 4612.69 98.142340 47 36
build_dataset creates the view, registers it in the
dataset catalog and returns a teradataml DataFrame you can query at once.
If a feature is computed by several processes (several versions), build_dataset stops
and lists them; pick one with {'nb_events': process_id}. To look the versions up:
Output: a dictionary {feature name: process id}.
3 · Reuse without rebuilding¶
The view is permanent; reattach to it in any later session:
You should see the same shape as in step 2.
Inspect before you build¶
return_query=True returns the generated SQL without creating the view or touching the
dataset catalog:
sql = tdfs4ds.build_dataset(
entity_id=[S.entity],
selected_features=["nb_events", "total_amount"],
view_name=f"{S.database}.{S.name}_entity_dataset_preview",
return_query=True,
)
print(sql)
You should see a REPLACE VIEW statement with one sub-query per feature.
Grouped query strategy¶
grouped=True collapses the features of one storage table into a single
MAX(CASE WHEN) ... GROUP BY sub-query. It pays for wide feature sets (20+ features) by
shortening the SQL the optimizer parses:
grouped = tdfs4ds.build_dataset(
entity_id=[S.entity],
selected_features=["nb_events", "total_amount", "avg_amount", "nb_partners"],
view_name=f"{S.database}.{S.name}_entity_dataset_grouped",
grouped=True,
)
print(grouped.shape)
You should see the same rows and values as the first dataset.
Point-in-time snapshots¶
read_dataset(..., as_of=) reads a dataset at a date: the values valid then, and no row
for an entity that had no value yet. Here every value was computed today, so a date in
2023 returns nothing; Tutorial 7 builds a history where
it matters.
june = tdfs4ds.read_dataset(f"{S.database}.{S.name}_entity_dataset", as_of="2023-06-30", as_pandas=True)
print(june.shape)
Output: no rows, because the values only exist from the moment they were stored.
Clean up¶
tdfs4ds.drop_dataset(...) removes a dataset (the dataset ids are in
Catalogs and datasets); the
last tutorial page lists everything the tutorials
create.
What's next¶
- Document the dataset with an LLM: AI / Dataset documentation.
- Wire it into the consumer agent so business users can ask about it: AI / Consumer agent.
- See which source tables feed it: Lineage / Dependency graph.