Amazon SageMaker AI trains machine learning models using data stored in an Amazon S3 bucket. Follow these steps to load training data from Teradata into an Amazon S3 bucket:
- In the Amazon SageMaker AI console, select
Applications and IDEs->Notebooks->Notebook instances, and then selectCreate notebook instance. See the Amazon SageMaker AI Developer Guide for instructions on how to create a notebook instance:
-
Open your notebook instance:
-
Start a new file by clicking on
New -> conda_python3: -
Install Teradata Python library:
!pip install teradataml -
In a new cell and import additional libraries:
import teradataml as tdml from teradataml import create_context, get_context, remove_context from teradataml.dataframe.dataframe import DataFrame import pandas as pd import boto3, os -
In a new cell, connect to Teradata. Replace
<hostname>,<database user name>,<database password>to match your Teradata environment:create_context(host = '<hostname>', username = '<database user name>', password = '<database password>') -
Retrieve data from the table where the training dataset resides using TeradataML DataFrame API:
train_data = tdml.DataFrame('table_with_training_data') trainDF = train_data.to_pandas()Note
For the XGBoost training job used in this how-to, place the target column first and encode categorical values as numeric values before exporting the data. The CSV file must not contain a header row.
-
Write data to a local file:
trainFileName = 'train.csv' trainDF.to_csv(trainFileName, header=None, index=False) -
Upload the file to Amazon S3. Replace
<s3_bucket_name>with the name of your Amazon S3 bucket:bucket = '<s3_bucket_name>' prefix = 'sagemaker/train' with open(trainFileName, 'rb') as trainFile: boto3.Session().resource('s3') \ .Bucket(bucket) \ .Object(os.path.join(prefix, trainFileName)) \ .upload_fileobj(trainFile)