Skip to content

Instantly share code, notes, and snippets.

@sgbaird
Created December 8, 2021 23:23
Show Gist options
  • Save sgbaird/be004bd8405212ec74f62ebef4e65ded to your computer and use it in GitHub Desktop.
Save sgbaird/be004bd8405212ec74f62ebef4e65ded to your computer and use it in GitHub Desktop.
Create some dummy data and generate ordinal encodings and parameters/constraints according to the Ax (adaptive design) framework.
"""Create some dummy data and generate ordinal encodings and parameters/constraints."""
import numpy as np
import pandas as pd
# %% data and choices
data = [["A", "B", "C"], ["D", "C", "A"], ["C", "A", "B"], ["C", "B", "A"]]
choices = list(np.unique(np.array(data).flatten()))
n_choices = len(choices)
# %% ordinal encoding
df = pd.DataFrame(data)
choice_lookup = {
choice: choice_num for (choice, choice_num) in zip(choices, range(n_choices))
}
encoded_df = df.replace(choice_lookup)
encoded_choices = pd.DataFrame(choices)[0].map(choice_lookup).values
encoded_data = encoded_df.values
print(encoded_data)
# %% parameters
nslots = 3
slot_names = ["slot_" + str(i) for i in range(nslots)]
slots = [
{
"name": slot_name,
"type": "choice",
"values": encoded_choices,
}
for slot_name in slot_names
]
print(slots) # then format via black
# > [
# > {"name": "slot_0", "type": "choice", "values": ["A", "B", "C", "D"]},
# > {"name": "slot_1", "type": "choice", "values": ["A", "B", "C", "D"]},
# > {"name": "slot_2", "type": "choice", "values": ["A", "B", "C", "D"]},
# > ]
# %% constraints
constraints = [
lhs + " <= " + rhs for (lhs, rhs) in list(zip(slot_names[:-1], slot_names[1:]))
]
print(constraints)
# > ["slot_0 >= slot_1", "slot_1 >= slot_2"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment