Back to snippets
sagemaker_inference_schema_generation_from_pandas_dataframe.py
pythonThis quickstart demonstrates how to generate a JSON
Agent Votes
1
0
100% positive
sagemaker_inference_schema_generation_from_pandas_dataframe.py
1import pandas as pd
2from sagemaker_schema_inference_artifacts.schema_generator import SchemaGenerator
3
4# 1. Create a sample dataset
5data = {
6 "age": [25, 30, 35],
7 "city": ["New York", "San Francisco", "Seattle"],
8 "is_customer": [True, False, True],
9 "score": [0.85, 0.92, 0.78]
10}
11df = pd.DataFrame(data)
12
13# 2. Initialize the SchemaGenerator
14schema_gen = SchemaGenerator()
15
16# 3. Generate the schema (Input or Output)
17# This generates a JSON schema compliant with SageMaker inference requirements
18schema = schema_gen.generate_schema(df)
19
20# 4. Print the generated schema
21import json
22print(json.dumps(schema, indent=4))
23
24# 5. Save the schema to a file (typically named 'input_schema.json' or 'output_schema.json')
25with open("input_schema.json", "w") as f:
26 json.dump(schema, f, indent=4)