-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathput_prediction_data.py
135 lines (112 loc) · 3.78 KB
/
put_prediction_data.py
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
import json
import boto3
from prediction import *
import pandas as pd
from decimal import Decimal
import time
dynamo_conn = boto3.resource('dynamodb', region_name='us-west-2', aws_access_key_id='', aws_secret_access_key='')
# conn = boto3.resource('dynamodb', endpoint_url="http://host.docker.internal:8000/")
TABLE_NAME_PRED = 'nba_prediction'
def create_table():
try:
table_names = [table.name for table in dynamo_conn.tables.all()]
if TABLE_NAME_PRED in table_names:
table = dynamo_conn.Table(TABLE_NAME_PRED)
else:
table = dynamo_conn.create_table(
TableName=TABLE_NAME_PRED,
KeySchema=[
{'AttributeName': 'HOME_TEAM', 'KeyType': 'HASH'}
],
AttributeDefinitions=[
{'AttributeName': 'HOME_TEAM', 'AttributeType': 'S'}
],
ProvisionedThroughput={
'ReadCapacityUnits': 1,
'WriteCapacityUnits': 1
}
)
table.wait_until_exists()
except Exception as e:
raise
return table
def insert_item(table, data):
try:
item = {}
for home_team, home_list in data.items():
item['HOME_TEAM'] = home_team
for away_dict in home_list:
for away_team, proba in away_dict.items():
item[away_team] = round(Decimal(proba),3)
table.put_item(Item=item)
except Exception as e:
print(e)
def build_expression(data):
pf = 'prefix'
vals = {}
exp = 'SET '
attr_names = {}
for d in data:
for key, value in d.items():
vals[':{}'.format(key)] = round(Decimal(value),3)
attr_names['#pf_{}'.format(key)] = key
exp += '#pf_{} = :{},'.format(key, key)
exp = exp.rstrip(",")
return vals, exp, attr_names
def is_home_team_exist(table, home_team):
try:
x = table.get_item(Key={'HOME_TEAM': home_team})
except Exception as e:
print(e)
if 'Item' not in x:
return False
return True
def update_item(table, data):
for key, value in data.items():
expression_vals, update_expression, attr_names = build_expression(value)
table.update_item(
Key={
'HOME_TEAM': key
},
ExpressionAttributeNames=attr_names,
ExpressionAttributeValues=expression_vals,
UpdateExpression=update_expression,
ReturnValues='NONE'
)
def get_team_name(games):
team_name = set()
for game in games['TEAM_ABBREVIATION']:
team_name.add(game)
return team_name
def store_predict(games, model):
"""
create a DB where primary key is home_team. For each row is the home team and column is the away team.
ex: for row ATL and Column BKN, ATL has a probability of 0.473 to win against BKN
:param games:
:param model:
:return:
"""
# start = time.time()
table = create_table()
teams = get_team_name(games)
data = {}
for home_team in teams:
home_list = []
for away_team in teams:
if home_team == away_team:
winner, proba = home_team, 1
else:
winner, proba = predict_winner(games, home_team, away_team, model)
# print("Prediction: {} will win with {}% probability.".format(winner, proba * 100))
if winner != home_team:
proba = 1 - proba
home_list.append({away_team: proba})
data = {home_team : home_list}
if is_home_team_exist(table, home_team):
update_item(table, data)
else:
insert_item(table, data)
print("Complete")
# print(data)
# end = time.time()
# print(end - start)