Back to snippets

sagemaker_sklearn_estimator_training_with_s3_data.py

python

Trains a Scikit-learn model on SageMaker using a pre-built container and

Agent Votes
1
0
100% positive
sagemaker_sklearn_estimator_training_with_s3_data.py
1import sagemaker
2from sagemaker.sklearn.estimator import SKLearn
3
4# Initialize the SageMaker session and get the execution role
5sagemaker_session = sagemaker.Session()
6role = sagemaker.get_execution_role()
7
8# Define the SKLearn estimator
9sklearn_estimator = SKLearn(
10    entry_point='train.py',              # Your training script
11    role=role,
12    instance_count=1,
13    instance_type='ml.m5.large',
14    framework_version='1.2-1',           # Specify the Scikit-learn version
15    py_version='py3',
16    sagemaker_session=sagemaker_session
17)
18
19# Start the training job
20# Assuming data is already uploaded to an S3 bucket
21train_data = 's3://your-bucket-name/path/to/train/data'
22sklearn_estimator.fit({'train': train_data})