|
| 1 | +# Copyright 2018 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START bqml_data_scientist_tutorial_import_and_client] |
| 16 | +from google.cloud import bigquery |
| 17 | +# [END bqml_data_scientist_tutorial_import_and_client] |
| 18 | +import pytest |
| 19 | + |
| 20 | +# [START bqml_data_scientist_tutorial_import_and_client] |
| 21 | +client = bigquery.Client() |
| 22 | +# [END bqml_data_scientist_tutorial_import_and_client] |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def delete_dataset(): |
| 27 | + yield |
| 28 | + client.delete_dataset( |
| 29 | + client.dataset('bqml_tutorial'), delete_contents=True) |
| 30 | + |
| 31 | + |
| 32 | +def test_data_scientist_tutorial(delete_dataset): |
| 33 | + # [START bqml_data_scientist_tutorial_create_dataset] |
| 34 | + dataset = bigquery.Dataset(client.dataset('bqml_tutorial')) |
| 35 | + dataset.location = 'US' |
| 36 | + client.create_dataset(dataset) |
| 37 | + # [END bqml_data_scientist_tutorial_create_dataset] |
| 38 | + |
| 39 | + # [START bqml_data_scientist_tutorial_create_model] |
| 40 | + sql = """ |
| 41 | + CREATE OR REPLACE MODEL `bqml_tutorial.sample_model` |
| 42 | + OPTIONS(model_type='logistic_reg') AS |
| 43 | + SELECT |
| 44 | + IF(totals.transactions IS NULL, 0, 1) AS label, |
| 45 | + IFNULL(device.operatingSystem, "") AS os, |
| 46 | + device.isMobile AS is_mobile, |
| 47 | + IFNULL(geoNetwork.country, "") AS country, |
| 48 | + IFNULL(totals.pageviews, 0) AS pageviews |
| 49 | + FROM |
| 50 | + `bigquery-public-data.google_analytics_sample.ga_sessions_*` |
| 51 | + WHERE |
| 52 | + _TABLE_SUFFIX BETWEEN '20160801' AND '20170630' |
| 53 | + """ |
| 54 | + df = client.query(sql).to_dataframe() |
| 55 | + print(df) |
| 56 | + # [END bqml_data_scientist_tutorial_create_model] |
| 57 | + |
| 58 | + # [START bqml_data_scientist_tutorial_get_training_statistics] |
| 59 | + sql = """ |
| 60 | + SELECT |
| 61 | + * |
| 62 | + FROM |
| 63 | + ML.TRAINING_INFO(MODEL `bqml_tutorial.sample_model`) |
| 64 | + """ |
| 65 | + df = client.query(sql).to_dataframe() |
| 66 | + print(df) |
| 67 | + # [END bqml_data_scientist_tutorial_get_training_statistics] |
| 68 | + |
| 69 | + # [START bqml_data_scientist_tutorial_evaluate_model] |
| 70 | + sql = """ |
| 71 | + SELECT |
| 72 | + * |
| 73 | + FROM ML.EVALUATE(MODEL `bqml_tutorial.sample_model`, ( |
| 74 | + SELECT |
| 75 | + IF(totals.transactions IS NULL, 0, 1) AS label, |
| 76 | + IFNULL(device.operatingSystem, "") AS os, |
| 77 | + device.isMobile AS is_mobile, |
| 78 | + IFNULL(geoNetwork.country, "") AS country, |
| 79 | + IFNULL(totals.pageviews, 0) AS pageviews |
| 80 | + FROM |
| 81 | + `bigquery-public-data.google_analytics_sample.ga_sessions_*` |
| 82 | + WHERE |
| 83 | + _TABLE_SUFFIX BETWEEN '20170701' AND '20170801')) |
| 84 | + """ |
| 85 | + df = client.query(sql).to_dataframe() |
| 86 | + print(df) |
| 87 | + # [END bqml_data_scientist_tutorial_evaluate_model] |
| 88 | + |
| 89 | + # [START bqml_data_scientist_tutorial_predict_transactions] |
| 90 | + sql = """ |
| 91 | + SELECT |
| 92 | + country, |
| 93 | + SUM(predicted_label) as total_predicted_purchases |
| 94 | + FROM ML.PREDICT(MODEL `bqml_tutorial.sample_model`, ( |
| 95 | + SELECT |
| 96 | + IFNULL(device.operatingSystem, "") AS os, |
| 97 | + device.isMobile AS is_mobile, |
| 98 | + IFNULL(totals.pageviews, 0) AS pageviews, |
| 99 | + IFNULL(geoNetwork.country, "") AS country |
| 100 | + FROM |
| 101 | + `bigquery-public-data.google_analytics_sample.ga_sessions_*` |
| 102 | + WHERE |
| 103 | + _TABLE_SUFFIX BETWEEN '20170701' AND '20170801')) |
| 104 | + GROUP BY country |
| 105 | + ORDER BY total_predicted_purchases DESC |
| 106 | + LIMIT 10 |
| 107 | + """ |
| 108 | + df = client.query(sql).to_dataframe() |
| 109 | + print(df) |
| 110 | + # [END bqml_data_scientist_tutorial_predict_transactions] |
| 111 | + |
| 112 | + # [START bqml_data_scientist_tutorial_predict_purchases] |
| 113 | + sql = """ |
| 114 | + SELECT |
| 115 | + fullVisitorId, |
| 116 | + SUM(predicted_label) as total_predicted_purchases |
| 117 | + FROM ML.PREDICT(MODEL `bqml_tutorial.sample_model`, ( |
| 118 | + SELECT |
| 119 | + IFNULL(device.operatingSystem, "") AS os, |
| 120 | + device.isMobile AS is_mobile, |
| 121 | + IFNULL(totals.pageviews, 0) AS pageviews, |
| 122 | + IFNULL(geoNetwork.country, "") AS country, |
| 123 | + fullVisitorId |
| 124 | + FROM |
| 125 | + `bigquery-public-data.google_analytics_sample.ga_sessions_*` |
| 126 | + WHERE |
| 127 | + _TABLE_SUFFIX BETWEEN '20170701' AND '20170801')) |
| 128 | + GROUP BY fullVisitorId |
| 129 | + ORDER BY total_predicted_purchases DESC |
| 130 | + LIMIT 10 |
| 131 | + """ |
| 132 | + df = client.query(sql).to_dataframe() |
| 133 | + print(df) |
| 134 | + # [END bqml_data_scientist_tutorial_predict_purchases] |
0 commit comments