forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathncaa_tutorial_test.py
More file actions
140 lines (128 loc) · 4.53 KB
/
ncaa_tutorial_test.py
File metadata and controls
140 lines (128 loc) · 4.53 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Copyright 2018 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import io
import os
import uuid
# [START bqml_ncaa_tutorial_import_and_client]
from google.cloud import bigquery
# [END bqml_ncaa_tutorial_import_and_client]
import pytest
# [START bqml_ncaa_tutorial_import_and_client]
client = bigquery.Client()
# We use a unique dataset ID for this example to avoid collisions with
# other invocations of this tutorial. In practice, you could leverage
# a persistent dataset and not create/destroy it with each invocation.
dataset_id = "bqml_tutorial_{}".format(str(uuid.uuid4().hex))
# [END bqml_ncaa_tutorial_import_and_client]
@pytest.fixture
def delete_dataset():
yield
client.delete_dataset(
client.dataset(dataset_id), delete_contents=True)
def test_ncaa_tutorial(delete_dataset):
# [START bqml_ncaa_tutorial_create_dataset]
dataset = bigquery.Dataset(client.dataset(dataset_id))
dataset.location = 'US'
client.create_dataset(dataset)
# [END bqml_ncaa_tutorial_create_dataset]
# Create the tables used by the tutorial
# Note: the queries are saved to a file. This should be updated to use the
# saved queries once the library supports running saved queries.
query_files = ['feature_input_query.sql', 'training_data_query.sql']
resources_directory = os.path.join(os.path.dirname(__file__), 'resources')
for fname in query_files:
query_filepath = os.path.join(
resources_directory, fname)
sql = io.open(query_filepath, 'r', encoding='utf-8').read().format(dataset_id)
client.query(sql).result()
# [START bqml_ncaa_tutorial_create_model]
sql = """
CREATE OR REPLACE MODEL `{0}.ncaa_model`
OPTIONS (
model_type='linear_reg',
max_iteration=50 ) AS
SELECT
* EXCEPT (
game_id, season, scheduled_date,
total_three_points_made,
total_three_points_att),
total_three_points_att as label
FROM
`{0}.wide_games`
WHERE
# remove the game to predict
game_id != 'f1063e80-23c7-486b-9a5e-faa52beb2d83'
""".format(dataset_id)
df = client.query(sql).to_dataframe()
print(df)
# [END bqml_ncaa_tutorial_create_model]
# [START bqml_ncaa_tutorial_get_training_statistics]
sql = """
SELECT
*
FROM
ML.TRAINING_INFO(MODEL `{}.ncaa_model`)
""".format(dataset_id)
df = client.query(sql).to_dataframe()
print(df)
# [END bqml_ncaa_tutorial_get_training_statistics]
# [START bqml_ncaa_tutorial_evaluate_model]
sql = """
WITH eval_table AS (
SELECT
*,
total_three_points_att AS label
FROM
`{0}.wide_games` )
SELECT
*
FROM
ML.EVALUATE(MODEL `{0}.ncaa_model`,
TABLE eval_table)
""".format(dataset_id)
df = client.query(sql).to_dataframe()
print(df)
# [END bqml_ncaa_tutorial_evaluate_model]
# [START bqml_ncaa_tutorial_predict_outcomes]
sql = """
WITH game_to_predict AS (
SELECT
*
FROM
`{0}.wide_games`
WHERE
game_id='f1063e80-23c7-486b-9a5e-faa52beb2d83' )
SELECT
truth.game_id AS game_id,
total_three_points_att,
predicted_total_three_points_att
FROM (
SELECT
game_id,
predicted_label AS predicted_total_three_points_att
FROM
ML.PREDICT(MODEL `{0}.ncaa_model`,
table game_to_predict) ) AS predict
JOIN (
SELECT
game_id,
total_three_points_att AS total_three_points_att
FROM
game_to_predict) AS truth
ON
predict.game_id = truth.game_id
""".format(dataset_id)
df = client.query(sql).to_dataframe()
print(df)
# [END bqml_ncaa_tutorial_predict_outcomes]