-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path02_BigQuery_BQML.model.lkml
102 lines (88 loc) · 2.73 KB
/
02_BigQuery_BQML.model.lkml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Adapted from https://github.com/GoogleCloudPlatform/training-data-analyst/blob/master/courses/bdml_fundamentals/demos/bqml-classification-model.sql
connection: "XYZ"
explore: full_data {
label: "Data and Prediction"
join: model_prediction {
relationship: one_to_one
type: inner
sql_on: ${model_prediction.fullVisitorId} = ${full_data.fullvisitorid} ;;
}
}
view: full_data {
derived_table:{
sql: WITH first_time_visitor as (SELECT
fullVisitorId,
date,
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)
SELECT * FROM first_time_visitor
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);;
}
dimension: fullvisitorid {}
dimension: will_buy_on_return_visit {
label: "Did buy on returning visit"
}
dimension: bounces {type:number}
dimension: time_on_site {type:number}
}
view: training_input {
derived_table: {
sql: SELECT * FROM ${full_data.SQL_TABLE_NAME} WHERE date BETWEEN '20160801' AND '20170430';;
}
}
view: testing_input {
derived_table: {
sql: SELECT * FROM ${full_data.SQL_TABLE_NAME} WHERE date BETWEEN '20170501' AND '20170630' ;;
}
}
view: future_purchase_model {
derived_table: {
persist_for: "24 hours" # need to have persistence
sql_create:
CREATE OR REPLACE MODEL ${SQL_TABLE_NAME}
OPTIONS(model_type='logistic_reg'
, labels=['will_buy_on_return_visit']
, min_rel_progress = 0.005
, max_iterations = 40
) AS
SELECT
* EXCEPT(fullVisitorId)
FROM ${training_input.SQL_TABLE_NAME};;
}
}
explore: model_evaluation {}
view: model_evaluation {
derived_table: {
sql: SELECT roc_auc,
CASE
WHEN roc_auc > .9 THEN 'good'
WHEN roc_auc > .8 THEN 'fair'
WHEN roc_auc > .7 THEN 'decent'
WHEN roc_auc > .6 THEN 'not great'
ELSE 'poor' END AS model_quality,
log_loss
FROM ML.EVALUATE(MODEL ${future_purchase_model.SQL_TABLE_NAME},(SELECT * FROM ${testing_input.SQL_TABLE_NAME})) ;;
}
dimension: model_quality {}
}
view: model_prediction {
derived_table: {
sql: SELECT * FROM ml.PREDICT(
MODEL ${future_purchase_model.SQL_TABLE_NAME},
(SELECT * FROM ${full_data.SQL_TABLE_NAME}));;
}
dimension: predicted_will_buy_on_return_visit {type:number}
dimension: fullVisitorId {
type: number
hidden:yes}
}