Skip to navigation

Predictive Queries

View as Markdown

After you have set up the Graph of your Tables, you can define a machine learning problem as a Kumo PredictiveQuery. Predictive queries use the predictive query language (PQL), a concise SQL-like syntax for defining models for new business problems. Refer to the PQL structure guide for details on query string construction.

Writing a Query

This example predicts customer lifetime value as a regression problem. The query finds the maximum quantity of transactions for each customer over the next 30 days, for customers who have made over 15 units worth of transactions in the past 7 days:

pquery = kumo.PredictiveQuery(
graph=graph,
query=(
"PREDICT MAX(transaction.Quantity, 0, 30, days)\n"
"FOR EACH customer.CustomerID\n"
"ASSUMING SUM(transaction.UnitPrice, 0, 7, days) > 15"
),
)
# Validate the predictive query syntax:
pquery.validate(verbose=True)

Validating a Query

The SDK provides quick ways to confirm your query matches expectations before generating data.

  • validate() checks for syntax errors and guides you toward a correct formulation.
  • get_task_type() returns the task type of the query (for example, binary classification or regression) so you can confirm the ML problem matches your intent.
# Confirm the task type:
print(pquery.get_task_type())

Generating a Training Table

After your query is validated, generate a training table to use for model fitting:

# Optionally get a suggested plan (can be customized):
training_table_plan = pquery.suggest_training_table_plan()
# Generate the training table (non_blocking=True schedules it in background):
training_table = pquery.generate_training_table(training_table_plan, non_blocking=False)
# Inspect the generated training data:
print(training_table.data_df().head())

If you do not need a custom plan, omit it and Kumo uses a suitable default.

Generating a Prediction Table

Generate a prediction table using the same approach:

# Optionally get a suggested plan:
prediction_table_plan = pquery.suggest_prediction_table_plan()
# Generate the prediction table:
prediction_table = pquery.generate_prediction_table(prediction_table_plan, non_blocking=False)
# Inspect the generated prediction data:
print(prediction_table.data_df().head())