James McCool commited on
Commit
47fd581
·
1 Parent(s): 32cb8bd

Testing some new logic around stratification

Browse files
Files changed (2) hide show
  1. app.py +1 -1
  2. global_func/stratification_function.py +16 -11
app.py CHANGED
@@ -2335,7 +2335,7 @@ if selected_tab == 'Manage Portfolio':
2335
  st.session_state['export_merge'] = st.session_state['export_base'].copy()
2336
  with st.expander('Stratify'):
2337
  with st.form(key='Stratification'):
2338
- sorting_choice = st.selectbox("Stat Choice", options=['median', 'Own', 'Weighted Own', 'Geomean', 'Lineup Edge', 'Finish_percentile', 'Diversity'], index=0)
2339
  lineup_target = st.number_input("Lineups to produce", value=150, min_value=1, step=1)
2340
  strat_sample = st.slider("Sample range", value=[0.0, 100.0], min_value=0.0, max_value=100.0, step=1.0)
2341
  submitted_col, export_col = st.columns(2)
 
2335
  st.session_state['export_merge'] = st.session_state['export_base'].copy()
2336
  with st.expander('Stratify'):
2337
  with st.form(key='Stratification'):
2338
+ sorting_choice = st.selectbox("Stat Choice", options=['median', 'Own', 'Weighted Own', 'Geomean', 'Lineup Edge', 'Finish_percentile', 'SE Score', 'Diversity'], index=0)
2339
  lineup_target = st.number_input("Lineups to produce", value=150, min_value=1, step=1)
2340
  strat_sample = st.slider("Sample range", value=[0.0, 100.0], min_value=0.0, max_value=100.0, step=1.0)
2341
  submitted_col, export_col = st.columns(2)
global_func/stratification_function.py CHANGED
@@ -2,7 +2,7 @@ import pandas as pd
2
  import numpy as np
3
 
4
  def stratification_function(portfolio: pd.DataFrame, lineup_target: int, exclude_cols: list, sport: str, sorting_choice: str, low_threshold: float, high_threshold: float):
5
- excluded_cols = ['salary', 'median', 'Own', 'Finish_percentile', 'Dupes', 'Stack', 'Size', 'Win%', 'Lineup Edge', 'Weighted Own', 'Geomean', 'Diversity']
6
  player_columns = [col for col in portfolio.columns if col not in excluded_cols]
7
 
8
  # Work with indices instead of copying entire DataFrame
@@ -15,16 +15,21 @@ def stratification_function(portfolio: pd.DataFrame, lineup_target: int, exclude
15
  similarity_floor = portfolio[sorting_choice].quantile(low_threshold / 100)
16
  similarity_ceiling = portfolio[sorting_choice].quantile(high_threshold / 100)
17
 
18
- # Create evenly spaced target similarity scores
19
- target_similarities = np.linspace(similarity_floor, similarity_ceiling, lineup_target)
20
-
21
- # Find the closest lineup to each target similarity score
22
- selected_indices = []
23
- for target_sim in target_similarities:
24
- # Find the index of the closest similarity score
25
- closest_idx = (portfolio[sorting_choice] - target_sim).abs().idxmin()
26
- if closest_idx not in selected_indices: # Avoid duplicates
27
- selected_indices.append(closest_idx)
 
 
 
 
 
28
 
29
  # Return view instead of copy
30
  return portfolio.loc[selected_indices].sort_values(by=sorting_choice, ascending=False)
 
2
  import numpy as np
3
 
4
  def stratification_function(portfolio: pd.DataFrame, lineup_target: int, exclude_cols: list, sport: str, sorting_choice: str, low_threshold: float, high_threshold: float):
5
+ excluded_cols = ['salary', 'median', 'Own', 'Finish_percentile', 'Dupes', 'Stack', 'Size', 'Win%', 'Lineup Edge', 'Weighted Own', 'Geomean', 'Diversity', 'SE Score']
6
  player_columns = [col for col in portfolio.columns if col not in excluded_cols]
7
 
8
  # Work with indices instead of copying entire DataFrame
 
15
  similarity_floor = portfolio[sorting_choice].quantile(low_threshold / 100)
16
  similarity_ceiling = portfolio[sorting_choice].quantile(high_threshold / 100)
17
 
18
+ while True:
19
+ target_similarities = np.linspace(similarity_floor, similarity_ceiling, lineup_target)
20
+
21
+ # Find the closest lineup to each target similarity score
22
+ selected_indices = []
23
+ for target_sim in target_similarities:
24
+ # Find the index of the closest similarity score
25
+ closest_idx = (portfolio[sorting_choice] - target_sim).abs().idxmin()
26
+ if closest_idx not in selected_indices: # Avoid duplicates
27
+ selected_indices.append(closest_idx)
28
+ if len(selected_indices) > lineup_target:
29
+ selected_indices = selected_indices[:lineup_target]
30
+ break
31
+ else:
32
+ lineup_target += 5
33
 
34
  # Return view instead of copy
35
  return portfolio.loc[selected_indices].sort_values(by=sorting_choice, ascending=False)