bigquery-data-science 0.7.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bigquery_data_science/__init__.py +58 -0
- bigquery_data_science/_skills/PLANS.md +76 -0
- bigquery_data_science/_skills/bigquery-ai/SKILL.md +66 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_agg.md +528 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_classify.md +450 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_count_tokens.md +180 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_detect_anomalies.md +322 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_embed.md +425 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_evaluate.md +368 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_forecast.md +437 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_generate.md +693 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_generate_bool.md +248 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_generate_double.md +254 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_generate_embedding.md +402 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_generate_int.md +252 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_generate_table.md +447 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_generate_text.md +356 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_if.md +373 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_key_drivers.md +331 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_parse_document.md +416 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_predict.md +559 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_score.md +310 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_search.md +465 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ai_similarity.md +371 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/catalog_search.md +951 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/content_analysis.md +284 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/content_moderation.md +369 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/data_enrichment.md +426 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/document_intelligence.md +470 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/document_rag.md +453 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/image_deduplication.md +770 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/log_analysis.md +668 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/metric_diagnostics.md +568 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ml_annotate_image.md +680 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ml_generate_embedding.md +217 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ml_generate_text.md +223 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ml_process_document.md +392 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ml_transcribe.md +815 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ml_translate.md +600 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/ml_understand_text.md +673 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/multimodal_analysis.md +266 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/rag_pipeline.md +593 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/semantic_search.md +510 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/tabular_prediction.md +485 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/time_series_intelligence.md +489 -0
- bigquery_data_science/_skills/bigquery-ai/narrative/vector_search.md +672 -0
- bigquery_data_science/_skills/bigquery-ai/reference/augmented-analytics.md +77 -0
- bigquery_data_science/_skills/bigquery-ai/reference/cloud-ai-service-models.md +88 -0
- bigquery_data_science/_skills/bigquery-ai/reference/document-processing.md +95 -0
- bigquery_data_science/_skills/bigquery-ai/reference/embeddings-and-search.md +166 -0
- bigquery_data_science/_skills/bigquery-ai/reference/general-purpose-generation.md +87 -0
- bigquery_data_science/_skills/bigquery-ai/reference/managed-functions.md +72 -0
- bigquery_data_science/_skills/bigquery-ai/reference/predictive-ai.md +113 -0
- bigquery_data_science/_skills/bigquery-ai/reference/workflows.md +56 -0
- bigquery_data_science/_skills/bigquery-ai/skill.manifest.json +109 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/INDEX.md +71 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aiagg.md +67 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aicausal_effect-covered-in-bigquery-ml.md +17 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aiclassify.md +72 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aicount_tokens.md +53 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aidetect_anomalies.md +63 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aiembed.md +85 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aievaluate.md +114 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aiforecast.md +123 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aigenerate.md +69 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aigenerate_bool.md +53 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aigenerate_double.md +45 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aigenerate_embedding.md +108 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aigenerate_int.md +45 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aigenerate_table.md +64 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aigenerate_text.md +167 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aiif.md +56 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aikey_drivers.md +76 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aiparse_document.md +91 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aipredict.md +74 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aiscore.md +52 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aisearch.md +66 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/aisimilarity.md +79 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/choosing-between-these-and-the-ai-functions.md +11 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/hybrid-search-capability.md +165 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/mlannotate_image.md +24 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/mlgenerate_embedding.md +34 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/mlgenerate_text.md +65 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/mlprocess_document.md +125 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/mltranscribe.md +26 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/mltranslate.md +27 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/mlunderstand_text.md +32 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/multimodal-input-patterns-by-function.md +128 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/object-tables.md +109 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/objectref-and-objectrefruntime-schema-reference.md +50 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/objfetch_metadata.md +49 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/objget_access_url.md +66 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/objget_read_url.md +40 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/objlist.md +34 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/objmake_ref.md +58 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/per-row-failures-and-the-retry-pattern.md +19 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/setup-apis-connection-roles.md +39 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/two-input-shapes.md +22 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/vector_search.md +78 -0
- bigquery_data_science/_skills/bigquery-ai/syntax/what-the-five-have-in-common.md +27 -0
- bigquery_data_science/_skills/bigquery-data-science/SKILL.md +69 -0
- bigquery_data_science/_skills/bigquery-data-science/reference/namespace-matrix.md +112 -0
- bigquery_data_science/_skills/bigquery-data-science/skill.manifest.json +13 -0
- bigquery_data_science/_skills/bigquery-ml/SKILL.md +70 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/airflow_with_kfp.md +379 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/anomaly_fraud_detection.md +195 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/arima_plus.md +635 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/arima_plus_xreg.md +453 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/autoencoder.md +470 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/automl_classifier.md +291 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/automl_regressor.md +274 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/boosted_tree_classifier.md +449 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/boosted_tree_regressor.md +400 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/bucketizing.md +201 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/causal_effect.md +773 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/churn_retention.md +271 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/cloud_workflows.md +370 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/composer_airflow.md +475 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/contribution_analysis.md +346 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/cross_validation.md +185 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/customer_segmentation.md +233 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/dataform.md +372 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/dbt.md +283 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/difference_in_differences.md +280 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/distance.md +195 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/dnn_classifier.md +326 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/dnn_regressor.md +308 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/embeddings_classification.md +426 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/encoding.md +238 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/ensembling.md +278 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/evaluation.md +445 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/exploration.md +779 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/export.md +321 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/feature_engineering.md +280 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/feature_store.md +652 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/feature_store_workflow.md +426 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/ga4_churn_prediction.md +285 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/hierarchical_forecasting.md +441 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/image.md +791 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/imported.md +426 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/kmeans.md +348 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/linear_regression.md +372 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/logistic_regression.md +390 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/matrix_factorization.md +404 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/model_monitoring.md +609 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/pca.md +357 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/price_elasticity_dml.md +286 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/propensity_score_matching.md +350 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/random_forest_classifier.md +445 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/random_forest_regressor.md +409 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/recommendation.md +281 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/regression_based_forecasting.md +491 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/remote.md +428 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/scalers.md +325 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/scheduled_queries.md +284 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/sql_scripting.md +243 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/survival_analysis.md +363 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/synthetic_control.md +221 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/text.md +215 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/time_series.md +669 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/transform_only.md +273 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/uplift_cate.md +241 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/vertex_kfp.md +360 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/wide_and_deep_classifier.md +316 -0
- bigquery_data_science/_skills/bigquery-ml/narrative/wide_and_deep_regressor.md +298 -0
- bigquery_data_science/_skills/bigquery-ml/reference/classification.md +80 -0
- bigquery_data_science/_skills/bigquery-ml/reference/model-management.md +90 -0
- bigquery_data_science/_skills/bigquery-ml/reference/preprocessing-functions.md +234 -0
- bigquery_data_science/_skills/bigquery-ml/reference/regression.md +79 -0
- bigquery_data_science/_skills/bigquery-ml/reference/unsupervised-and-specialized.md +101 -0
- bigquery_data_science/_skills/bigquery-ml/reference/workflows-and-pipelines.md +111 -0
- bigquery_data_science/_skills/bigquery-ml/skill.manifest.json +177 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/1-model_type-catalog.md +28 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/2-task-mlevaluate-metric-columns.md +26 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/3-capability-matrix-per-model-type.md +21 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/4-explainability-weights-functions.md +14 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/5-connection-matrix.md +11 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/6-which-model-free-function-no-create-model-no-connection.md +15 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/7-point-in-time-feature-retrieval-which-of-the-two.md +11 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/INDEX.md +111 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/aicausal_effect.md +85 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/arima_plus-forecasting-lifecycle-functions.md +14 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/arima_plus-univariate-time-series-forecasting.md +132 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/arima_plus_xreg-multivariate-time-series-with-external-regressors.md +103 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/autoencoder.md +102 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/automl_classifier-automl_regressor.md +88 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/boosted_tree_classifier-boosted_tree_regressor.md +117 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/contribution_analysis.md +113 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/dnn_classifier-dnn_regressor.md +107 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/dnn_linear_combined_classifier-dnn_linear_combined_regressor-wide-and-deep.md +98 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/explainability-functions-mlexplain_predict-mlglobal_explain-mlfeature_importance.md +7 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/exploratory-data-analysis-mldescribe_data-mlcorrelation.md +11 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/export-model.md +92 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/feature-engineering-standalone-preprocessing-functions.md +13 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/image-preprocessing-functions-mldecode_image-mlresize_image-mlconvert_image_type-mlconvert_color_space.md +63 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/imported-models-tensorflow-tensorflow_lite-onnx-xgboost.md +36 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/kmeans.md +87 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/linear_reg-logistic_reg-generalized-linear-models.md +101 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/matrix_factorization.md +162 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mladvanced_weights.md +76 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlarima_coefficients.md +30 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlarima_evaluate.md +49 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlbag_of_words.md +47 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlbucketize.md +47 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlcentroids.md +46 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlconfusion_matrix.md +56 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlconvert_color_space.md +53 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlconvert_image_type.md +39 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlcorrelation.md +78 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mldecode_image.md +44 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mldescribe_data.md +74 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mldetect_anomalies.md +86 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mldistance.md +48 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlentity_features_at_time.md +56 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlevaluate.md +72 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlexplain_forecast.md +50 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlexplain_predict.md +66 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlfeature_cross.md +46 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlfeature_importance.md +47 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlfeature_info.md +50 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlfeatures_at_time.md +51 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlforecast.md +76 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlgenerate_embedding-from-a-bqml-pca-autoencoder-matrix_factorization-model.md +64 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlglobal_explain.md +63 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlhash_bucketize.md +47 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlholiday_info.md +46 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlimputer.md +46 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mllp_norm.md +47 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlmetrics.md +80 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlngrams.md +37 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlone_hot_encoder-mlmulti_hot_encoder-mllabel_encoder.md +67 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlpolynomial_expand.md +47 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlpredict.md +67 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlprincipal_component_info.md +42 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlprincipal_components.md +40 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlquantile_bucketize.md +43 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlrecommend.md +60 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlreconstruction_loss.md +48 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlresize_image.md +45 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlroc_curve.md +63 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mltf_idf.md +37 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mltfdv_describe.md +34 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mltfdv_validate.md +48 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mltraining_info.md +78 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mltransform-applying-a-saved-transform.md +52 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mltranspose-not-a-function-pointer.md +32 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mltrial_info.md +60 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlvalidate_data_drift.md +99 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlvalidate_data_skew.md +50 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/mlweights.md +84 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/model-free-time-series-decomposition-mltrend-mlseasonality-mldetect_change_points.md +100 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/model-introspection-mlfeature_info-mltraining_info-mltrial_info.md +7 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/model-monitoring-data-validation.md +23 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/numerical-scalers-mlstandard_scaler-mlmin_max_scaler-mlmax_abs_scaler-mlrobust_scaler-mlnormalizer.md +77 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/onnx.md +55 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/pca.md +91 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/point-in-time-feature-retrieval-mlfeatures_at_time-mlentity_features_at_time.md +25 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/quick-comparison.md +14 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/random_forest_classifier-random_forest_regressor.md +123 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/remote-remote-model-over-a-vertex-ai-endpoint-the-mechanism.md +87 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/tensorflow.md +48 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/tensorflow_lite.md +29 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/text-preprocessing-functions-mlngrams-mltf_idf-mlbag_of_words.md +18 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/timesfm-built-in-foundation-forecaster-see-bigquery-ai.md +28 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/transform_only.md +91 -0
- bigquery_data_science/_skills/bigquery-ml/syntax/xgboost.md +72 -0
- bigquery_data_science/_skills/pinned-models.json +28 -0
- bigquery_data_science/catalog.py +1107 -0
- bigquery_data_science/cli.py +786 -0
- bigquery_data_science/installer.py +282 -0
- bigquery_data_science/mcp_server.py +317 -0
- bigquery_data_science/tooling/__init__.py +15 -0
- bigquery_data_science/tooling/convert_notebook.py +176 -0
- bigquery_data_science/tooling/link_policy.py +247 -0
- bigquery_data_science/tooling/manifest.py +152 -0
- bigquery_data_science/tooling/markdown_anchors.py +84 -0
- bigquery_data_science/tooling/narrative_drift.py +163 -0
- bigquery_data_science/tooling/pinned_models.py +216 -0
- bigquery_data_science/tooling/reference_structure.py +85 -0
- bigquery_data_science/tooling/sync_syntax.py +407 -0
- bigquery_data_science/tooling/validate.py +208 -0
- bigquery_data_science-0.7.0.dist-info/METADATA +306 -0
- bigquery_data_science-0.7.0.dist-info/RECORD +286 -0
- bigquery_data_science-0.7.0.dist-info/WHEEL +4 -0
- bigquery_data_science-0.7.0.dist-info/entry_points.txt +5 -0
- bigquery_data_science-0.7.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
> **Source (`statmike/bigquery-data-science`):** **Notebook:** [`bigquery-ml/workflows/regression_based_forecasting/regression_based_forecasting.ipynb`](https://github.com/statmike/bigquery-data-science/blob/main/bigquery-ml/workflows/regression_based_forecasting/regression_based_forecasting.ipynb)
|
|
2
|
+
|
|
3
|
+
# Regression-Based Forecasting — BigQuery ML
|
|
4
|
+
|
|
5
|
+
Forecast daily bike-share demand using general-purpose regression models — `LINEAR_REG` and `BOOSTED_TREE_REGRESSOR`, both already built in this project's `models/` folder — instead of a native time-series model. Regression models have no built-in notion of time, so this workflow's real content is **feature engineering**: turning a single `(date, num_trips)` series into a training table via time-part extraction, lag features, and lead-based labels for direct multi-step forecasting.
|
|
6
|
+
|
|
7
|
+
**Models used:** `LINEAR_REG`, `BOOSTED_TREE_REGRESSOR`
|
|
8
|
+
**Functions used:** `ML.EVALUATE`, `ML.PREDICT`
|
|
9
|
+
|
|
10
|
+
**First workflow in the project** — composes model types from Phase 2 into an end-to-end forecasting technique, on the exact same station (Pershing Square North) and TEST window (last 28 days, held out after `2018-05-03`) as `models/arima_plus` (`models/arima_plus/`), so the two notebooks' forecast accuracy is directly comparable.
|
|
11
|
+
|
|
12
|
+
Four techniques, in increasing sophistication:
|
|
13
|
+
1. **Time features only** (`LINEAR_REG`) — day-of-week/month/year seasonality, no memory of recent demand.
|
|
14
|
+
2. **+ lag features** (`LINEAR_REG`) — adds 1-day/1-week/1-month/1-quarter/1-year lags of the target itself. Raises a real evaluation trap: a naive test-set evaluation *leaks* future information through those lags. Compares leaked vs. truncated vs. a properly recursive evaluation.
|
|
15
|
+
3. **Direct multi-step** (`LINEAR_REG`) — one model per horizon day (28 models), each trained to predict exactly *h* days ahead directly from lag/time features at the forecast origin. No recursion needed.
|
|
16
|
+
4. **Direct multi-step** (`BOOSTED_TREE_REGRESSOR`) — same 28-model structure, gradient-boosted trees instead of a linear model.
|
|
17
|
+
|
|
18
|
+
**A real, measured GOTCHA drives how Step 4 is built:** pre-validating this notebook found `BOOSTED_TREE_REGRESSOR` takes ~2.5–4.5 minutes to train per model in BigQuery ML — regardless of data size (this dataset has fewer than 1,000 training rows) and regardless of a BigQuery Editions slot reservation (tested directly: a reservation made no measurable difference, confirming the cost is inherent to this model type's training path, not slot-queueing). Training 28 of these sequentially would take 1.5–2 hours. Section 4 instead submits training jobs **concurrently** (confirmed live: BigQuery runs independently-named `CREATE MODEL` jobs from the same client in true parallel, not queued) in batches, cutting wall time from hours to tens of minutes.
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
## Setup
|
|
22
|
+
|
|
23
|
+
Set your project and location, authenticate, and create a shared dataset for the model.
|
|
24
|
+
|
|
25
|
+
> This workflow trains ordinary regression models on engineered features — no connection or remote model required, same as any other `LINEAR_REG`/`BOOSTED_TREE_REGRESSOR` notebook in this project. See the `setup` (Setup Reference) for details.
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
PROJECT_ID = 'statmike-mlops-349915' # <-- Replace with your project ID
|
|
29
|
+
LOCATION = 'US' # BigQuery dataset location
|
|
30
|
+
DATASET_ID = 'bq_ml' # Shared dataset across all bigquery-ml notebooks
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Environment
|
|
34
|
+
|
|
35
|
+
> **Already set up the project environment?** The cell below is a no-op — packages are already in your kernel. See the `setup` (Setup Reference) for details.
|
|
36
|
+
>
|
|
37
|
+
> **Running standalone** (Colab, Colab Enterprise, Vertex AI Workbench)? The cell below installs required packages into your current kernel.
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from google.cloud import bigquery
|
|
41
|
+
import pandas as pd
|
|
42
|
+
import matplotlib.pyplot as plt
|
|
43
|
+
|
|
44
|
+
client = bigquery.Client(project=PROJECT_ID)
|
|
45
|
+
pd.set_option('display.max_colwidth', None)
|
|
46
|
+
|
|
47
|
+
# Create the shared dataset (idempotent)
|
|
48
|
+
dataset_ref = bigquery.DatasetReference(PROJECT_ID, DATASET_ID)
|
|
49
|
+
dataset = bigquery.Dataset(dataset_ref)
|
|
50
|
+
dataset.location = LOCATION
|
|
51
|
+
client.create_dataset(dataset, exists_ok=True)
|
|
52
|
+
print(f'Dataset {PROJECT_ID}.{DATASET_ID} ready')
|
|
53
|
+
|
|
54
|
+
# Register %%bigquery cell magic (auto-loaded in Colab, needed elsewhere)
|
|
55
|
+
%load_ext bigquery_magics
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Materialize the training data
|
|
59
|
+
|
|
60
|
+
Daily trip counts for a single station (Pershing Square North), with a `splits` column marking the last 28 days as `TEST` — the identical station and split point used in `models/arima_plus/`, so the two notebooks' accuracy is directly comparable.
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
query = f"""
|
|
64
|
+
CREATE OR REPLACE TABLE `{PROJECT_ID}.{DATASET_ID}.regression_forecasting_trips` AS
|
|
65
|
+
SELECT
|
|
66
|
+
DATE(starttime) AS date,
|
|
67
|
+
COUNT(*) AS num_trips
|
|
68
|
+
FROM `bigquery-public-data.new_york_citibike.citibike_trips`
|
|
69
|
+
WHERE start_station_name = 'Pershing Square North'
|
|
70
|
+
GROUP BY date
|
|
71
|
+
"""
|
|
72
|
+
client.query(query).result()
|
|
73
|
+
|
|
74
|
+
query = f"""
|
|
75
|
+
SELECT COUNT(*) AS n_rows, MIN(date) AS min_date, MAX(date) AS max_date
|
|
76
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.regression_forecasting_trips`
|
|
77
|
+
"""
|
|
78
|
+
client.query(query).to_dataframe()
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### GOTCHA (verified): `LAG`/`LEAD` operate on row position, not calendar time
|
|
82
|
+
|
|
83
|
+
This series has real gaps — the row count above is fewer than the number of calendar days between its min and max date. `models/arima_plus/` found (and documented) a genuine ~6-month gap (October 2016 – March 2017) in this same public dataset, which `ARIMA_PLUS` handles explicitly (interpolating or nulling across the gap by calendar date). Plain SQL window functions do **not** do this: `LAG(num_trips, 1) OVER (ORDER BY date)` returns the previous *row*, not the previous *calendar day* — during a gap, a "1-day lag" silently becomes a lag of however many calendar days the gap actually spans. Verified directly below: the single largest jump between consecutive rows in this table is exactly the same ~183-day gap.
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
query = f"""
|
|
87
|
+
WITH gaps AS (
|
|
88
|
+
SELECT date, DATE_DIFF(date, LAG(date) OVER (ORDER BY date), DAY) AS days_since_prior_row
|
|
89
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.regression_forecasting_trips`
|
|
90
|
+
)
|
|
91
|
+
SELECT date AS gap_end_date, days_since_prior_row
|
|
92
|
+
FROM gaps
|
|
93
|
+
ORDER BY days_since_prior_row DESC
|
|
94
|
+
LIMIT 3
|
|
95
|
+
"""
|
|
96
|
+
client.query(query).to_dataframe()
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
This TRAIN-region gap doesn't affect the TEST-period forecasts evaluated below (the 28-day TEST window itself has no gaps), but it's a real property of every lag feature built in Examples 2–4: a handful of TRAIN rows have lag values that are actually many months stale, not truly "1 day ago." Regression models have no visibility into this — unlike `ARIMA_PLUS`, nothing here flags or corrects for it.
|
|
100
|
+
|
|
101
|
+
The two failure modes are opposites, and it's worth knowing both. Plain `LAG` misaligns **loudly**: the lag value is real data, just from the wrong date, and the row count versus the calendar span gives it away (as measured above). The model-free time-series functions in `functions/time_series` (`functions/time_series/`) — `ML.TREND`, `ML.SEASONALITY`, `ML.DETECT_CHANGE_POINTS` — misalign **quietly**: each gap-fills the series to its own calendar span before computing, so the dates always line up, but the values spanning a gap are manufactured rather than observed. Neither is wrong; they fail in ways you check for differently. If you want a calendar-aware view of this series' trend and seasonality without training a model, those functions are the tool — just read anything inside the October 2016 – March 2017 window as interpolation, not observation.
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
## Example 1 — Time features only (`LINEAR_REG`)
|
|
105
|
+
|
|
106
|
+
Decompose the date into calendar parts (year, month, day-of-month, day-of-year, day-of-week, weekend flag) and let a linear regression learn seasonality from those alone — no memory of recent demand at all.
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
EX1_CTE = f"""
|
|
110
|
+
WITH prepped AS (
|
|
111
|
+
SELECT
|
|
112
|
+
date,
|
|
113
|
+
num_trips,
|
|
114
|
+
IF(date > DATE('2018-05-03'), 'TEST', 'TRAIN') AS splits,
|
|
115
|
+
EXTRACT(YEAR FROM date) AS year,
|
|
116
|
+
EXTRACT(MONTH FROM date) AS month,
|
|
117
|
+
EXTRACT(DAY FROM date) AS day_of_month,
|
|
118
|
+
EXTRACT(DAYOFYEAR FROM date) AS day_of_year,
|
|
119
|
+
EXTRACT(DAYOFWEEK FROM date) AS day_of_week,
|
|
120
|
+
IF(EXTRACT(DAYOFWEEK FROM date) IN (1, 7), 1, 0) AS weekend
|
|
121
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.regression_forecasting_trips`
|
|
122
|
+
)
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
query = f"""
|
|
126
|
+
CREATE OR REPLACE MODEL `{PROJECT_ID}.{DATASET_ID}.regression_forecast_ex1`
|
|
127
|
+
OPTIONS(
|
|
128
|
+
model_type = 'LINEAR_REG',
|
|
129
|
+
input_label_cols = ['num_trips']
|
|
130
|
+
) AS
|
|
131
|
+
{EX1_CTE}
|
|
132
|
+
SELECT year, month, day_of_month, day_of_year, day_of_week, weekend, num_trips
|
|
133
|
+
FROM prepped
|
|
134
|
+
WHERE splits = 'TRAIN'
|
|
135
|
+
"""
|
|
136
|
+
client.query(query).result()
|
|
137
|
+
print('Model regression_forecast_ex1 created')
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
query = f"""
|
|
142
|
+
{EX1_CTE}
|
|
143
|
+
SELECT * FROM ML.EVALUATE(MODEL `{PROJECT_ID}.{DATASET_ID}.regression_forecast_ex1`,
|
|
144
|
+
(SELECT year, month, day_of_month, day_of_year, day_of_week, weekend, num_trips
|
|
145
|
+
FROM prepped WHERE splits = 'TEST'))
|
|
146
|
+
"""
|
|
147
|
+
client.query(query).to_dataframe()
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### A custom metrics helper, reused for every technique below
|
|
151
|
+
|
|
152
|
+
BQML's `ML.EVALUATE` only applies cleanly to Example 1's simple train/test split. Examples 2–4 build their own actual-vs-predicted tables, so the same four metrics (MAE, RMSE, MAPE, pMAE) are computed directly in pandas here — applied identically to every technique for a fair final comparison.
|
|
153
|
+
|
|
154
|
+
A saved actual-vs-predicted table is also exactly what `functions/evaluation` (`functions/evaluation/`)'s `ML.METRICS` takes — no model argument, so it works on Examples 2–4 where `ML.EVALUATE` cannot. It would supply `mean_absolute_error` here (plus `mean_squared_error`, `r2_score` and three more), but not RMSE, MAPE or pMAE, and it returns one row per call rather than one row per technique. Four metrics in a single pandas pass is the simpler thing to compare across techniques; reach for `ML.METRICS` when the predictions live in BigQuery and the scoring has to stay in SQL.
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
def compute_metrics(df, actual_col='actual', pred_col='predicted'):
|
|
158
|
+
errors = df[actual_col] - df[pred_col]
|
|
159
|
+
mae = errors.abs().mean()
|
|
160
|
+
rmse = (errors ** 2).mean() ** 0.5
|
|
161
|
+
mape = (errors.abs() / df[actual_col]).mean() * 100
|
|
162
|
+
pmae = mae / df[actual_col].mean() * 100
|
|
163
|
+
return pd.Series({'MAE': mae, 'RMSE': rmse, 'MAPE (%)': mape, 'pMAE (%)': pmae})
|
|
164
|
+
|
|
165
|
+
results = {} # technique name -> metrics Series, filled in as each example runs
|
|
166
|
+
|
|
167
|
+
query = f"""
|
|
168
|
+
{EX1_CTE}
|
|
169
|
+
SELECT date, num_trips AS actual, predicted_num_trips AS predicted
|
|
170
|
+
FROM ML.PREDICT(MODEL `{PROJECT_ID}.{DATASET_ID}.regression_forecast_ex1`,
|
|
171
|
+
(SELECT * FROM prepped WHERE splits = 'TEST'))
|
|
172
|
+
ORDER BY date
|
|
173
|
+
"""
|
|
174
|
+
ex1_predictions = client.query(query).to_dataframe()
|
|
175
|
+
results['Ex1: time features only (LINEAR_REG)'] = compute_metrics(ex1_predictions)
|
|
176
|
+
|
|
177
|
+
fig, ax = plt.subplots(figsize=(10, 4))
|
|
178
|
+
ax.plot(ex1_predictions['date'], ex1_predictions['actual'], label='actual', marker='o')
|
|
179
|
+
ax.plot(ex1_predictions['date'], ex1_predictions['predicted'], label='predicted', marker='o')
|
|
180
|
+
ax.set_title('Example 1: time features only — TEST period')
|
|
181
|
+
ax.legend()
|
|
182
|
+
plt.show()
|
|
183
|
+
|
|
184
|
+
results['Ex1: time features only (LINEAR_REG)']
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
## Example 2 — Add lag features (`LINEAR_REG`)
|
|
189
|
+
|
|
190
|
+
Add 1-day, 1-week, 1-month, 1-quarter, and 1-year lags of `num_trips` to the time features. This raises a real forecasting trap: at real forecast time, you only truly know the 1-day/1-week lags for the *first* day or week of a multi-day horizon — everything past that would have to come from your own prior forecasts, not ground truth. Evaluating naively against the TEST split lets those future lags leak in.
|
|
191
|
+
|
|
192
|
+
```python
|
|
193
|
+
EX2_CTE = f"""
|
|
194
|
+
WITH prepped AS (
|
|
195
|
+
SELECT
|
|
196
|
+
date,
|
|
197
|
+
num_trips,
|
|
198
|
+
IF(date > DATE('2018-05-03'), 'TEST', 'TRAIN') AS splits,
|
|
199
|
+
EXTRACT(YEAR FROM date) AS year,
|
|
200
|
+
EXTRACT(MONTH FROM date) AS month,
|
|
201
|
+
EXTRACT(DAY FROM date) AS day_of_month,
|
|
202
|
+
EXTRACT(DAYOFYEAR FROM date) AS day_of_year,
|
|
203
|
+
EXTRACT(DAYOFWEEK FROM date) AS day_of_week,
|
|
204
|
+
IF(EXTRACT(DAYOFWEEK FROM date) IN (1, 7), 1, 0) AS weekend,
|
|
205
|
+
LAG(num_trips, 1) OVER (ORDER BY date) AS lag_1day,
|
|
206
|
+
LAG(num_trips, 7) OVER (ORDER BY date) AS lag_1week,
|
|
207
|
+
LAG(num_trips, 28) OVER (ORDER BY date) AS lag_1month,
|
|
208
|
+
LAG(num_trips, 90) OVER (ORDER BY date) AS lag_1quarter,
|
|
209
|
+
LAG(num_trips, 365) OVER (ORDER BY date) AS lag_1year
|
|
210
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.regression_forecasting_trips`
|
|
211
|
+
)
|
|
212
|
+
"""
|
|
213
|
+
|
|
214
|
+
query = f"""
|
|
215
|
+
CREATE OR REPLACE MODEL `{PROJECT_ID}.{DATASET_ID}.regression_forecast_ex2`
|
|
216
|
+
OPTIONS(
|
|
217
|
+
model_type = 'LINEAR_REG',
|
|
218
|
+
input_label_cols = ['num_trips']
|
|
219
|
+
) AS
|
|
220
|
+
{EX2_CTE}
|
|
221
|
+
SELECT year, month, day_of_month, day_of_year, day_of_week, weekend,
|
|
222
|
+
lag_1day, lag_1week, lag_1month, lag_1quarter, lag_1year, num_trips
|
|
223
|
+
FROM prepped
|
|
224
|
+
WHERE splits = 'TRAIN'
|
|
225
|
+
"""
|
|
226
|
+
client.query(query).result()
|
|
227
|
+
print('Model regression_forecast_ex2 created (BQML mean-imputes the NULL lags in the earliest TRAIN rows)')
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### Three ways to evaluate a lag-based model on the TEST horizon
|
|
231
|
+
|
|
232
|
+
- **LEAKED** — evaluate straight against the TEST split's real lag values. Every lag, even ones that would require knowing 20+ days into the future, is the true historical number. Optimistic and unrealistic beyond day 1.
|
|
233
|
+
- **TRUNCATED** — null out `lag_1day`/`lag_1week` wherever they'd fall inside the TEST horizon itself (not truly knowable yet). Honest about what's missing, but BQML's NULL-handling isn't the same as actually forecasting a lag — likely too pessimistic.
|
|
234
|
+
- **RECURSIVE** — the way this model would actually be used: forecast one day at a time, feeding each forecast back in as the next day's lag input.
|
|
235
|
+
|
|
236
|
+
LEAKED is the *time-series* form of a mistake that shows up everywhere features are built from history. Its cross-sectional twin — aggregating an entity's whole history into a feature and joining it onto rows that predate the aggregation — is measured end to end in `workflows/feature_store` (`workflows/feature_store/`), where it is worth `roc_auc` 0.9387 against a correct 0.5194 and then evaporates in production. The retrieval functions that avoid it are covered in `functions/feature_store` (`functions/feature_store/`); the reason they do not apply directly here is that this notebook's entity is a *single* series, so its as-of cutoff is the forecast origin rather than a per-entity timestamp.
|
|
237
|
+
|
|
238
|
+
```python
|
|
239
|
+
# LEAKED: straight prediction against the TEST split's real (future) lag values
|
|
240
|
+
query = f"""
|
|
241
|
+
{EX2_CTE}
|
|
242
|
+
SELECT date, num_trips AS actual, predicted_num_trips AS predicted
|
|
243
|
+
FROM ML.PREDICT(MODEL `{PROJECT_ID}.{DATASET_ID}.regression_forecast_ex2`,
|
|
244
|
+
(SELECT * FROM prepped WHERE splits = 'TEST'))
|
|
245
|
+
ORDER BY date
|
|
246
|
+
"""
|
|
247
|
+
leaked_predictions = client.query(query).to_dataframe()
|
|
248
|
+
compute_metrics(leaked_predictions)
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
```python
|
|
252
|
+
# TRUNCATED: null out lag_1day/lag_1week wherever they'd require knowing the TEST horizon's own future
|
|
253
|
+
query = f"""
|
|
254
|
+
{EX2_CTE}
|
|
255
|
+
SELECT date, num_trips AS actual, predicted_num_trips AS predicted
|
|
256
|
+
FROM ML.PREDICT(MODEL `{PROJECT_ID}.{DATASET_ID}.regression_forecast_ex2`,
|
|
257
|
+
(SELECT * EXCEPT(lag_1day, lag_1week),
|
|
258
|
+
IF(date > DATE_ADD(DATE('2018-05-03'), INTERVAL 1 DAY), NULL, lag_1day) AS lag_1day,
|
|
259
|
+
IF(date > DATE_ADD(DATE('2018-05-03'), INTERVAL 7 DAY), NULL, lag_1week) AS lag_1week
|
|
260
|
+
FROM prepped WHERE splits = 'TEST'))
|
|
261
|
+
ORDER BY date
|
|
262
|
+
"""
|
|
263
|
+
truncated_predictions = client.query(query).to_dataframe()
|
|
264
|
+
compute_metrics(truncated_predictions)
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
### Recursive prediction
|
|
268
|
+
|
|
269
|
+
Predict the first TEST day using its true lag values (known at the forecast origin), then predict each subsequent day using the *previous day's own forecast* as its `lag_1day` (and, once the horizon passes 7 days, as its `lag_1week`). `lag_1month`/`lag_1quarter`/`lag_1year` always fall inside the 28-day horizon's TRAIN history, so they stay real throughout — a 28-day horizon never reaches far enough to need a forecasted `lag_1month` value.
|
|
270
|
+
|
|
271
|
+
```python
|
|
272
|
+
import datetime
|
|
273
|
+
|
|
274
|
+
history = client.query(f"""
|
|
275
|
+
SELECT date, num_trips FROM `{PROJECT_ID}.{DATASET_ID}.regression_forecasting_trips` ORDER BY date
|
|
276
|
+
""").to_dataframe()
|
|
277
|
+
history['date'] = pd.to_datetime(history['date'])
|
|
278
|
+
known_values = history.set_index('date')['num_trips'].to_dict()
|
|
279
|
+
|
|
280
|
+
test_start = datetime.date(2018, 5, 4)
|
|
281
|
+
horizon = 28
|
|
282
|
+
forecasts = {}
|
|
283
|
+
|
|
284
|
+
def value_on(d):
|
|
285
|
+
d = pd.Timestamp(d)
|
|
286
|
+
return forecasts.get(d, known_values.get(d))
|
|
287
|
+
|
|
288
|
+
recursive_rows = []
|
|
289
|
+
for h in range(horizon):
|
|
290
|
+
d = test_start + datetime.timedelta(days=h)
|
|
291
|
+
lag_1day = value_on(d - datetime.timedelta(days=1))
|
|
292
|
+
lag_1week = value_on(d - datetime.timedelta(days=7))
|
|
293
|
+
lag_1month = value_on(d - datetime.timedelta(days=28))
|
|
294
|
+
lag_1quarter = value_on(d - datetime.timedelta(days=90))
|
|
295
|
+
lag_1year = value_on(d - datetime.timedelta(days=365))
|
|
296
|
+
day_of_week = d.isoweekday() % 7 + 1 # BigQuery DAYOFWEEK: Sunday=1 ... Saturday=7
|
|
297
|
+
weekend = 1 if day_of_week in (1, 7) else 0
|
|
298
|
+
|
|
299
|
+
query = f"""
|
|
300
|
+
SELECT predicted_num_trips
|
|
301
|
+
FROM ML.PREDICT(MODEL `{PROJECT_ID}.{DATASET_ID}.regression_forecast_ex2`, (
|
|
302
|
+
SELECT {d.year} AS year, {d.month} AS month, {d.day} AS day_of_month,
|
|
303
|
+
{d.timetuple().tm_yday} AS day_of_year, {day_of_week} AS day_of_week, {weekend} AS weekend,
|
|
304
|
+
CAST({lag_1day} AS INT64) AS lag_1day, CAST({lag_1week} AS INT64) AS lag_1week,
|
|
305
|
+
CAST({lag_1month} AS INT64) AS lag_1month, CAST({lag_1quarter} AS INT64) AS lag_1quarter,
|
|
306
|
+
CAST({lag_1year} AS INT64) AS lag_1year
|
|
307
|
+
))
|
|
308
|
+
"""
|
|
309
|
+
predicted = client.query(query).to_dataframe()['predicted_num_trips'].iloc[0]
|
|
310
|
+
forecasts[pd.Timestamp(d)] = predicted
|
|
311
|
+
recursive_rows.append({'date': d, 'actual': known_values.get(pd.Timestamp(d)), 'predicted': predicted})
|
|
312
|
+
|
|
313
|
+
recursive_predictions = pd.DataFrame(recursive_rows)
|
|
314
|
+
recursive_predictions
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
```python
|
|
318
|
+
comparison = pd.DataFrame({
|
|
319
|
+
'LEAKED': compute_metrics(leaked_predictions),
|
|
320
|
+
'TRUNCATED': compute_metrics(truncated_predictions),
|
|
321
|
+
'RECURSIVE': compute_metrics(recursive_predictions),
|
|
322
|
+
}).T
|
|
323
|
+
comparison
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
LEAKED looks the most accurate (it's the only one allowed to peek at real future demand through its lags) and TRUNCATED the least — mean-imputing an unknown lag throws away real signal without replacing it with anything useful. RECURSIVE lands in between: a realistic, deployable evaluation that at least feeds the model *something* informative (its own prior forecast) instead of a mean-imputed placeholder. **RECURSIVE is the number carried into the final comparison below — it's the only one of the three that reflects how this model would actually be run.**
|
|
327
|
+
|
|
328
|
+
```python
|
|
329
|
+
results['Ex2: + lag features, recursive (LINEAR_REG)'] = compute_metrics(recursive_predictions)
|
|
330
|
+
|
|
331
|
+
fig, ax = plt.subplots(figsize=(10, 4))
|
|
332
|
+
ax.plot(recursive_predictions['date'], recursive_predictions['actual'], label='actual', marker='o')
|
|
333
|
+
ax.plot(recursive_predictions['date'], recursive_predictions['predicted'], label='predicted (recursive)', marker='o')
|
|
334
|
+
ax.set_title('Example 2: + lag features, recursive forecast — TEST period')
|
|
335
|
+
ax.legend()
|
|
336
|
+
plt.show()
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
---
|
|
340
|
+
## Example 3 — Direct multi-step (`LINEAR_REG`)
|
|
341
|
+
|
|
342
|
+
Train a separate model for each of the 28 horizon days: model *h*'s label is `LEAD(num_trips, h)`, so it directly predicts "the value *h* days from now" from lag/time features anchored at a single origin date — no recursion, and no dependence on a prior forecast's error compounding into the next step.
|
|
343
|
+
|
|
344
|
+
**Avoiding a subtle leak:** a naive `WHERE splits = 'TRAIN'` filter on the *anchor* date would still allow some anchors near the TRAIN/TEST boundary to have a label drawn from the TEST period itself (since the label is `h` days *after* the anchor). Each model instead requires `date + h <= '2018-05-03'`, so every training label comes from TRAIN, never TEST.
|
|
345
|
+
|
|
346
|
+
All 28 models are submitted to BigQuery concurrently (fast here regardless — `LINEAR_REG` trains in seconds — but this also validates the pattern used for the much slower boosted-tree version in Example 4 below).
|
|
347
|
+
|
|
348
|
+
```python
|
|
349
|
+
def make_direct_cte(h):
|
|
350
|
+
return f"""
|
|
351
|
+
WITH prepped AS (
|
|
352
|
+
SELECT
|
|
353
|
+
date,
|
|
354
|
+
LEAD(num_trips, {h}) OVER (ORDER BY date) AS label,
|
|
355
|
+
EXTRACT(YEAR FROM date) AS year,
|
|
356
|
+
EXTRACT(MONTH FROM date) AS month,
|
|
357
|
+
EXTRACT(DAY FROM date) AS day_of_month,
|
|
358
|
+
EXTRACT(DAYOFYEAR FROM date) AS day_of_year,
|
|
359
|
+
EXTRACT(DAYOFWEEK FROM date) AS day_of_week,
|
|
360
|
+
IF(EXTRACT(DAYOFWEEK FROM date) IN (1, 7), 1, 0) AS weekend,
|
|
361
|
+
LAG(num_trips, 1) OVER (ORDER BY date) AS lag_1day,
|
|
362
|
+
LAG(num_trips, 7) OVER (ORDER BY date) AS lag_1week,
|
|
363
|
+
LAG(num_trips, 28) OVER (ORDER BY date) AS lag_1month,
|
|
364
|
+
LAG(num_trips, 90) OVER (ORDER BY date) AS lag_1quarter,
|
|
365
|
+
LAG(num_trips, 365) OVER (ORDER BY date) AS lag_1year
|
|
366
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.regression_forecasting_trips`
|
|
367
|
+
)
|
|
368
|
+
"""
|
|
369
|
+
|
|
370
|
+
TEST_ORIGIN = "DATE('2018-05-03')" # last TRAIN date — the forecast anchor for every horizon day
|
|
371
|
+
HORIZON = 28
|
|
372
|
+
|
|
373
|
+
def train_direct_model(model_name, model_type, h):
|
|
374
|
+
cte = make_direct_cte(h)
|
|
375
|
+
query = f"""
|
|
376
|
+
CREATE OR REPLACE MODEL `{PROJECT_ID}.{DATASET_ID}.{model_name}`
|
|
377
|
+
OPTIONS(model_type = '{model_type}', input_label_cols = ['label']) AS
|
|
378
|
+
{cte}
|
|
379
|
+
SELECT year, month, day_of_month, day_of_year, day_of_week, weekend,
|
|
380
|
+
lag_1day, lag_1week, lag_1month, lag_1quarter, lag_1year, label
|
|
381
|
+
FROM prepped
|
|
382
|
+
WHERE date + {h} <= {TEST_ORIGIN} AND label IS NOT NULL AND lag_1year IS NOT NULL
|
|
383
|
+
"""
|
|
384
|
+
return client.query(query) # submitted asynchronously — caller awaits .result()
|
|
385
|
+
|
|
386
|
+
def predict_direct_model(model_name, h):
|
|
387
|
+
cte = make_direct_cte(h)
|
|
388
|
+
query = f"""
|
|
389
|
+
{cte}
|
|
390
|
+
SELECT date + {h} AS date, label AS actual, predicted_label AS predicted
|
|
391
|
+
FROM ML.PREDICT(MODEL `{PROJECT_ID}.{DATASET_ID}.{model_name}`,
|
|
392
|
+
(SELECT * FROM prepped WHERE date = {TEST_ORIGIN}))
|
|
393
|
+
"""
|
|
394
|
+
return client.query(query).to_dataframe()
|
|
395
|
+
|
|
396
|
+
def train_direct_models_in_batches(model_prefix, model_type, batch_size):
|
|
397
|
+
"""Submit CREATE MODEL jobs concurrently in batches of `batch_size`, waiting for each
|
|
398
|
+
batch to finish before submitting the next (BigQuery runs distinctly-named CREATE MODEL
|
|
399
|
+
jobs from one client in true parallel — verified live — so a batch of jobs finishes in
|
|
400
|
+
roughly the time of its single slowest model, not the sum of all of them)."""
|
|
401
|
+
horizon_days = list(range(1, HORIZON + 1))
|
|
402
|
+
for batch_start in range(0, len(horizon_days), batch_size):
|
|
403
|
+
batch = horizon_days[batch_start:batch_start + batch_size]
|
|
404
|
+
jobs = {h: train_direct_model(f'{model_prefix}_h{h}', model_type, h) for h in batch}
|
|
405
|
+
for h, job in jobs.items():
|
|
406
|
+
job.result()
|
|
407
|
+
print(f'{model_prefix}: trained horizon days {batch}')
|
|
408
|
+
|
|
409
|
+
train_direct_models_in_batches('regression_forecast_ex3', 'LINEAR_REG', batch_size=HORIZON)
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
```python
|
|
413
|
+
ex3_predictions = pd.concat(
|
|
414
|
+
[predict_direct_model(f'regression_forecast_ex3_h{h}', h) for h in range(1, HORIZON + 1)],
|
|
415
|
+
ignore_index=True
|
|
416
|
+
).sort_values('date')
|
|
417
|
+
results['Ex3: direct multi-step (LINEAR_REG)'] = compute_metrics(ex3_predictions)
|
|
418
|
+
|
|
419
|
+
fig, ax = plt.subplots(figsize=(10, 4))
|
|
420
|
+
ax.plot(ex3_predictions['date'], ex3_predictions['actual'], label='actual', marker='o')
|
|
421
|
+
ax.plot(ex3_predictions['date'], ex3_predictions['predicted'], label='predicted', marker='o')
|
|
422
|
+
ax.set_title('Example 3: direct multi-step (LINEAR_REG) — TEST period')
|
|
423
|
+
ax.legend()
|
|
424
|
+
plt.show()
|
|
425
|
+
|
|
426
|
+
results['Ex3: direct multi-step (LINEAR_REG)']
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
---
|
|
430
|
+
## Example 4 — Direct multi-step (`BOOSTED_TREE_REGRESSOR`)
|
|
431
|
+
|
|
432
|
+
Identical structure to Example 3 — one model per horizon day, same leak-free training filter — swapping in gradient-boosted trees.
|
|
433
|
+
|
|
434
|
+
**GOTCHA (verified):** a single `BOOSTED_TREE_REGRESSOR` model here takes ~2.5–4.5 minutes to train, versus ~15 seconds for the equivalent `LINEAR_REG` model — on the same, small (fewer than 1,000 row) training table. Pre-validating this notebook ruled out two obvious explanations directly: reducing `max_iterations` and disabling `early_stop` made a model take *longer*, not shorter (ruling out boosting-round count as the driver), and running the same model under a temporary BigQuery Editions autoscale reservation made no measurable difference either (ruling out on-demand slot queueing — the job's own query plan showed it actively burning slot-time the whole way through, not waiting for capacity). This appears to be a fixed cost of `BOOSTED_TREE_REGRESSOR` training in BigQuery ML, not something tunable away.
|
|
435
|
+
|
|
436
|
+
Training all 28 sequentially would take **1.5–2 hours**. This step instead reuses `train_direct_models_in_batches` from Example 3 with a smaller batch size — pre-validated directly: submitting several `BOOSTED_TREE_REGRESSOR` `CREATE MODEL` jobs (with distinct model names) at once lets BigQuery run them in true concurrent parallel, so a batch of ~10 finishes in minutes, not 10× the single-model time. Expect this cell to take **roughly 15–30 minutes** in total across all 3 batches — still a large improvement over fully sequential, but the longest-running cell in this notebook by far.
|
|
437
|
+
|
|
438
|
+
```python
|
|
439
|
+
train_direct_models_in_batches('regression_forecast_ex4', 'BOOSTED_TREE_REGRESSOR', batch_size=10)
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
```python
|
|
443
|
+
ex4_predictions = pd.concat(
|
|
444
|
+
[predict_direct_model(f'regression_forecast_ex4_h{h}', h) for h in range(1, HORIZON + 1)],
|
|
445
|
+
ignore_index=True
|
|
446
|
+
).sort_values('date')
|
|
447
|
+
results['Ex4: direct multi-step (BOOSTED_TREE_REGRESSOR)'] = compute_metrics(ex4_predictions)
|
|
448
|
+
|
|
449
|
+
fig, ax = plt.subplots(figsize=(10, 4))
|
|
450
|
+
ax.plot(ex4_predictions['date'], ex4_predictions['actual'], label='actual', marker='o')
|
|
451
|
+
ax.plot(ex4_predictions['date'], ex4_predictions['predicted'], label='predicted', marker='o')
|
|
452
|
+
ax.set_title('Example 4: direct multi-step (BOOSTED_TREE_REGRESSOR) — TEST period')
|
|
453
|
+
ax.legend()
|
|
454
|
+
plt.show()
|
|
455
|
+
|
|
456
|
+
results['Ex4: direct multi-step (BOOSTED_TREE_REGRESSOR)']
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
---
|
|
460
|
+
## Comparison
|
|
461
|
+
|
|
462
|
+
All four techniques, evaluated identically (MAE/RMSE/MAPE/pMAE over the same 28-day TEST window), plus a reference row pulled directly from `models/arima_plus/`'s own `ML.EVALUATE` output for Pershing Square North on this exact TEST window — the native time-series model this workflow is implicitly competing with.
|
|
463
|
+
|
|
464
|
+
```python
|
|
465
|
+
comparison_table = pd.DataFrame(results).T
|
|
466
|
+
|
|
467
|
+
# Reference row: ARIMA_PLUS's own measured accuracy for Pershing Square North on the identical
|
|
468
|
+
# TEST window, taken directly from models/arima_plus/'s ML.EVALUATE output (Step 5 there).
|
|
469
|
+
test_avg_actual = client.query(f"""
|
|
470
|
+
SELECT AVG(num_trips) AS avg_actual
|
|
471
|
+
FROM `{PROJECT_ID}.{DATASET_ID}.regression_forecasting_trips`
|
|
472
|
+
WHERE date > DATE('2018-05-03')
|
|
473
|
+
""").to_dataframe()['avg_actual'].iloc[0]
|
|
474
|
+
arima_mae = 135.461280
|
|
475
|
+
comparison_table.loc['Reference: ARIMA_PLUS (models/arima_plus/)'] = {
|
|
476
|
+
'MAE': arima_mae,
|
|
477
|
+
'RMSE': 162.790009,
|
|
478
|
+
'MAPE (%)': 56.792525,
|
|
479
|
+
'pMAE (%)': arima_mae / test_avg_actual * 100,
|
|
480
|
+
}
|
|
481
|
+
comparison_table
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
**Read this table for the ranking, not as a verdict on regression vs. ARIMA_PLUS in general** — it reflects one station, one 28-day window, and untuned default hyperparameters throughout. `ARIMA_PLUS` gets automatic seasonal/trend decomposition, holiday effects, and anomaly-robust fitting for free; every regression technique here only has whatever's explicit in its feature list.
|
|
485
|
+
|
|
486
|
+
Two results are worth calling out specifically, since they run against the naive expectation that "more principled" or "more sophisticated" automatically means "more accurate":
|
|
487
|
+
|
|
488
|
+
- **Direct multi-step `LINEAR_REG` (Example 3) is the *worst* performer here on every metric** — worse than even the plain time-features-only model (Example 1). Avoiding recursion and evaluation leakage doesn't help if it comes at the cost of splitting one already-small training table (~1,150 TRAIN rows) into 28 separate per-horizon models, each fit on fewer effective examples than Example 1 or 2 ever had to work with.
|
|
489
|
+
- **Direct multi-step `BOOSTED_TREE_REGRESSOR` (Example 4) gets the best MAPE of *any* technique in this table, including the `ARIMA_PLUS` reference** (45.68% vs. ARIMA_PLUS's 56.79%) — despite a worse MAE/RMSE than ARIMA_PLUS. MAE/RMSE are dominated by the largest absolute misses (weekday demand spikes); MAPE weights every day equally in relative terms. A model can be the better *percentage-error* forecaster while still losing on absolute error, and this table has a genuine example of exactly that.
|
|
490
|
+
|
|
491
|
+
`ARIMA_PLUS` still wins on MAE, RMSE, and pMAE, and Example 2's recursive `LINEAR_REG` remains the strongest of the regression techniques on those same three metrics — the takeaway isn't "boosted trees beat ARIMA_PLUS," it's that **which metric you optimize for changes which technique looks best**, even on identical data.
|