-
Notifications
You must be signed in to change notification settings - Fork 2
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 #38 from McGill-MMA-EnterpriseAnalytics/aladelca-p…
…atch-5 predict
- Loading branch information
Showing
3 changed files
with
64 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import json | ||
import pandas as pd | ||
from utils import * | ||
from predict import Predict as pr | ||
def lambda_handler(event, context): | ||
bucket_name = 'enterprise-data-science' | ||
path = 'training_files/trained_model.pickle' | ||
#client = boto3.client('s3') | ||
|
||
data = json.loads(json.dumps(event)) | ||
''' | ||
with open(event) as f: | ||
data = json.load(f) | ||
print(data) | ||
''' | ||
#data = json.loads(json.dumps(event)) | ||
try: | ||
data = data['result'] | ||
except: | ||
data = data | ||
df = pd.DataFrame(data['transactions']) | ||
|
||
predictions = pr.main_predict(df, bucket_name, path) | ||
|
||
|
||
#results = {'results': predictions} | ||
results_dicts = [json.loads(item) for item in predictions] | ||
response = { | ||
"statusCode": 200, | ||
"headers": { | ||
"Content-Type": "application/json" | ||
}, | ||
"body": results_dicts | ||
} | ||
return response |
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,14 @@ | ||
from utils import * | ||
import numpy | ||
class Predict: | ||
def main_predict(data, bucket_name, path): | ||
model = load_models_from_s3(bucket_name, path ) | ||
|
||
probas = model.predict_proba(data) | ||
preds = np.where(probas[:,1] >= 0.4, 1, 0) | ||
data['prob_0'] = probas[:,0] | ||
data['prob_1'] = probas[:,1] | ||
data['predictions'] = preds | ||
record = data.to_dict(orient='records')[0] | ||
json_result = data.to_json(orient='records', lines=True).splitlines() | ||
return json_result |
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,15 @@ | ||
import boto3 | ||
from io import BytesIO | ||
import joblib | ||
def load_models_from_s3(bucket_name, object_name): | ||
|
||
s3 = boto3.client('s3') | ||
model_file = BytesIO() | ||
s3.download_fileobj(bucket_name, object_name, model_file) | ||
|
||
model_file.seek(0) | ||
|
||
|
||
modelo = joblib.load(model_file) | ||
|
||
return modelo |