Skip to content

Instantly share code, notes, and snippets.

@jadechip
Created November 16, 2019 07:31
Show Gist options
  • Save jadechip/a98e36a9549f57cf4288a32cac492ee1 to your computer and use it in GitHub Desktop.
Save jadechip/a98e36a9549f57cf4288a32cac492ee1 to your computer and use it in GitHub Desktop.
Create rudimentary classification model in BigQuery ML
CREATE OR REPLACE MODEL `ecommerce.classification_model`
OPTIONS
(
model_type='logistic_reg',
labels = ['will_buy_on_return_visit']
)
AS
#standardSQL
SELECT
* EXCEPT(fullVisitorId)
FROM
# features
(SELECT
fullVisitorId,
IFNULL(totals.bounces, 0) AS bounces,
IFNULL(totals.timeOnSite, 0) AS time_on_site
FROM
`data-to-insights.ecommerce.web_analytics`
WHERE
totals.newVisits = 1
AND date BETWEEN '20160801' AND '20170430') # train on first 9 months
JOIN
(SELECT
fullvisitorid,
IF(COUNTIF(totals.transactions > 0 AND totals.newVisits IS NULL) > 0, 1, 0) AS will_buy_on_return_visit
FROM
`data-to-insights.ecommerce.web_analytics`
GROUP BY fullvisitorid)
USING (fullVisitorId)
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment