Skip to content

Commit

Permalink
Replace boto with boto3.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pogorzelska committed Feb 22, 2016
1 parent b290992 commit 1a13b4b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
40 changes: 20 additions & 20 deletions targeted-marketing-python/build_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
python build_model.py ["Optional name for created objects"]
"""
import base64
import boto
import boto3
import json
import os
import sys
Expand All @@ -32,7 +32,7 @@
def build_model(data_s3_url, schema_fn, recipe_fn, name, train_percent=70):
"""Creates all the objects needed to build an ML Model & evaluate its quality.
"""
ml = boto.connect_machinelearning()
ml = boto3.client('machinelearning')
(train_ds_id, test_ds_id) = create_data_sources(ml, data_s3_url, schema_fn,
train_percent, name)
ml_model_id = create_model(ml, train_ds_id, recipe_fn, name)
Expand All @@ -59,10 +59,10 @@ def create_data_sources(ml, data_s3_url, schema_fn, train_percent, name):
"DataSchema": open(schema_fn).read(),
}
ml.create_data_source_from_s3(
data_source_id=train_ds_id,
data_spec=spec,
data_source_name=name + " - training split",
compute_statistics=True
DataSourceId=train_ds_id,
DataSpec=spec,
DataSourceName=name + " - training split",
ComputeStatistics=True
)
print("Created training data set %s" % train_ds_id)

Expand All @@ -74,10 +74,10 @@ def create_data_sources(ml, data_s3_url, schema_fn, train_percent, name):
}
})
ml.create_data_source_from_s3(
data_source_id=test_ds_id,
data_spec=spec,
data_source_name=name + " - testing split",
compute_statistics=True
DataSourceId=test_ds_id,
DataSpec=spec,
DataSourceName=name + " - testing split",
ComputeStatistics=True
)
print("Created test data set %s" % test_ds_id)
return (train_ds_id, test_ds_id)
Expand All @@ -91,18 +91,18 @@ def create_model(ml, train_ds_id, recipe_fn, name):
"""
model_id = 'ml-' + base64.b32encode(os.urandom(10))
ml.create_ml_model(
ml_model_id=model_id,
ml_model_name=name + " model",
ml_model_type="BINARY", # we're predicting True/False values
parameters={
MLModelId=model_id,
MLModelName=name + " model",
MLModelType="BINARY", # we're predicting True/False values
Parameters={
# Refer to the "Machine Learning Concepts" documentation
# for guidelines on tuning your model
"sgd.maxPasses": "100",
"sgd.maxMLModelSizeInBytes": "104857600", # 100 MiB
"sgd.l2RegularizationAmount": "1e-4",
},
recipe=open(recipe_fn).read(),
training_data_source_id=train_ds_id
Recipe=open(recipe_fn).read(),
TrainingDataSourceId=train_ds_id
)
print("Created ML Model %s" % model_id)
return model_id
Expand All @@ -111,10 +111,10 @@ def create_model(ml, train_ds_id, recipe_fn, name):
def create_evaluation(ml, model_id, test_ds_id, name):
eval_id = 'ev-' + base64.b32encode(os.urandom(10))
ml.create_evaluation(
evaluation_id=eval_id,
evaluation_name=name + " evaluation",
ml_model_id=model_id,
evaluation_data_source_id=test_ds_id
EvaluationId=eval_id,
EvaluationName=name + " evaluation",
MLModelId=model_id,
EvaluationDataSourceId=test_ds_id
)
print("Created Evaluation %s" % eval_id)
return eval_id
Expand Down
26 changes: 13 additions & 13 deletions targeted-marketing-python/use_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
python use_model.py ml-12345678901 0.77 s3://your-bucket/prefix
"""
import base64
import boto
import boto3
import datetime
import os
import random
Expand All @@ -39,28 +39,28 @@
def use_model(model_id, threshold, schema_fn, output_s3, data_s3url):
"""Creates all the objects needed to build an ML Model & evaluate its quality.
"""
ml = boto.connect_machinelearning()
ml = boto3.client('machinelearning')

poll_until_completed(ml, model_id) # Can't use it until it's COMPLETED
ml.update_ml_model(model_id, score_threshold=threshold)
ml.update_ml_model(MLModelId=model_id, ScoreThreshold=threshold)
print("Set score threshold for %s to %.2f" % (model_id, threshold))

bp_id = 'bp-' + base64.b32encode(os.urandom(10))
ds_id = create_data_source_for_scoring(ml, data_s3url, schema_fn)
ml.create_batch_prediction(
batch_prediction_id=bp_id,
batch_prediction_name="Batch Prediction for marketing sample",
ml_model_id=model_id,
batch_prediction_data_source_id=ds_id,
output_uri=output_s3
BatchPredictionId=bp_id,
BatchPredictionName="Batch Prediction for marketing sample",
MLModelId=model_id,
BatchPredictionDataSourceId=ds_id,
OutputUri=output_s3
)
print("Created Batch Prediction %s" % bp_id)


def poll_until_completed(ml, model_id):
delay = 2
while True:
model = ml.get_ml_model(model_id)
model = ml.get_ml_model(MLModelId=model_id)
status = model['Status']
message = model.get('Message', '')
now = str(datetime.datetime.now().time())
Expand All @@ -76,13 +76,13 @@ def poll_until_completed(ml, model_id):
def create_data_source_for_scoring(ml, data_s3url, schema_fn):
ds_id = 'ds-' + base64.b32encode(os.urandom(10))
ml.create_data_source_from_s3(
ds_id,
data_source_name="DS for Batch Prediction %s" % data_s3url,
data_spec={
DataSourceId=ds_id,
DataSourceName="DS for Batch Prediction %s" % data_s3url,
DataSpec={
"DataLocationS3": data_s3url,
"DataSchema": open(schema_fn).read(),
},
compute_statistics=False
ComputeStatistics=False
)
print("Created Datasource %s for batch prediction" % ds_id)
return ds_id
Expand Down

0 comments on commit 1a13b4b

Please sign in to comment.