# How to insert two features in a model when a feature only applies to a certain group in the model

I'm building a machine learning model in Python to predict soccer player values. Consider the following feature columns of the dataframe:

         [features]
---------------------------------
position | goals | goals_conceded
-------- |-------|---------------
Forward  |  23   |     NaN
Defender |   2   |     NaN
Defender |   4   |     NaN
Keeper   |   NaN |     20
Keeper   |   NaN |     43


Since keepers don't usually score goals, they'll almost always have null values in the "goals" column, but they still can have this statistic, so it would be fine to fill the NaNs with 0. On the other hand, since line players can't have "goals_conceded" stats, they'll also have null values in that column, but in this case, players will never have this statistic, since this is a keeper only stat. How do I build a machine learning model considering these two columns as features?

I thought about putting them together in one single column, but that can't happen since for a line player, the more goals he makes the better it is. For goalkeepers it's the opposite, the less goals he conceeds the better. I also can't fill the columns with zeros since it would affect the model prediction in the "goals_conceded" column for example, since 98% of the rows contain in line players info.

This happens with many of the columns in my dataframe, such as "clean sheets" (only Keepers will have this stat) and "shots at target" (only line players will have this stat). How do I deal with them?

• Looks like the actual problem is that classes are unbalanced. But theses columns aren't the ones you are trying to predict, are they? – Pedro Henrique Monforte Aug 16 '20 at 6:28
• Nope, I want to predict a column that contains the player value in euros, using these columns as features for my model. – Caldass_ Aug 16 '20 at 7:29

To me the data in the current form seems wrong fro training a model for all players, it is like trying to tell whether an apple is better than a tennis ball. They have completely different characteristics.

what you could do instead is group players with similar features in different sets and train the models to predict their scores relevant to their feature set. So for example goalkeepers will be compared against other goalkeepers and assigned scores accordingly. After which you need to set a baseline value for each set and scale the scores for the different classes accordingly.

• So you're saying I should separate in line players from goalkeepers so I would have a dataframe with only line players and another one with only goalkeepers? – Caldass_ Aug 16 '20 at 18:31
• Precisely. In fact why just goalkeepers and players, you could have a category for defenders as well because forwards might have better on paper stats compared to defenders. Once you've trained your models on these individual classes, all that remains is standardization – tehem Aug 17 '20 at 3:12

Remember that any model will have the relationships between the features (X) and the label (y), so your ideas are correct. What is better will depend on your data, so try both, and try more ideas if you can. The best model will be the one that generalize better for new data, so take the final decision with data.

Please, note that your features are at least 3, goals, goals_conceded and position, so this last will help with the problem.

And I suggest not to fill NaN with 0, but -1 (or any other imposible/invalid integer number), because a newbie player will have 0 in the feature(s), and it's correct, and you want to identificate this.

• Let me see if I got you: 1 - You're saying that by considering the players position (and also lots of other features) this problem is already solved, so putting the "goals_conceded" and the "goals" together would be fine since there are other features that the model can use to be trained correctly? 2- Instead of filling the keeper goals NaNs with 0, filling with -1? If that's what you mean, shouldn't I still fill with 0 since keepers can still score goals, it's rare but it happens. Plus, there are keepers that are the official penalty takers and freekick takers, etc. – Caldass_ Aug 16 '20 at 18:44

Its an interesting question. In this case I am doubting if training data is even correct. Because two different observations are themselves not comparable. For a ML model to say or understand how two observations are different they both should be comparable. Its a different thing that one value is unknown and one value does not apply to data.

Note: I don't know if below is valid because I am not very knowledgeable in soccer. There may a different way to look at this problem. Since Forwarder can also be assigned as a keeper. (No one will do it since he may not be good at it but he can still play as a keeper). So in your data the forwarder simply never conceded a goal so you may replace NaN with 0.

• Interesting point, but I don't think it would work because if a foward has 0 conceded goals, and (because of other stats) he is worth only 600k, a keeper with those stats should be worth a lot. Although, the same thing would happen with valuable players, they would be worth a lot and still have 0 conceded goals. I honestly can't tell if this could be applied. – Caldass_ Aug 16 '20 at 18:35
• What you said makes sense. forward goal_conceded should not be replaced with 0. I guess making different model for forward, defender and goal keepers will be better as @tehem suggested – Hitesh Somani Aug 17 '20 at 7:19