Skip to content

Instantly share code, notes, and snippets.

@evmcheb
Created April 9, 2020 04:08
Show Gist options
  • Save evmcheb/c50df022fcafc4f49a59445156f2c67f to your computer and use it in GitHub Desktop.
Save evmcheb/c50df022fcafc4f49a59445156f2c67f to your computer and use it in GitHub Desktop.
import os
import numpy as np
from tqdm import tqdm
from pymongo import MongoClient
bookies = {
"44":"Betfair",
"16":"bet365",
"18":"Pinnacle",
"5":"Unibet"
}
client = MongoClient()
db = client.nba
games = db.games
seasons = [f"002{str(i).zfill(2)}" for i in range(8, 20)]
# load the samples into memory
x, y = [], []
def normalize_sample(nparr):
for feature in range(nparr.shape[-1]): # iterate over the features
f = np.nan_to_num(nparr[:, :, feature])
nparr[:, :, feature] = (f-f.min())/(f.max()-f.min())
return nparr
for season in seasons:
for filename in tqdm(os.listdir(f"samples/{season}")[120:]):
if ".npy" in filename:
game = list(games.find({"GAME_ID":filename.strip(".npy"), "bet365":{"$exists":"True"}}))
if not game:
continue
game = game[0]
closing_odds = 1/float(game["bet365"].split()[1].split("v")[0])
home_win = int(game["HOME"] == game["WINNER"])
sample = np.load(f"samples/{season}/{filename}")
x.append((normalize_sample(sample), closing_odds))
y.append(home_win)
x = np.array(x)
y = np.array(y)
import random
print(x.shape, y.shape)
diff = len(y)//2 - np.count_nonzero(y == 0)
for i in tqdm(range(diff)):
while True:
a = random.randint(1, len(y)-1)
if y[a] == 1:
x = np.delete(x, a, 0)
y = np.delete(y, a, 0)
break
print(len(x), len(y))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment