Skip to content

Commit 20d0c23

Browse files
committed
add plots on team model parameter changes since start of season
1 parent f7c73f5 commit 20d0c23

File tree

1 file changed

+106
-2
lines changed

1 file changed

+106
-2
lines changed

notebooks/team_model_bpl_next.ipynb

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,116 @@
299299
"plt.title(team_a);"
300300
]
301301
},
302+
{
303+
"cell_type": "markdown",
304+
"metadata": {},
305+
"source": [
306+
"### Change since start of season"
307+
]
308+
},
309+
{
310+
"cell_type": "code",
311+
"execution_count": null,
312+
"metadata": {},
313+
"outputs": [],
314+
"source": [
315+
"model_team = get_fitted_team_model(CURRENT_SEASON, 1, session)\n",
316+
"select_idx = jnp.array([list(model_team.teams).index(t) for t in CURRENT_TEAMS], dtype=int)\n",
317+
"a_mean_1 = model_team.attack.mean(axis=0)[select_idx]\n",
318+
"b_mean_1 = model_team.defence.mean(axis=0)[select_idx]\n",
319+
"\n",
320+
"model_team = get_fitted_team_model(CURRENT_SEASON, NEXT_GAMEWEEK, session)\n",
321+
"select_idx = jnp.array([list(model_team.teams).index(t) for t in CURRENT_TEAMS], dtype=int)\n",
322+
"a_mean_2 = model_team.attack.mean(axis=0)[select_idx]\n",
323+
"b_mean_2 = model_team.defence.mean(axis=0)[select_idx]"
324+
]
325+
},
326+
{
327+
"cell_type": "code",
328+
"execution_count": null,
329+
"metadata": {},
330+
"outputs": [],
331+
"source": [
332+
"fig, ax = plt.subplots(1, 1, figsize=(9, 9))\n",
333+
"ax.set_aspect(\"equal\")\n",
334+
"\n",
335+
"for a_1, b_1, a_2, b_2 in zip(a_mean_1, b_mean_1, a_mean_2, b_mean_2, strict=True):\n",
336+
" plt.arrow(\n",
337+
" a_1,\n",
338+
" b_1,\n",
339+
" a_2 - a_1,\n",
340+
" b_2 - b_1,\n",
341+
" width=0.005,\n",
342+
" length_includes_head=True,\n",
343+
" )\n",
344+
"plt.xlabel(\"attack\", fontsize=14)\n",
345+
"plt.ylabel(\"defence\", fontsize=14)\n",
346+
"\n",
347+
"for idx, team in enumerate(CURRENT_TEAMS):\n",
348+
" #ax.annotate(team, (a_mean_2[idx] - 0.03, b_mean_2[idx] + 0.02), fontsize=12)\n",
349+
" ax.annotate(team, (a_mean_2[idx] - 0.02, b_mean_2[idx] + 0.005), fontsize=12)"
350+
]
351+
},
302352
{
303353
"cell_type": "code",
304354
"execution_count": null,
305355
"metadata": {},
306356
"outputs": [],
307-
"source": []
357+
"source": [
358+
"a_mean = np.full((len(CURRENT_TEAMS), NEXT_GAMEWEEK), np.nan)\n",
359+
"b_mean = np.full((len(CURRENT_TEAMS), NEXT_GAMEWEEK), np.nan)\n",
360+
"\n",
361+
"for gw in range(1, NEXT_GAMEWEEK + 1):\n",
362+
" model_team = get_fitted_team_model(CURRENT_SEASON, gw, session)\n",
363+
" select_idx = jnp.array(\n",
364+
" [list(model_team.teams).index(t) for t in CURRENT_TEAMS], dtype=int\n",
365+
" )\n",
366+
" a_mean[:, gw - 1] = model_team.attack.mean(axis=0)[select_idx]\n",
367+
" b_mean[:, gw - 1] = model_team.defence.mean(axis=0)[select_idx]"
368+
]
369+
},
370+
{
371+
"cell_type": "code",
372+
"execution_count": null,
373+
"metadata": {},
374+
"outputs": [],
375+
"source": [
376+
"fig = plt.figure(figsize=(8, 8))\n",
377+
"for a_team, b_team, team in zip(a_mean, b_mean, CURRENT_TEAMS, strict=True):\n",
378+
" plt.plot(a_team, b_team, marker=\"o\", label=team)\n",
379+
" plt.annotate(team, (a_team[-1] - 0.02, b_team[-1] + 0.01), fontsize=12)\n",
380+
"plt.xlabel(\"Attack\")\n",
381+
"plt.ylabel(\"Defence\")\n",
382+
"plt.axis(\"equal\")\n",
383+
"fig.legend(loc=\"outside lower center\", ncol=len(CURRENT_TEAMS) / 4, bbox_to_anchor=(0.5, -0.07))"
384+
]
385+
},
386+
{
387+
"cell_type": "code",
388+
"execution_count": null,
389+
"metadata": {},
390+
"outputs": [],
391+
"source": [
392+
"fig, ax = plt.subplots(1, 2, figsize=(16, 6), sharex=True)\n",
393+
"for idx, team in enumerate(CURRENT_TEAMS):\n",
394+
" ax[0].plot(\n",
395+
" np.arange(1, NEXT_GAMEWEEK + 1),\n",
396+
" a_mean[idx],\n",
397+
" marker=\"o\",\n",
398+
" label=team,\n",
399+
" )\n",
400+
" ax[1].plot(\n",
401+
" np.arange(1, NEXT_GAMEWEEK + 1),\n",
402+
" b_mean[idx],\n",
403+
" marker=\"o\",\n",
404+
" label=team,\n",
405+
" )\n",
406+
"ax[0].set_xlabel(\"Gameweek\")\n",
407+
"ax[1].set_xlabel(\"Gameweek\")\n",
408+
"ax[0].set_title(\"Attack\")\n",
409+
"ax[1].set_title(\"Defence\")\n",
410+
"fig.legend(loc=\"outside lower center\", ncol=len(CURRENT_TEAMS) / 2, bbox_to_anchor=(0.5, -0.15))\n"
411+
]
308412
}
309413
],
310414
"metadata": {
@@ -323,7 +427,7 @@
323427
"name": "python",
324428
"nbconvert_exporter": "python",
325429
"pygments_lexer": "ipython3",
326-
"version": "3.12.7"
430+
"version": "3.12.11"
327431
}
328432
},
329433
"nbformat": 4,

0 commit comments

Comments
 (0)