def __init__(self, gm, RoP): self.game = Game(gm, RoP) record, rot, bxs = self.game.preprocess(load=1) self.record = record self.rot = rot self.bxs = bxs self.dist = [ ' 0~ 5ft 2PT', ' 5~10ft 2PT', ' 10~15ft 2PT', ' 15~20ft 2PT', ' >20ft 2PT', ' <=25ft 3PT', ' 25~30ft 3PT', ' 30~35ft 3PT', ' 35~40ft 3PT', ' >40ft 3PT', ' restricted(RA)', ' paint(Non-RA)', ' mid range', ' left corner 3', ' right corner 3', ' above the break 3' ]
def detectgame(self, gm, playeres, season=0): game = Game(gm[:-7], 'playoff' if self.RoP else 'regular') for qtr in range(3, game.quarters): for ply in game.yieldPlay(qtr): play = Play(ply, qtr) if play.time() <= MPTime(self.lm): # 在规定时间内 rec, ind = play.record() s = play.score(ind=ind) if s and play.diffbeforescore(s) <= self.dp: try: p = self.pm2pn[rec.split(' ')[0]] except KeyError: p = rec.split(' ')[0] if season: p += ' %d_%d' % (season, season + 1) if p not in playeres: playeres[p] = np.zeros((1, len(self.columns))) if 'makes' in rec: # 得分增加命中数 playeres[p][0, 0] += s playeres[p][0, s * 3 - 1] += 1 if 'assist' in rec: # 受助攻 playeres[p][0, s + 11] += 1 playeres[p][0, 15] += 1 try: astp = self.pm2pn[rec.split(' ')[-1][:-1]] except: astp = rec.split(' ')[-1][:-1] if season: astp += ' %d_%d' % (season, season + 1) if astp not in playeres: playeres[astp] = np.zeros( (1, len(self.columns))) playeres[astp][0, s + 14] += 1 playeres = self.onlycountonce( astp, gm[:-7], playeres) playeres[p][0, s * 3] += 1 # 得分或投失增加出手数 playeres = self.onlycountonce(p, gm[:-7], playeres) # if 'LeBron James' in p and season == 2019: # print(s, play.play, gm[:-7], self.plyrcgs['LeBron James 2019_2020']) return playeres
def inline(r): line = r['R'][RoH] return sorted(line, reverse=True) lines_all = LoadPickle('./data/Lineups/anaSeason5Lineups.pickle') for season in range(2020, 2021): ss = '%d_%d' % (season, season + 1) for i in range(2): lines_all[i][ss] = {} lines = { } # {'tm1': {'line1': ['sum_time', '+/-'], 'line2': ['sum_time', '+/-'] ...}, 'tm2': ...} gms = os.listdir('D:/sunyiwu/stat/data/seasons/%s/%s/' % (ss, regularOrPlayoffs[i])) for gm in tqdm(gms): game = Game(gm, regularOrPlayoffs[i]) record = LoadPickle(gameMarkToDir(gm, regularOrPlayoffs[i], tp=3)) rot = game.rotation(record) bxs, rot = game.replayer(record, rot) et = '%d:00.0' % (48 + 5 * (game.quarters - 4)) for tmix, tm in enumerate(list(game.bxscr[0])): # 分别回溯两支球队 if tm not in lines: lines[tm] = {} RoH = list(game.bxscr[0]).index(tm) for ix, r in enumerate(rot): line = inline(r) if ' '.join(line) not in lines[tm]: lines[tm][' '.join(line)] = [ MPTime('0:00.0'), 0, [np.zeros((21, )), np.zeros((21, ))]
# ================ 球员单场数据准备 ================ mpsum += mp pts = float(sgm['PTS']) fta, ft, fga, fg, tpa, tp = float( sgm['FTA']), float(sgm['FT']), float( sgm['FGA']), float(sgm['FG']), float( sgm['3PA']), float(sgm['3P']) drb, orb, trb = float(sgm['DRB']), float( sgm['ORB']), float(sgm['TRB']) ast, stl, blk, tov, pf = float( sgm['AST']), float(sgm['STL']), float( sgm['BLK']), float(sgm['TOV']), float( sgm['PF']) # ================ 球员本队数据准备 ================ gm = sgm['Playoffs' if i else 'Date'].values[0] game = Game(gm, RoF[i]) tm_gametime = 48 if game.quarters == 4 else 48 + 5 * ( game.quarters - 4) plyrs = game.teamplyrs() ts = 0 if pm in plyrs[0] else 1 os = 0 if ts else 1 ttl = [ game.bxscr[1][0][-1], game.bxscr[1][1][-1] ] tm_ast, tm_fg = int(ttl[ts][14]), int( ttl[ts][2]) # 0.5 * ((Tm FGA + 0.4 * Tm FTA - 1.07 * (Tm ORB / (Tm ORB + Opp DRB)) * (Tm FGA - Tm FG) + Tm TOV) + # (Opp FGA + 0.4 * Opp FTA - 1.07 * (Opp ORB / (Opp ORB + Tm DRB)) * (Opp FGA - Opp FG) + Opp TOV)) tm_poss = int(ttl[ts][3]) + 0.4 * int( ttl[ts][9]) - 1.07 * ( int(ttl[ts][11]) /
[MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0')], [MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0')]], [[MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0')], [MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0')], [MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0'), MPTime('0:00.0')]]] average_time = [[['', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '']], [['', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '']]] count_score = np.zeros((2, 3, 9)) ss = '%d_%d' % (season, season + 1) # print(ss) for i in range(2): # 分别统计常规赛、季后赛 gms = os.listdir('D:/sunyiwu/stat/data/seasons/%s/%s/' % (ss, regularOrPlayoffs[i])) for gm in tqdm(gms): count_games[i] += 1 # print('\t\t\t' + gm) game = Game(gm[:-7], regularOrPlayoffs[i]) gameplyrs = game.teamplyrs() record = LoadPickle(gameMarkToDir(gm[:-7], regularOrPlayoffs[i], tp=3)) # _, _, _, record = game.game_scanner(gm[:-7]) zoom = 0 bp = -1 pm = '' for rec in record: if zoom: # 处于抢断/失误观察期内 # 出现得失分 if 'MK' in rec or 'MS' in rec: GoM = 1 if 'MK' in rec else 0 KoS = 'MK' if GoM else 'MS' plyr_side = 0 if pm in gameplyrs[0] else 1 if tar_item: plyr_side = 0 if plyr_side else 1
#!/usr/bin/python # -*- coding:utf8 -*- from klasses.Game import Game from klasses.miscellaneous import MPTime gm = '200102280DEN' game = Game(gm, 'regular') # print(game.teamplyrs()) _, _, _, record = game.game_scanner(gm) for i in record: # if 'MK' in i or 'MS' in i: print(i) # game.game_analyser(gm, record) # game.pace(gm, record) game.find_time_series(gm, record) # a = MPTime('47:36.1') # b = MPTime('47:36.0') # print(a >= b) # True
#!/usr/bin/python # -*- coding:utf8 -*- from klasses.Game import Game from klasses.Player import Player from util import LoadPickle, playerMarkToDir, writeToPickle, gameMarkToSeason from stats_nba.Game_nba import Game_nba import pandas as pd import numpy as np np.set_printoptions(suppress=True) pd.options.display.expand_frame_repr = False pd.options.display.width = 50 RoF = 0 gm = '201201070NJN' game = Game(gm, 'playoff' if RoF else 'regular') record = game.game_scanner() # for i in record: # print(i) record = game.game_analyser(record) record = game.game_analyser(record, T=1) ss = gameMarkToSeason(gm) rof = 'playoff' if RoF else 'regular' season_dir = 'D:/sunyiwu/stat/data/seasons_scanned/%s/%s/' % (ss, rof) writeToPickle(season_dir + gm + '_scanned.pickle', record) for i in record: print(i) # print() game.find_time_series(record) game.start_of_quarter(record)
inter = 600 # 设置按时间归并区间的间隔 inter_s = inter // 10 whole = 34800 # 整场比赛时间(双加时为止) max_diff = 40 diff_inter = 5 # 1、2、3分 x 命中数、出手数 x -40~40分差 x 间隔统计时间 shoots = np.zeros((3, 2, max_diff * 2 // diff_inter + 1, whole // inter)) prctg = np.zeros((3, max_diff * 2 // diff_inter + 1, whole // inter)) for season in player.yieldSeasons(): for gameInf in player.yieldGames(season): # score += int(gameInf[26]) gm = gameInf[1] team = gameInf[3] op = gameInf[5] game = Game(gm, ROP, team, op) for qtr in range(game.quarters): for playInf in game.yieldPlay(qtr): play = Play(playInf, qtr, pm=pm, HOA=game.HOA) if play.playRecord() and play.teamPlay(): if play.playerAst(): # 助攻 asts += 1 elif play.playerMadeShoot(): # 命中投篮 s, d, t = score_diff_time(play, game, max_diff) d -= s # 关注的是球员得分前的分差 score += s t = tailTime(t, inter_s) d = groupDiff(d + 40, max_diff, diff_inter) shoots[s - 1, 0, d, t] += 1 shoots[s - 1, 1, d, t] += 1