1
$\begingroup$

I am working in a ML project where I would like to predict the final result of a soccer game. There are three output categories: Local Win, Tie, Visitor Win. In order to do so, I am using stacking models. I would like to predict first whether a team will win or not (1/0) for both teams, and use that output as input in the final model with the three output categories.

My problem is that I have a database of matches (let's say 400 matches, each one with a match_id), and from there, I form one of teams (with 800 teams, since there are two teams per match, so 2 samples will have the same match_id). I compute 800 outputs, one output (1/0) per team, predicting whether it will win or not.

Once this is done, I would like to go back to my original database, but adding two new columns with the outputs I just predicted. How could I make the two samples with the same match_id be put together again?

The code without Pandas would look like this. For each of the 800 samples whose output has already been predicted, I want to map them with the match they belong to. This is the idea...

for sample in database_samples:
    if samples['match_id'] == matches['match_id']:
       if samples['team_is_local']:
          matches['match_id']['localWins'] = sample['output']
       else:
          matches['match_id']['visitorWins'] = sample['output']

Thank you for your help.

$\endgroup$

1 Answer 1

1
$\begingroup$

Let's assume you have some DataFrame in which there are the match_id and the features, called full_data. You extract the features and call this features_df. When you write predictions = model.predict(features_df), you most likely receive a numpy.array or a pandas.DataFrame, or even a list. You can then just write

full_data["win"] = predictions

and the predictions are added as new column to the DataFrame. The order stays the same when predicting, so you don't have to worry about matching the ID. You can then proceed to do whatever you want to do with that data.

$\endgroup$
4
  • $\begingroup$ Thank you for your answer! That will help me to map the 800 predicitions with the 800 teams samples in the same database including the new columns. After that, how do I map two samples to only one match, so I get my 400 samples matches database with the new output columns? I will edit the final part of the main question to explain better $\endgroup$ Commented Oct 31, 2022 at 12:41
  • $\begingroup$ Do you really need it in that format or may I suggest not using two columns for 'localWins' and 'visitorWins'? When you know that the locals win/lost/had a draw, you know the result for the visitors. You could change this to just have one column called "result" and 0 means the locals won, 1 means they had a draw and 2 means they won, for example. This should make the process in a whole shorter, faster and the data much easier to transform. Is that a solution or do you need these specific formats? $\endgroup$ Commented Nov 1, 2022 at 16:17
  • $\begingroup$ Yes, I would need that format since I am working with stacking models to get the final output (0,1,2) It is like an intermediate step. First computing two outputs, win or not win for each team, and then using them to get the final one (0,1,2). Thank you $\endgroup$ Commented Nov 1, 2022 at 19:23
  • $\begingroup$ Then, could you please provide minimum examples of what your data looks like at every step and what it should look like in the end? We could think of and imagine what the goal is, but explicit is always better than implicit, since there is no room for miscommunication and interpretation. $\endgroup$ Commented Nov 2, 2022 at 20:02

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.