James McCool commited on
Commit
d0e6684
·
1 Parent(s): 73d2fb0

Refactor large_field_preset function to streamline player removal logic by eliminating unnecessary iterations and simplifying the exposure tracking process. Ensure sufficient lineups are generated before finalizing selections, enhancing overall efficiency and accuracy.

Browse files
Files changed (1) hide show
  1. global_func/large_field_preset.py +14 -63
global_func/large_field_preset.py CHANGED
@@ -4,73 +4,24 @@ def large_field_preset(portfolio: pd.DataFrame, lineup_target: int, exclude_cols
4
 
5
  for slack_var in range(1, 20):
6
  concat_portfolio = pd.DataFrame(columns=portfolio.columns)
7
-
8
- # Define player columns (columns that contain player names)
9
- player_columns = [col for col in portfolio.columns if col not in exclude_cols]
10
-
11
- # Track players to remove across iterations
12
- remove_list = []
13
-
14
- # Iterate until no high-exposure players are found
15
- max_iterations = 10 # Prevent infinite loops
16
- for iteration in range(max_iterations):
17
- concat_portfolio = pd.DataFrame(columns=portfolio.columns)
18
 
19
- for team in portfolio['Stack'].unique():
20
- rows_to_drop = []
21
- working_portfolio = portfolio.copy()
22
-
23
- # Remove players from previous iteration if any
24
- if remove_list:
25
- remove_mask = working_portfolio[player_columns].apply(
26
- lambda row: not any(player in list(row) for player in remove_list), axis=1
27
- )
28
- working_portfolio = working_portfolio[remove_mask]
29
-
30
- working_portfolio = working_portfolio[working_portfolio['Stack'] == team].sort_values(by='Finish_percentile', ascending = True)
31
- working_portfolio = working_portfolio.reset_index(drop=True)
32
-
33
- if len(working_portfolio) == 0:
34
- continue
35
-
36
- curr_own_type_max = working_portfolio.loc[0, 'Own'] + (slack_var / 20 * working_portfolio.loc[0, 'Own'])
37
 
38
- for i in range(1, len(working_portfolio)):
39
- if working_portfolio.loc[i, 'Own'] > curr_own_type_max:
40
- rows_to_drop.append(i)
41
- else:
42
- curr_own_type_max = working_portfolio.loc[i, 'Own'] + (slack_var / 20 * working_portfolio.loc[i, 'Own'])
43
-
44
- working_portfolio = working_portfolio.drop(rows_to_drop).reset_index(drop=True)
45
- concat_portfolio = pd.concat([concat_portfolio, working_portfolio])
46
-
47
- # Check player exposure
48
- if len(concat_portfolio) == 0:
49
- break
50
-
51
- player_exposure = {}
52
- for col in player_columns:
53
- for player in concat_portfolio[col].unique():
54
- if pd.notna(player): # Skip NaN values
55
- player_mask = concat_portfolio[player_columns].apply(
56
- lambda row: player in list(row), axis=1
57
- )
58
- exposure = player_mask.sum() / len(concat_portfolio)
59
- player_exposure[player] = exposure
60
-
61
- # Find players with exposure > 35%
62
- high_exposure_players = [player for player, exposure in player_exposure.items() if exposure > 0.35]
63
 
64
- # If no high-exposure players, we're done
65
- if not high_exposure_players:
66
- break
67
-
68
- # Add high-exposure players to remove list
69
- remove_list.extend(high_exposure_players)
70
- remove_list = list(set(remove_list)) # Remove duplicates
71
 
72
- # Check if we have enough lineups and no high-exposure players
73
- if len(concat_portfolio) >= lineup_target and len(high_exposure_players) == 0:
74
  return concat_portfolio.sort_values(by='Finish_percentile', ascending=True).head(lineup_target)
75
 
76
  return concat_portfolio.sort_values(by='Finish_percentile', ascending=True)
 
4
 
5
  for slack_var in range(1, 20):
6
  concat_portfolio = pd.DataFrame(columns=portfolio.columns)
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ for team in portfolio['Stack'].unique():
9
+ rows_to_drop = []
10
+ working_portfolio = portfolio.copy()
11
+ working_portfolio = working_portfolio[working_portfolio['Stack'] == team].sort_values(by='Finish_percentile', ascending = True)
12
+ working_portfolio = working_portfolio.reset_index(drop=True)
13
+ curr_own_type_max = working_portfolio.loc[0, 'Own'] + (slack_var / 20 * working_portfolio.loc[0, 'Own'])
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ for i in range(1, len(working_portfolio)):
16
+ if working_portfolio.loc[i, 'Own'] > curr_own_type_max:
17
+ rows_to_drop.append(i)
18
+ else:
19
+ curr_own_type_max = working_portfolio.loc[i, 'Own'] + (slack_var / 20 * working_portfolio.loc[i, 'Own'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ working_portfolio = working_portfolio.drop(rows_to_drop).reset_index(drop=True)
22
+ concat_portfolio = pd.concat([concat_portfolio, working_portfolio])
 
 
 
 
 
23
 
24
+ if len(concat_portfolio) >= lineup_target:
 
25
  return concat_portfolio.sort_values(by='Finish_percentile', ascending=True).head(lineup_target)
26
 
27
  return concat_portfolio.sort_values(by='Finish_percentile', ascending=True)