James McCool commited on
Commit
02245bb
·
1 Parent(s): fbbb0e8

Add team ownership calculation in hedging_preset.py to enhance lineup generation. Introduced a new function to sum hitter ownership by team and adjusted lineup target calculations for improved accuracy. This change allows for better team-based lineup strategies.

Browse files
Files changed (1) hide show
  1. global_func/hedging_preset.py +32 -2
global_func/hedging_preset.py CHANGED
@@ -2,6 +2,7 @@ import pandas as pd
2
  import math
3
  from global_func.small_field_preset import small_field_preset
4
  from global_func.large_field_preset import large_field_preset
 
5
 
6
  def hedging_preset(portfolio: pd.DataFrame, lineup_target: int, projections_file: pd.DataFrame):
7
 
@@ -12,6 +13,28 @@ def hedging_preset(portfolio: pd.DataFrame, lineup_target: int, projections_file
12
  check_own_df = check_own_df.sort_values(by='ownership', ascending=False)
13
  top_owned = check_own_df['player_names'].head(list_size).tolist()
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  concat_portfolio = pd.DataFrame(columns=portfolio.columns)
16
 
17
  for player in top_owned:
@@ -33,9 +56,16 @@ def hedging_preset(portfolio: pd.DataFrame, lineup_target: int, projections_file
33
  locked_df = working_df[lock_mask]
34
  print(locked_df.head(10))
35
 
36
- removed_lineups = small_field_preset(removed_df, math.ceil(lineup_target / (list_size * 2)), excluded_cols)
37
- locked_lineups = large_field_preset(locked_df, math.ceil(lineup_target / (list_size * 2)), excluded_cols)
38
 
39
  concat_portfolio = pd.concat([concat_portfolio, removed_lineups, locked_lineups])
 
 
 
 
 
 
 
40
 
41
  return concat_portfolio.head(lineup_target)
 
2
  import math
3
  from global_func.small_field_preset import small_field_preset
4
  from global_func.large_field_preset import large_field_preset
5
+ from global_func.get_portfolio_names import trim_portfolio
6
 
7
  def hedging_preset(portfolio: pd.DataFrame, lineup_target: int, projections_file: pd.DataFrame):
8
 
 
13
  check_own_df = check_own_df.sort_values(by='ownership', ascending=False)
14
  top_owned = check_own_df['player_names'].head(list_size).tolist()
15
 
16
+ def get_team_hitter_ownership(projections_file: pd.DataFrame):
17
+ """
18
+ Calculate the sum ownership of hitters on each team.
19
+ Excludes SP and P positions and sums ownership by team.
20
+
21
+ Args:
22
+ projections_file (pd.DataFrame): DataFrame with 'position', 'team', and 'ownership' columns
23
+
24
+ Returns:
25
+ pd.Series: Series with team names as index and total hitter ownership as values, sorted descending
26
+ """
27
+ # Filter out pitchers (SP and P positions)
28
+ hitters_df = projections_file[~projections_file['position'].isin(['P', 'SP'])]
29
+
30
+ # Group by team and sum ownership
31
+ team_ownership = hitters_df.groupby('team')['ownership'].sum().sort_values(ascending=False)
32
+
33
+ return team_ownership
34
+
35
+ team_ownership = get_team_hitter_ownership(projections_file)
36
+ top_owned_teams = team_ownership.head(list_size).index.tolist()
37
+
38
  concat_portfolio = pd.DataFrame(columns=portfolio.columns)
39
 
40
  for player in top_owned:
 
56
  locked_df = working_df[lock_mask]
57
  print(locked_df.head(10))
58
 
59
+ removed_lineups = small_field_preset(removed_df, math.ceil(lineup_target / (list_size * 3)), excluded_cols)
60
+ locked_lineups = large_field_preset(locked_df, math.ceil(lineup_target / (list_size * 3)), excluded_cols)
61
 
62
  concat_portfolio = pd.concat([concat_portfolio, removed_lineups, locked_lineups])
63
+
64
+ for team in top_owned_teams:
65
+ working_df = portfolio.copy()
66
+ teams_df = working_df[working_df['Stack'] == team]
67
+
68
+ team_lineups = large_field_preset(teams_df, math.ceil(lineup_target / (list_size * 3)), excluded_cols)
69
+ concat_portfolio = pd.concat([concat_portfolio, team_lineups])
70
 
71
  return concat_portfolio.head(lineup_target)