-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from LEFTA98/sagemaker_integration
Sagemaker integration
- Loading branch information
Showing
12 changed files
with
130 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import json | ||
|
||
import numpy as np | ||
from eland import DataFrame | ||
from typing import List, Optional | ||
from math import ceil | ||
|
||
from sagemaker import RealTimePredictor, Session | ||
|
||
DEFAULT_UPLOAD_CHUNK_SIZE = 1000 | ||
|
||
|
||
def make_sagemaker_prediction(endpoint_name: str, | ||
data: DataFrame, | ||
target_column: str, | ||
sagemaker_session: Optional[Session] = None, | ||
column_order: Optional[List[str]] = None, | ||
chunksize: int = None, | ||
sort_index: Optional[str] = '_doc' | ||
)-> np.array: | ||
""" | ||
Make a prediction on an eland dataframe using a deployed SageMaker model endpoint. | ||
Note that predictions will be returned based on the order in which data is ordered when | ||
ed.Dataframe.iterrows() is called on them. | ||
Parameters | ||
---------- | ||
endpoint_name: string representing name of SageMaker endpoint | ||
data: eland DataFrame representing data to feed to SageMaker model. The dataframe must match the input datatypes | ||
of the model and also have the correct number of columns. | ||
target_column: column name of the dependent variable in the data. | ||
sagemaker_session: A SageMaker Session object, used for SageMaker interactions (default: None). If not specified, | ||
one is created using the default AWS configuration chain. | ||
column_order: list of string values representing the proper order that the columns of independent variables should | ||
be read into the SageMaker model. Must be a permutation of the column names of the eland DataFrame. | ||
chunksize: how large each chunk being uploaded to sagemaker should be. | ||
sort_index: the index with which to sort the predictions by. Defaults to '_doc', an internal identifier for | ||
Lucene that optimizes performance. | ||
Returns | ||
---------- | ||
np.array representing the output of the model on input data | ||
""" | ||
predictor = RealTimePredictor(endpoint=endpoint_name, sagemaker_session=sagemaker_session, content_type='text/csv') | ||
data = data.drop(columns=target_column) | ||
|
||
if column_order is not None: | ||
data = data[column_order] | ||
if chunksize is None: | ||
chunksize = DEFAULT_UPLOAD_CHUNK_SIZE | ||
|
||
indices = [index for index, _ in data.iterrows(sort_index=sort_index)] | ||
|
||
to_return = [] | ||
|
||
for i in range(ceil(data.shape[0] / chunksize)): | ||
df_slice = indices[chunksize * i: min(len(indices), chunksize * (i+1))] | ||
to_process = data.filter(df_slice, axis=0) | ||
preds = predictor.predict(to_process.to_csv(header=False, index=False)) | ||
to_return.append(preds) | ||
|
||
return indices, to_return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ elasticsearch>=8,<9 | |
pandas>=1.2,<2 | ||
matplotlib<4 | ||
numpy<2 | ||
opensearch-py>=2 | ||
sagemaker>=1.72,<2 | ||
tqdm<5 | ||
|
||
# | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ elasticsearch>=8,<9 | |
pandas>=1.2,<2 | ||
matplotlib<4 | ||
numpy<2 | ||
opensearch-py>=2 | ||
opensearch-py>=2 | ||
sagemaker>=1.72,<2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters