in demo_scripts/create_afd_resources.py [0:0]
def afd_train_model_demo(s3bucket, s3suffix, eventtype, modelname, iamrole, fraudcategory):
#############################################
##### Setup #####
config_file = RECIPE[fraudcategory]
MODEL_NAME = modelname
EVENT_TYPE = eventtype
IAM_ROLE = iamrole
EVENT_VARIABLES = [variable["variable_name"] for variable in config_file["variable_mappings"]]
EVENT_LABELS = ["fraud", "legit"]
# Variable mappings of demo data in this use case. Important to teach this to customer
click.echo(f'{pd.DataFrame(config_file["variable_mappings"])}')
# Copy file from
data_path = config_file["data_path"]
response = s3.list_objects_v2(Bucket=s3bucket, Prefix=os.path.join(s3suffix, data_path))
if response['KeyCount'] == 0:
s3.put_object(
Bucket=s3bucket,
Key=os.path.join(s3suffix, data_path),
Body=open(data_path, 'rb')
)
S3_DATA_PATH = "s3://" + os.path.join(s3bucket, s3suffix, data_path)
#############################################
##### Create event variables and labels #####
# -- create variable --
for variable in config_file["variable_mappings"]:
DEFAULT_VALUE = '0.0' if variable["data_type"] == "FLOAT" else '<null>'
try:
resp = client.get_variables(name = variable["variable_name"])
click.echo("{0} exists, data type: {1}".format(variable["variable_name"], resp['variables'][0]['dataType']))
except:
click.echo("Creating variable: {0}".format(variable["variable_name"]))
resp = client.create_variable(
name = variable["variable_name"],
dataType = variable["data_type"],
dataSource ='EVENT',
defaultValue = DEFAULT_VALUE,
description = variable["variable_name"],
variableType = variable["variable_type"])
response = client.put_label(
name = "fraud",
description = "FRAUD")
response = client.put_label(
name = "legit",
description = "LEGIT")
#############################################
##### Define Entity and Event Types #####
# -- create entity type --
try:
response = client.get_entity_types(name = ENTITY_TYPE)
click.echo("-- entity type exists --")
click.echo(response)
except:
response = client.put_entity_type(
name = ENTITY_TYPE,
description = ENTITY_DESC
)
click.echo("-- create entity type --")
click.echo(response)
# -- create event type --
try:
response = client.get_event_types(name = EVENT_TYPE)
click.echo("\n-- event type exists --")
click.echo(response)
except:
response = client.put_event_type (
name = EVENT_TYPE,
eventVariables = EVENT_VARIABLES,
labels = EVENT_LABELS,
entityTypes = [ENTITY_TYPE])
click.echo("\n-- create event type --")
click.echo(response)
#############################################
##### Create and train your model #####
try:
response = client.create_model(
description = MODEL_DESC,
eventTypeName = EVENT_TYPE,
modelId = MODEL_NAME,
modelType = MODEL_TYPE)
click.echo("-- initalize model --")
click.echo(response)
except Exception:
pass
# -- initalized the model, it's now ready to train --
# -- first define training_data_schema for model to use --
training_data_schema = {
'modelVariables' : EVENT_VARIABLES,
'labelSchema' : {
'labelMapper' : {
'FRAUD' : ["fraud"],
'LEGIT' : ["legit"]
}
}
}
response = client.create_model_version(
modelId = MODEL_NAME,
modelType = MODEL_TYPE,
trainingDataSource = 'EXTERNAL_EVENTS',
trainingDataSchema = training_data_schema,
externalEventsDetail = {
'dataLocation' : S3_DATA_PATH,
'dataAccessRoleArn': IAM_ROLE
}
)
model_version = response['modelVersionNumber']
click.echo("-- model training --")
click.echo(response)