4
$\begingroup$

I have some soccer outcomes for each league round to import in a data form similar to that:

results={{round1,{{team1,team2,4,0},{team3,team4,2,1}}},{round2,{{team2,team3,2,4}, {team4,team1,1,3}}},{round3,{{team1,team3,0,1},{team4,team2,2,2}}}};

I should build a temporany rank matrix after each round with the following data (here the example for after the second round):

{{team,home,hgscored,hgconceded,agscored,agconceded},{team1,1,4,0,1,3,1},{team2,1,2,4,1,0,4},{team3,1,2,1,1,4,2},{team4,1,1,3,1,1,2}}

to take in order to derive a vector with, for instance, the following outcome percentage for the next round:

forecast={{T1,T2,1,X,2},{team1,team3,54%,27%,19%},{team4,team2,42%,24%,42%}};

and iteratively comparise forecast vector prediction with the related round played whose outcomes are in results vector, collecting everithing on a vector in order to test the model: how many events were correctly predicted by forecast?

$\endgroup$

1 Answer 1

2
$\begingroup$

UPDATED

Patterns are the key to this one.

Just pick off the games themselves from the results.

games = Partition[Flatten@results[[All, 2]], 4]

(* {{team1, team2, 4, 0}, {team3, team4, 2, 1}, {team2, team3, 2, 4}, 
    {team4, team1, 1, 3}, {team1, team3, 0, 1}, {team4, team2, 2, 2}} *)

Define this pattern and the replacement right hand side. The th is "team home" ,etc. It parses out the home and away teams and sets up the data. I didn't know what your home meant, so I just generate records with 5 fields.

patt = {th_, ta_, ths_, tas_} :> {{th, 1, ths, tas, 0, 0, 0}, {ta, 0, 0, 0, 1, tas, ths}}

Run it over the games

res = Cases[games, patt]

(* {{{team1, 1, 4, 0, 0, 0, 0}, {team2, 0, 0, 0, 1, 0, 4}}, 
    {{team3, 1, 2, 1, 0, 0, 0}, {team4, 0, 0, 0, 1, 1, 2}}, 
    {{team2, 1, 2, 4, 0, 0, 0}, {team3, 0, 0, 0, 1, 4, 2}},
    {{team4, 1, 1, 3, 0, 0, 0}, {team1, 0, 0, 0, 1, 3, 1}}, 
    {{team1, 1, 0, 1, 0, 0, 0}, {team3, 0, 0, 0, 1, 1, 0}},
    {{team4, 1, 2, 2, 0, 0, 0}, {team2, 0, 0, 0, 1, 2, 2}}} *)

Do a GatherBy on team names after flattening things out.

res2 = GatherBy[Partition[Flatten@res, 7], First];

(* {{{team1, 1, 4, 0, 0, 0, 0}, {team1, 0, 0, 0, 1, 3, 1}, {team1, 1, 0,  1, 0, 0, 0}}, 
    {{team2, 0, 0, 0, 1, 0, 4}, {team2, 1, 2, 4, 0, 0, 0}, {team2, 0, 0, 0, 1, 2, 2}}, 
    {{team3, 1, 2, 1, 0, 0, 0}, {team3, 0, 0, 0, 1, 4, 2}, {team3, 0, 0, 0, 1, 1, 0}}, 
    {{team4, 0, 0, 0, 1, 1, 2}, {team4, 1, 1, 3, 0, 0, 0}, {team4, 1, 2, 2, 0, 0, 0}}} *)

Sum up the values for each team, and label by team.

res3 =Flatten@{First@First@#, Rest[Total[#]]} & /@ res2

(* {{team1, 2, 4, 1, 1, 3, 1}, 
    {team2, 1, 2, 4, 2, 2, 6}, 
    {team3, 1, 2, 1, 2, 5, 2}, 
    {team4, 2, 3, 5, 1, 1, 2}} *)

Join[{{team, home, hgscored, hgconceded, away, agscored, agconceded}},   res3] // TableForm

$$ \left( \begin{array}{ccccccc} \text{team} & \text{home} & \text{hgscored} & \text{hgconceded} & \text{away} & \text{agscored} & \text{agconceded} \\ \text{team1} & 2 & 4 & 1 & 1 & 3 & 1 \\ \text{team2} & 1 & 2 & 4 & 2 & 2 & 6 \\ \text{team3} & 1 & 2 & 1 & 2 & 5 & 2 \\ \text{team4} & 2 & 3 & 5 & 1 & 1 & 2 \\ \end{array} \right) $$

Not sure the details on your analysis of the data, but this gets it in a form you can work with.

$\endgroup$
7
  • $\begingroup$ Thank you MikeY, unfortunately this is not what I looked for. I should need "home" and "away" which are, respectively, the number of the matches played by each team at home and away. I tried to change patt in: patt = {h_, ths_, thc_, a_, tas_, tac_} -> {{h, ths, thc, 0, 0, 0}, {0, 0, 0, a, tas, tas}} but it doesn't work. Moreover, I should build an above-mentioned rank matrix as it was an iterator where each round rank is the sum of it and the previous ones, in order to derive other parameters from it, put in forecast and then comparising results and forecast for each round $\endgroup$
    – Nate
    Commented Feb 25, 2020 at 17:39
  • 1
    $\begingroup$ @Mick, I updated the answer to include the number of home and away games. Still not sure I am following on the vector aspect. $\endgroup$
    – MikeY
    Commented Feb 25, 2020 at 17:51
  • $\begingroup$ MikeY, perfect for home/away, thank you! And from the way to built the rank also, the matrix representation is that I thougth! The only way is that I should need it after the first round, then after the second, then after the third and so on, because I should insert it in a iterative For or Do cicle to derive forecast. I guess that res3` is just for the final rank.. should I change res3 in: res3 =Flatten@{First@First@#, Rest[Total[#]**[[i]]]**} & /@ res2? $\endgroup$
    – Nate
    Commented Feb 25, 2020 at 18:12
  • $\begingroup$ You should be using RuleDelayed in your definition of patt to properly localize Symbols. $\endgroup$
    – Mr.Wizard
    Commented Feb 25, 2020 at 20:08
  • $\begingroup$ Where would not using :> blow up on me? I tried both and got the same answer...not sure. $\endgroup$
    – MikeY
    Commented Feb 25, 2020 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.