1- import numpy as np
1+ import numpy as np
22import pandas as pd
33import matplotlib .pyplot as plt
44
5+
56def generate_weights (df , n , bias , plot_weights = False , enforce = False ):
67 """
7- Generates a Pandas series of weights for each datum given a particular bias.
8+ Generates a Pandas series of weights for each datum given a particular bias.
89
910 df: DataFrame of candidates
1011 n: size of the design to pick
@@ -15,7 +16,7 @@ def generate_weights(df, n, bias, plot_weights=False, enforce=False):
1516 plot_weights: boolean, whether to plot distribution of weights, default False
1617 enforce: boolean, whether to force biases, default False
1718
18- Returns: Pandas Series of normalized row weights.
19+ Returns: Pandas Series of normalized row weights.
1920 """
2021 weights = pd .Series (1.0 , index = df .index )
2122 for col , params in bias .items ():
@@ -29,7 +30,7 @@ def generate_weights(df, n, bias, plot_weights=False, enforce=False):
2930 if enforce :
3031 if (weights > 0 ).sum () < n :
3132 raise ValueError (f"Not enough rows ({ (weights > 0 ).sum ()} ) satisfy all enforce conditions for n={ n } ." )
32-
33+
3334 weights = weights / weights .sum ()
3435
3536 print ("Weights min:" , weights .min (), "max:" , weights .max ())
@@ -43,10 +44,11 @@ def generate_weights(df, n, bias, plot_weights=False, enforce=False):
4344 plt .show ()
4445
4546 return weights
46-
47+
48+
4749def sample_with_bias (df , n , replace = False , seed = None , bias = None , enforce = False , plot_weights = False ):
4850 """
49- Returns a random Pandas DataFrame sample of data points from a population with or without bias.
51+ Returns a random Pandas DataFrame sample of data points from a population with or without bias.
5052
5153 df: DataFrame of candidates
5254 n: int, size of the design to pick
@@ -58,14 +60,15 @@ def sample_with_bias(df, n, replace=False, seed=None, bias=None, enforce=False,
5860 enforce: boolean, whether to force biases, default False
5961 plot_weights: boolean, whether to plot distribution of weights, default False
6062
61- Returns: Pandas DataFrame of sampled data points.
63+ Returns: Pandas DataFrame of sampled data points.
6264 """
6365 if bias :
6466 w = generate_weights (df , n , bias , plot_weights , enforce )
6567 return df .sample (n = n , replace = replace , random_state = seed , weights = w )
6668 else :
6769 return df .sample (n = n , replace = replace , random_state = seed )
6870
71+
6972def _space_filling_score (Z , metric = "hybrid" ):
7073 """
7174 Z: (k, d) standardized features of the candidate sample
@@ -87,6 +90,7 @@ def _space_filling_score(Z, metric="hybrid"):
8790 return 0.6 * d_min + 0.4 * d_mnn
8891 raise ValueError ("Unknown metric" )
8992
93+
9094def best_sample (df , k , feature_cols , * , n_trials = 500 , bias = None , plot_weights = False , enforce = False ,
9195 random_state = None , standardize = True , dropna = True , metric = "hybrid" ):
9296 """
@@ -103,7 +107,7 @@ def best_sample(df, k, feature_cols, *, n_trials=500, bias=None, plot_weights=Fa
103107 dfv = df .loc [idx ]
104108 Xfull = base .loc [idx ].to_numpy (dtype = float )
105109
106- if bias :
110+ if bias :
107111 weights = generate_weights (df , k , bias , plot_weights , enforce )
108112 else :
109113 weights = None
@@ -137,4 +141,4 @@ def toZ(X): return X
137141 best_score = s
138142 best_df = cand
139143
140- return best_df , {"score" : best_score , "metric" : metric , "n_trials" : n_trials }
144+ return best_df , {"score" : best_score , "metric" : metric , "n_trials" : n_trials }
0 commit comments