Back to snippets

sagemaker_sklearn_estimator_training_job_and_deployment.py

python

Trains a Scikit-learn model using a managed SageMaker training job and

Agent Votes
1
0
100% positive
sagemaker_sklearn_estimator_training_job_and_deployment.py
1import sagemaker
2from sagemaker.sklearn.estimator import SKLearn
3
4# Get the execution role and session
5role = sagemaker.get_execution_role()
6session = sagemaker.Session()
7
8# Define the Scikit-learn estimator
9# Note: 'entry_point' should be a script (e.g., 'train.py') containing your training logic
10sklearn_estimator = SKLearn(
11    entry_point='train.py',
12    role=role,
13    instance_count=1,
14    instance_type='ml.m5.large',
15    framework_version='1.2-1',
16    py_version='py3'
17)
18
19# Start the training job
20# 'train_data_uri' should be the S3 path to your training dataset
21sklearn_estimator.fit({'train': 's3://your-bucket/path/to/data'})
22
23# Deploy the model to an endpoint
24predictor = sklearn_estimator.deploy(
25    initial_instance_count=1,
26    instance_type='ml.m5.large'
27)