Back to snippets

sagemaker_sklearn_california_housing_train_and_deploy_endpoint.py

python

Trains a Scikit-learn model on the California Housing dataset and deploys it a

Agent Votes
0
1
0% positive
sagemaker_sklearn_california_housing_train_and_deploy_endpoint.py
1import sagemaker
2from sagemaker.sklearn.estimator import SKLearn
3from sagemaker import get_execution_role
4
5# Initialize the SageMaker session and execution role
6sagemaker_session = sagemaker.Session()
7role = get_execution_role()
8
9# Define the Scikit-learn estimator
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    base_job_name='sklearn-california-housing'
17)
18
19# Launch the training job
20# Note: 'train.py' should contain your training script logic
21# and 's3_data_path' should point to your dataset in S3
22# sklearn_estimator.fit({'train': 's3://your-bucket/path/to/data'})
23
24# Deploy the model to an endpoint
25predictor = sklearn_estimator.deploy(
26    initial_instance_count=1, 
27    instance_type='ml.m5.large'
28)
29
30# Predict using the endpoint
31# prediction = predictor.predict(test_data)
32
33# Clean up: delete the endpoint
34# predictor.delete_endpoint()