diff --git a/README.md b/README.md index ff01006..72ca71e 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,24 @@ with predict_features AS ( ) select * from {{ dbt_ml.recommend(ref('model'), 'predict_features') }} +``` + +The ML.DETECT_ANOMALIES function provides anomaly detection for BigQuery ML. + +```sql +# detect_anomalies_model.sql + +{{ + config( + materialized='table' + ) +}} + +with eval_data as ( + ... +) +select * from {{ dbt_ml.detect_anomalies(ref('model'), 'eval_data', threshold) }} ``` ### Tuning hyperparameters diff --git a/macros/detect_anomalies.sql b/macros/detect_anomalies.sql new file mode 100644 index 0000000..61dc691 --- /dev/null +++ b/macros/detect_anomalies.sql @@ -0,0 +1,7 @@ +{% macro detect_anomalies(relation, source, threshold = 0.95) %} + ml.detect_anomalies( + model {{ relation }}, + struct({{ threshold }} as anomaly_prob_threshold), + (select * from {{ source }}) + ) +{% endmacro %} \ No newline at end of file