Home > Mobile >  Is there a faster way to loop through numpy matrix
Is there a faster way to loop through numpy matrix

Time:08-06

have been trying on how to calculate poisson probabilities for both teams to score, but they don't add up to zero when add up possible scorelines and having a range of 0 to 6 isn't even enough.

from scipy.stats import poisson
import numpy as np
home_goals_vector = poisson(1.23).pmf(np.arange(0, 6))
away_goals_vector = poisson(2.23).pmf(np.arange(0, 6))
print(home_goals_vector)
m = np.outer(home_goals_vector, away_goals_vector)
print(m[1, 1]   m[2, 1]   m[3, 1]   m[4, 1]   m[1, 2]   m[2, 2]   m[2, 3]   m[2, 4]   m[3, 2]   m[3, 3]   m[3, 4]   m[4, 1]   m[4, 2]   m[4, 4]   m[1, 3]   m[1, 4])
print(np.sum(m))

Now, I know the variable m contains the possible scores and the probabilities, is there a faster way to do this cause I need all possible scores so that scorelines that both have no zero and scorelines that do have zero, if i add up their probabilities I get 100%

CodePudding user response:

After some deliberation in the comments, it seems that you are wanting to exclude the zeroeth row and column from the summation over the outer product of the two probability vectors.

This can be done by substituting np.sum(m) for np.sum(m[1:, 1:]) in your code example. This is an example of using NumPy's slicing notation, which is similar to Matlab's slicing notation (by design).

  • Related