Skip to content

Commit

Permalink
updated bigquery_ml_classification_logistic_regression_predict_with_m…
Browse files Browse the repository at this point in the history
…odel2.sql
  • Loading branch information
HariSekhon committed Oct 10, 2024
1 parent 1c66587 commit a5db059
Showing 1 changed file with 53 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,72 +16,72 @@
-- use the improved model 2 to predict which users will buy on return visit

SELECT
*
*
FROM
ml.PREDICT(
MODEL `ecommerce.classification_model_2`, (
ml.PREDICT(
MODEL `ecommerce.classification_model_2`, (

WITH all_visitor_stats AS (
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
)
WITH all_visitor_stats AS (
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
)

SELECT
CONCAT(fullvisitorid, '-',CAST(visitId AS STRING)) AS unique_session_id,
SELECT
CONCAT(fullvisitorid, '-',CAST(visitId AS STRING)) AS unique_session_id,

-- labels
will_buy_on_return_visit,
-- labels
will_buy_on_return_visit,

MAX(CAST(h.eCommerceAction.action_type AS INT64)) AS latest_ecommerce_progress,
MAX(CAST(h.eCommerceAction.action_type AS INT64)) AS latest_ecommerce_progress,

-- behavior on the site
IFNULL(totals.bounces, 0) AS bounces,
IFNULL(totals.timeOnSite, 0) AS time_on_site,
totals.pageviews,
-- behavior on the site
IFNULL(totals.bounces, 0) AS bounces,
IFNULL(totals.timeOnSite, 0) AS time_on_site,
totals.pageviews,

-- where the visitor came from
trafficSource.source,
trafficSource.medium,
channelGrouping,
-- where the visitor came from
trafficSource.source,
trafficSource.medium,
channelGrouping,

-- mobile or desktop
device.deviceCategory,
-- mobile or desktop
device.deviceCategory,

-- geographic
IFNULL(geoNetwork.country, "") AS country
-- geographic
IFNULL(geoNetwork.country, "") AS country

FROM
`data-to-insights.ecommerce.web_analytics`,
UNNEST(hits) AS h
FROM
`data-to-insights.ecommerce.web_analytics`,
UNNEST(hits) AS h

JOIN
all_visitor_stats
USING
(fullvisitorid)
JOIN
all_visitor_stats
USING
(fullvisitorid)

WHERE
-- only predict for new visits
totals.newVisits = 1
-- test on a non-overlapping subset to the training data
AND date BETWEEN '20170701' AND '20170801'
WHERE
-- only predict for new visits
totals.newVisits = 1
-- test on a non-overlapping subset to the training data
AND date BETWEEN '20170701' AND '20170801'

GROUP BY
unique_session_id,
will_buy_on_return_visit,
bounces,
time_on_site,
totals.pageviews,
trafficSource.source,
trafficSource.medium,
channelGrouping,
device.deviceCategory,
country
GROUP BY
unique_session_id,
will_buy_on_return_visit,
bounces,
time_on_site,
totals.pageviews,
trafficSource.source,
trafficSource.medium,
channelGrouping,
device.deviceCategory,
country
)
)
)

ORDER BY
-- your label name will be prefixed with 'predicted_' for the field generated by the model
predicted_will_buy_on_return_visit DESC;
-- your label name will be prefixed with 'predicted_' for the field generated by the model
predicted_will_buy_on_return_visit DESC;

0 comments on commit a5db059

Please sign in to comment.