James McCool commited on
Commit
d38df13
·
1 Parent(s): 1b1db4f

Refactor large_field_preset function to streamline the portfolio selection process by removing unnecessary iterations and simplifying the logic for team-based filtering, enhancing efficiency and lineup accuracy.

Browse files
Files changed (1) hide show
  1. global_func/large_field_preset.py +17 -60
global_func/large_field_preset.py CHANGED
@@ -4,67 +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
- player_columns = [col for col in concat_portfolio.columns if col not in concat_portfolio]
9
-
10
- remove_list = []
11
-
12
- max_iterations = 5
13
- for each_iteration in range(max_iterations):
14
- player_stats = []
15
- concat_portfolio = pd.DataFrame(columns=portfolio.columns)
16
-
17
- for team in portfolio['Stack'].unique():
18
- rows_to_drop = []
19
- working_portfolio = portfolio.copy()
20
- if remove_list:
21
- if len(remove_list) > 0:
22
- remove_mask = working_portfolio[player_columns].apply(
23
- lambda row: not any(player in list(row) for player in remove_list), axis=1
24
- )
25
- working_portfolio = working_portfolio[remove_mask]
26
-
27
- working_portfolio = working_portfolio[working_portfolio['Stack'] == team].sort_values(by='Finish_percentile', ascending = True)
28
- working_portfolio = working_portfolio.reset_index(drop=True)
29
-
30
- if len(working_portfolio) == 0:
31
- continue
32
-
33
- curr_own_type_max = working_portfolio.loc[0, 'Own'] + (slack_var / 20 * working_portfolio.loc[0, 'Own'])
34
-
35
- for i in range(1, len(working_portfolio)):
36
- if working_portfolio.loc[i, 'Own'] > curr_own_type_max:
37
- rows_to_drop.append(i)
38
- else:
39
- curr_own_type_max = working_portfolio.loc[i, 'Own'] + (slack_var / 20 * working_portfolio.loc[i, 'Own'])
40
-
41
- working_portfolio = working_portfolio.drop(rows_to_drop).reset_index(drop=True)
42
-
43
- concat_portfolio = pd.concat([concat_portfolio, working_portfolio])
44
-
45
- player_names = set()
46
- for col in concat_portfolio.columns:
47
- if col not in exclude_cols:
48
- player_names.update(concat_portfolio[col].unique())
49
- for player in player_names:
50
- player_mask = concat_portfolio[player_columns].apply(
51
- lambda row: player in list(row), axis=1
52
- )
53
-
54
- if player_mask.any():
55
- player_stats.append({
56
- 'Player': player,
57
- 'Lineup Count': player_mask.sum(),
58
- 'Exposure': player_mask.sum() / len(concat_portfolio),
59
- })
60
- player_exposure = pd.DataFrame(player_stats)
61
- player_exposure = player_exposure[player_exposure['Exposure'] > .50]
62
- remove_list = player_exposure['Player'].tolist()
63
-
64
- if len(remove_list) == 0:
65
- break
66
 
67
- if len(concat_portfolio) >= lineup_target and len(remove_list) == 0:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  return concat_portfolio.sort_values(by='Finish_percentile', ascending=True).head(lineup_target)
69
 
70
  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)