File size: 15,306 Bytes
4021124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
##############################
Amazon SageMaker Feature Store
##############################

.. rubric:: **Create Feature Groups**
   :name: bCe9CAXalwH

This guide will show you how to create and use
`Amazon SageMaker Feature Store <https://docs.aws.amazon.com/sagemaker/latest/dg/feature-store-getting-started.html>`__.
The example code in this guide covers using the SageMaker Python SDK. The
underlying APIs are available for developers using other languages.

.. rubric:: Features
   :name: bCe9CAtWHPP

Prior to using a feature store you will typically load your dataset, run
transformations, and set up your features for ingestion. This step has a
lot of variation and is highly dependent on your data. The example code
in the following code blocks will often make reference to an example
notebook, \ `Fraud Detection with Amazon SageMaker Feature Store
<https://sagemaker-examples.readthedocs.io/en/latest/sagemaker-featurestore/sagemaker_featurestore_fraud_detection_python_sdk.html>`__.
It is recommended that you run this notebook
in SageMaker Studio and use the code from there, as the code in this
guide is conceptual and not fully functional if copied.

.. rubric:: Feature store data types and schema
   :name: bCe9CAr4kIT

Feature Store supported types are ``String``, ``Fractional``, and
``Integral``. The default type is set to ``String``. This means that, if
a column in your dataset is not a ``float`` or ``long`` type, it will
default to ``String`` in your feature store.


You may use a schema to describe your data’s columns and data types. You
pass this schema into FeatureDefinitions, a required parameter for a
FeatureGroup. However, for Python developers, the SageMaker Python SDK
has automatic data type detection when you use the
``load_feature_definitions`` function.

.. rubric:: Feature store setup
   :name: bCe9CAgy6IH

To start using Feature Store, first create a SageMaker session, boto3
session, and a Feature Store session. Also, setup the bucket you will
use for your features; this is your Offline Store. The following will
use the SageMaker default bucket and add a custom prefix to it.

.. note::

   The role that you use requires these managed
   policies:\ ``AmazonSageMakerFullAccess``\ and\ ``AmazonSageMakerFeatureStoreAccess``\ .


.. code:: python

   import boto3
   import sagemaker
   from sagemaker.session import Session

   boto_session = boto3.Session(region_name=region)
   role = sagemaker.get_execution_role()
   sagemaker_session = sagemaker.Session()
   region = sagemaker_session.boto_region_name
   default_bucket = sagemaker_session.default_bucket()
   prefix = 'sagemaker-featurestore'
   offline_feature_store_bucket = 's3://*{}*/*{}*'.format(default_bucket, prefix)

   sagemaker_client = boto_session.client(service_name='sagemaker', region_name=region)
   featurestore_runtime = boto_session.client(service_name='sagemaker-featurestore-runtime', region_name=region)

   feature_store_session = Session(
       boto_session=boto_session,
       sagemaker_client=sagemaker_client,
       sagemaker_featurestore_runtime_client=featurestore_runtime
   )

.. rubric:: Load datasets and partition data into feature groups
   :name: bCe9CA31y9f

You will load your data into data frames for each of your features. You
will use these data frames after you setup the feature group. In the
fraud detection example, you can see these steps in the following code.

.. code:: python

   import numpy as np
   import pandas as pd
   import matplotlib.pyplot as plt
   import io

   fraud_detection_bucket_name = 'sagemaker-featurestore-fraud-detection'
   identity_file_key = 'sampled_identity.csv'
   transaction_file_key = 'sampled_transactions.csv'

   identity_data_object = s3_client.get_object(Bucket=fraud_detection_bucket_name, Key=identity_file_key)
   transaction_data_object = s3_client.get_object(Bucket=fraud_detection_bucket_name, Key=transaction_file_key)

   identity_data = pd.read_csv(io.BytesIO(identity_data_object['Body'].read()))
   transaction_data = pd.read_csv(io.BytesIO(transaction_data_object['Body'].read()))

   identity_data = identity_data.round(5)
   transaction_data = transaction_data.round(5)

   identity_data = identity_data.fillna(0)
   transaction_data = transaction_data.fillna(0)

   # Feature transformations for this dataset are applied before ingestion into FeatureStore.
   # One hot encode card4, card6
   encoded_card_bank = pd.get_dummies(transaction_data['card4'], prefix = 'card_bank')
   encoded_card_type = pd.get_dummies(transaction_data['card6'], prefix = 'card_type')

   transformed_transaction_data = pd.concat([transaction_data, encoded_card_type, encoded_card_bank], axis=1)
   transformed_transaction_data = transformed_transaction_data.rename(columns={"card_bank_american express": "card_bank_american_express"})

.. rubric:: Feature group setup
   :name: bCe9CARx8h9

Name your feature groups and customize the feature names with a unique
name, and setup each feature group with the ``FeatureGroup`` class.

.. code:: python

   from sagemaker.feature_store.feature_group import FeatureGroup
   feature_group_name = "some string for a name"
   feature_group = FeatureGroup(name=feature_group_name, sagemaker_session=feature_store_session)

For example, in the fraud detection example, the two feature groups are
“identity” and “transaction”. In the following code you can see how the
names are customized with a timestamp, then each group is setup by
passing in the name and the session.

.. code:: python

   import time
   from time import gmtime, strftime, sleep
   from sagemaker.feature_store.feature_group import FeatureGroup

   identity_feature_group_name = 'identity-feature-group-' + strftime('%d-%H-%M-%S', gmtime())
   transaction_feature_group_name = 'transaction-feature-group-' + strftime('%d-%H-%M-%S', gmtime())

   identity_feature_group = FeatureGroup(name=identity_feature_group_name, sagemaker_session=feature_store_session)
   transaction_feature_group = FeatureGroup(name=transaction_feature_group_name, sagemaker_session=feature_store_session)

.. rubric:: Record identifier and event time feature
   :name: bCe9CA17VV7

Next, you will need a record identifier name and an event time feature
name. This will match the column of the corresponding features in your
data. For example, in the fraud detection example, the column of
interest is “TransactionID”. “EventTime” can be appended to your data
when no timestamp is available. In the following code, you can see how
these variables are set, and then ``EventTime`` is appended to both
feature’s data.

.. code:: python

   record_identifier_name = "TransactionID"
   event_time_feature_name = "EventTime"
   current_time_sec = int(round(time.time()))
   identity_data[event_time_feature_name] = pd.Series([current_time_sec]*len(identity_data), dtype="float64")
   transformed_transaction_data[event_time_feature_name] = pd.Series([current_time_sec]*len(transaction_data), dtype="float64")

.. rubric:: Feature definitions
   :name: bCe9CA4yUcO

You can now load the feature definitions by passing a data frame
containing the feature data. In the following code for the fraud
detection example, the identity feature and transaction feature are each
loaded by using ``load_feature_definitions``, and this function
automatically detects the data type of each column of data. For
developers using a schema rather than automatic detection, refer to the
`Creating Feature Groups with Data Wrangler example <https://docs.aws.amazon.com/sagemaker/latest/dg/data-wrangler-data-export.html#data-wrangler-data-export-feature-store>`__ for
code that shows loading the schema, mapping it and adding as a
``FeatureDefinition`` that is used when you create the ``FeatureGroup``.
This example also covers a boto3 implementation, instead of using the
SageMaker Python SDK.

.. code:: python

   identity_feature_group.load_feature_definitions(data_frame=identity_data); # output is suppressed
   transaction_feature_group.load_feature_definitions(data_frame=transformed_transaction_data); # output is suppressed

.. rubric:: Create a feature group
   :name: bCe9CAwMEgY

The last step for creating the feature group is to use the
``create`` function. The following code shows all of the available
parameters. The online store is not created by default, so you must set
this as \ ``True`` if you want to enable it. The ``s3_uri`` is the
location of your offline store.

.. code:: python

   # create a FeatureGroup
   feature_group.create(
       description = "Some info about the feature group",
       feature_group_name = feature_group_name,
       record_identifier_name = record_identifier_name,
       event_time_feature_name = event_time_feature_name,
       feature_definitions = feature_definitions,
       role_arn = role,
       s3_uri = offline_feature_store_bucket,
       enable_online_store = True,
       online_store_kms_key_id = None,
       offline_store_kms_key_id = None,
       disable_glue_table_creation = False,
       data_catalog_config = None,
       tags = ["tag1","tag2"])

The following code from the fraud detection example shows a minimal
``create`` call for each of the two features groups being created.

.. code:: python

   identity_feature_group.create(
       s3_uri=offline_feature_store_bucket,
       record_identifier_name=record_identifier_name,
       event_time_feature_name=event_time_feature_name,
       role_arn=role,
       enable_online_store=True
   )

   transaction_feature_group.create(
       s3_uri=offline_feature_store_bucket,
       record_identifier_name=record_identifier_name,
       event_time_feature_name=event_time_feature_name,
       role_arn=role,
       enable_online_store=True
   )

Creating a feature group takes time as the data is loaded. You will need
to wait until it is created before you can use it. You can check status
using the following method.

.. code:: python

    status = feature_group.describe().get("FeatureGroupStatus")

While it is creating you will get a ``Creating`` as a response. When
this has finished successfully the response will be ``Created``. The
other possible statuses are ``CreateFailed``, ``Deleting``, or
``DeleteFailed``.

.. rubric:: Describe a feature group
   :name: bCe9CA2TNON

You can retrieve information about your feature group with the
``describe`` function.

.. code:: python

   feature_group.describe()

.. rubric:: List feature groups
   :name: bCe9CA2wPF2

You can list all of your feature groups with the
``list_feature_groups`` function.

.. code:: python

   sagemaker_client.list_feature_groups()

.. rubric:: Put records in a feature group
   :name: bCe9CAymRdA

You can use the ``ingest`` function to load your feature data. You pass
in a data frame of feature data, set the number of workers, and choose
to wait for it to return or not. The following example demonstrates
using the ``ingest`` function.

.. code:: python

   feature_group.ingest(
       data_frame=feature_data, max_workers=3, wait=True
   )

For each feature group you have, run the ``ingest`` function on the
feature data you want to load.

.. rubric:: Get records from a feature group
   :name: bCe9CA25xj5

You can use the ``get_record`` function to retrieve the data for a
specific feature by its record identifier. The following example uses an
example identifier to retrieve the record.

.. code:: python

   record_identifier_value = str(2990130)
   featurestore_runtime.get_record(FeatureGroupName=transaction_feature_group_name, RecordIdentifierValueAsString=record_identifier_value)

You can use the ``batch_get_record`` function to retrieve multiple records simultaneously from your feature store. The following example uses this API to retrieve a batch of records.

.. code:: python

   record_identifier_values = ["573291", "109382", "828400", "124013"]
   featurestore_runtime.batch_get_record(Identifiers=[{"FeatureGroupName": transaction_feature_group_name, "RecordIdentifiersValueAsString": record_identifier_values}])

An example response from the fraud detection example:

.. code:: python

   ...
   'Record': [{'FeatureName': 'TransactionID', 'ValueAsString': '2990130'},
     {'FeatureName': 'isFraud', 'ValueAsString': '0'},
     {'FeatureName': 'TransactionDT', 'ValueAsString': '152647'},
     {'FeatureName': 'TransactionAmt', 'ValueAsString': '75.0'},
     {'FeatureName': 'ProductCD', 'ValueAsString': 'H'},
     {'FeatureName': 'card1', 'ValueAsString': '4577'},
   ...

.. rubric:: Hive DDL commands
   :name: bCe9CA30nHn

The SageMaker Python SDK’s FeatureStore class also provides the
functionality to generate Hive DDL commands. The schema of the table is
generated based on the feature definitions. Columns are named after
feature name and data-type are inferred based on feature type.

.. code:: python

   print(feature_group.as_hive_ddl())

An example output:

.. code:: python

   CREATE EXTERNAL TABLE IF NOT EXISTS sagemaker_featurestore.identity-feature-group-27-19-33-00 (
     TransactionID INT
     id_01 FLOAT
     id_02 FLOAT
     id_03 FLOAT
     id_04 FLOAT
    ...

.. rubric:: Build a Training Dataset
   :name: bCe9CAVnDLV

Feature Store automatically builds a Amazon Glue Data Catalog when
Feature Groups are created and can optionally be turned off. The
following we show how to create a single training dataset with feature
values from both identity and transaction feature groups created above.
Also, the following shows how to run an Amazon Athena query to join data
stored in the Offline Store from both identity and transaction feature
groups.


To start, create an Athena query using\ ``athena_query()``\ for both
identity and transaction feature groups. The ``table_name`` is the Glue
table that is auto-generated by Feature Store.

.. code:: python

   identity_query = identity_feature_group.athena_query()
   transaction_query = transaction_feature_group.athena_query()

   identity_table = identity_query.table_name
   transaction_table = transaction_query.table_name

.. rubric:: Writing and Executing your Athena Query
   :name: bCe9CArSR5J

You will write your query using SQL on these feature groups, and then
execute the query with the ``.run()`` command and specify your S3 bucket
location for the data set to be saved there.

.. code:: python

   # Athena query
   query_string = 'SELECT * FROM "'+transaction_table+'" LEFT JOIN "'+identity_table+'" ON "'+transaction_table+'".transactionid = "'+identity_table+'".transactionid'

   # run Athena query. The output is loaded to a Pandas dataframe.
   dataset = pd.DataFrame()
   identity_query.run(query_string=query_string, output_location='s3://'+default_s3_bucket_name+'/query_results/')
   identity_query.wait()
   dataset = identity_query.as_dataframe()

From here you can train a model using this data set and then perform
inference.

.. rubric:: Delete a feature group
   :name: bCe9CA61b78

You can delete a feature group with the ``delete`` function.

.. code:: python

   feature_group.delete()

The following code example is from the fraud detection example.

.. code:: python

   identity_feature_group.delete()
   transaction_feature_group.delete()