主队的进球数在y轴上,客队的进球数在x轴上。分数线0-0例如是1.21,分数线4-3是0.84。我知道主队获胜的可能性等于
np.sum(np.tril(match_score_matrix, -1))
开奖的概率等于:
np.sum(np.diag(match_score_matrix))
损失的概率等于:
np.sum(np.triu(match_score_matrix, 1)),
现在,我想知道每个目标差异的概率。在此矩阵中,以下目标差异结果是可能的[-6,-5,...,0,...,15)。我该如何编写一个循环来计算每个结果的可能性?
def get_probabilities(match_score_matrix, max_goals_home, max_goals_away):
return dict({'max_goals_away': np.something,
'-5', np.something,
'-4', np.something,
...
'0', np.diag(match_score_matrix)),
'1', np.something
...
'max_goals_home', np.something })
如何在一个易于使用的循环中编写此代码?预先谢谢你!
答案 0 :(得分:0)
您可以使用np.diag
提取第k
条对角线,然后将其求和。
{str(i):np.sum(np.diag(match_score_matrix,k=i)) for i in range(-15,8)}
答案 1 :(得分:0)
考虑在np.diagonal
中使用 offset 。因为对角线是主队和客队之间的进球数相等时,所以当客队比主队高一个进球时,向上偏移一个概率。相反,当主队比客队高出一个目标时,向下偏移一个概率。因此,将两个概率相加。
# AWAY ONE GOAL HIGHER
np.sum(np.diagonal(match_score_matrix, offset=1))
# HOME ONE GOAL HIGHER
np.sum(np.diagonal(match_score_matrix, offset=-1))
# AWAY TWO GOALS HIGHER
np.sum(np.diagonal(match_score_matrix, offset=2))
# HOME TWO GOALS HIGHER
np.sum(np.diagonal(match_score_matrix, offset=-2))
...
# AWAY MAX GOALS HIGHER USING array.shape
np.sum(np.diagonal(match_score_matrix, offset=match_score_matrix.shape[0]))
# HOME MAX GOALS HIGHER USING array.shape
np.sum(np.diagonal(match_score_matrix, offset=-match_score_matrix.shape[0]))
对于所需的字典,请使用字典理解
def get_probabilities(match_score_matrix, max_goals_home, max_goals_away):
# DICTIONARY COMPREHENSION
return {str(i): np.sum(np.diagonal(match_score_matrix, offset=i)) for i in range(-15,15)}