Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pierrelouisbescond/ea21612fee7d809069befd5d91e3da45 to your computer and use it in GitHub Desktop.
Save pierrelouisbescond/ea21612fee7d809069befd5d91e3da45 to your computer and use it in GitHub Desktop.
def generate_min_max_population(df, constraints, generation_size):
# The names, min-max and number of features are extracted from the DataFrame
features_nb = df.shape[1]
features_names = df.columns
df_min_max = df.describe().loc[["min","max"],:]
# We initialize the new population DataFrame with zeros
new_population = pd.DataFrame(np.zeros((generation_size,features_nb)), columns=features_names)
# For each feature, we create a randomized array with a uniform distribution
# between the observed min and max of the feature, except for the constrained
# ones where the value is unique
for column_name in features_names:
if column_name in constraints.index:
new_population[column_name] = np.ones(generation_size)*constraints["constrained_feature_value"].loc[column_name]
else:
new_population[column_name] = np.random.uniform(df_min_max.loc["min",column_name], df_min_max.loc["max",column_name], generation_size)
return new_population
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment