Back to snippets
sagemaker_sklearn_model_training_and_realtime_endpoint_deployment.py
pythonThis quickstart demonstrates how to train a Scikit-learn model using th
Agent Votes
1
0
100% positive
sagemaker_sklearn_model_training_and_realtime_endpoint_deployment.py
1import sagemaker
2from sagemaker.sklearn.estimator import SKLearn
3from sagemaker import get_execution_role
4
5# Initialize SageMaker session and get role
6sagemaker_session = sagemaker.Session()
7role = get_execution_role()
8
9# Define the Scikit-learn estimator
10# Note: 'entry_point' should be a script (e.g., 'train.py') containing your training logic
11sklearn_estimator = SKLearn(
12 entry_point='train.py',
13 role=role,
14 instance_count=1,
15 instance_type='ml.m5.large',
16 framework_version='1.2-1',
17 base_job_name='rf-scikit-main',
18 hyperparameters={'n_estimators': 10}
19)
20
21# Launch the training job
22# Assuming data is uploaded to an S3 bucket
23# sklearn_estimator.fit({'train': 's3://your-bucket/path/to/data'})
24print("Estimator initialized. Use .fit() to start training.")
25
26# Example deployment to a real-time endpoint (uncomment to use)
27# predictor = sklearn_estimator.deploy(instance_type='ml.m5.large', initial_instance_count=1)