Spaces:
Sleeping
Sleeping
File size: 2,112 Bytes
2e3cd9d 99b9aa9 47fd581 2e3cd9d 521dad6 2e3cd9d 7eef51a 2e3cd9d 7eef51a 2e3cd9d 7eef51a 2e3cd9d a8635e8 521dad6 47fd581 7ba4f09 47fd581 7ba4f09 0b924a0 3ca2ebf 0b924a0 7ba4f09 0b924a0 3ca2ebf 47fd581 521dad6 2e3cd9d 7eef51a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import pandas as pd
import numpy as np
def stratification_function(portfolio: pd.DataFrame, lineup_target: int, exclude_cols: list, sport: str, sorting_choice: str, low_threshold: float, high_threshold: float):
excluded_cols = ['salary', 'median', 'Own', 'Finish_percentile', 'Dupes', 'Stack', 'Size', 'Win%', 'Lineup Edge', 'Weighted Own', 'Geomean', 'Diversity', 'SE Score']
player_columns = [col for col in portfolio.columns if col not in excluded_cols]
base_target = lineup_target
# Work with indices instead of copying entire DataFrame
if sorting_choice == 'Finish_percentile':
sorted_indices = portfolio[sorting_choice].sort_values(ascending=True).index
else:
sorted_indices = portfolio[sorting_choice].sort_values(ascending=False).index
# Calculate quantiles without copying
similarity_floor = portfolio[sorting_choice].quantile(low_threshold / 100)
similarity_ceiling = portfolio[sorting_choice].quantile(high_threshold / 100)
for range_var in range(1, 10):
target_similarities = np.linspace(similarity_floor, similarity_ceiling, base_target)
# Find the closest lineup to each target similarity score
selected_indices = []
for target_sim in target_similarities:
# Find the index of the closest similarity score
closest_idx = (portfolio[sorting_choice] - target_sim).abs().idxmin()
if closest_idx not in selected_indices: # Avoid duplicates
selected_indices.append(closest_idx)
print(len(selected_indices))
if len(selected_indices) > lineup_target:
selected_indices = selected_indices[:lineup_target]
print(len(selected_indices))
range_var = 10
break
elif len(selected_indices) == lineup_target:
print(len(selected_indices))
range_var = 10
break
else:
base_target += 5 * range_var
# Return view instead of copy
return portfolio.loc[selected_indices].sort_values(by=sorting_choice, ascending=False)
|