<jupyter_start><jupyter_text>Phase 3 - deployment This notebook will provide and overview how to deploy and predict the CPE in two ways- The model was build/export in the last notebook (Phase_2_Advanced_Analytics__predictions) This notebook show another option to save/export the model using the H2O flow UI and complement the information with deployment for predictions.The predictions will be presented in 2 ways- Batch process - Online / real time predictions Export model: Export the model GBM (best performance) using H2O flow UI as detailed below<jupyter_code>from IPython.display import Image
Image(filename='./data/H2O-FLOW-UI-GBM-MODEL.PNG')
from IPython.display import Image
Image(filename='./data/H2O-FLOW-UI-GBM-MODEL-download.PNG')<jupyter_output><empty_output><jupyter_text>Sample of new campaigns to be predicted<jupyter_code>import pandas as pd
df = pd.read_csv('./GBM_MODEL/New_campaings_for_predictions.csv')
df.tail(10)<jupyter_output><empty_output><jupyter_text>Important attention point- All information will  be provided for prediction (base information available in the simulated/demo data) however just the relevant information were used during the model build detailed in the Notebook: Phase_2_Advanced_Analytics__predictions  - For example LineItemsID is just an index number and do not provide relevant information and is not going to be used for prediction Batch Prediction: Generate prediction for new data  To execute the prediction as presented below it is not necessary to have an H2O cluster running The processo show below was executed in 2 steps to show in detail the process but in production environment this process must be executed in just one step &emsp; Simulation in 2 stepsStep 1. batch process to run the java programStep 2. python program to link the new data and the predictions with the CPE &emsp; &emsp; Can be used any programming language to run the prediction and get the results (such as R, Python, Java, C, ...) Run batch java process to gererate/score the predictions of CPE<jupyter_code>## To generate prediction (CPE) for new data just run the command

## EXAMPLE
## java -Xmx4g -XX:ReservedCodeCacheSize=256m -cp <h2o-genmodel.jar_EXPORTED_ABOVE> hex.genmodel.tools.PredictCsv --mojo <GBM_log_CPE_model.zip_EXPORTED_ABOVE> --input INPUT_FILE_FOR_PREDICTION.csv --output OUTUPUT_FILE_WITH_PREDICTIONS_FOR_CPE__EXPORT_EXPORT_PREDICTIONS.csv --decimal

## REAL PREDICTION
## java -Xmx4g -XX:ReservedCodeCacheSize=256m -cp h2o-genmodel.jar hex.genmodel.tools.PredictCsv --mojo GBM_log_CPE_model.zip --input New_campaings_for_predictions.csv --output New_campaings_for_predictions__EXPORT_EXPORT_PREDICTIONS.csv --decimal
from IPython.display import Image
Image(filename='./data/Batch-prediction-h2o.PNG')<jupyter_output><empty_output><jupyter_text>Sincronize all information - new campaign data and new predictions for CPE- Remember that the prediction was done in logarithmic scale and now is necessary to rever the result with exponential function<jupyter_code>CPE_predictions = pd.read_csv('./GBM_MODEL/New_campaings_for_predictions__EXPORT_EXPORT_PREDICTIONS.csv')
CPE_predictions.tail()
import numpy as np
df['CPE_predition_LOG'] =  CPE_predictions['predict']
df['CPE_predition'] = round(np.exp(CPE_predictions['predict']) -1, 3)
df.tail()<jupyter_output><empty_output><jupyter_text>Online prediction: Generate prediction for new data  The online prediction could be implemented using diferent architectures such as1. Serverless function such as Amazon AWS Lambda + API Gateway https://aws.amazon.com/lambda/?nc2=h_ql_prod_fs_lbd    2. Java program that use POJO/MOJO model for online prediction http://docs.h2o.ai/h2o/latest-stable/h2o-docs/productionizing.htmlstep-2-compile-and-run-the-mojo3. Microservices architecture using Docker (python + flask app + NGINX for load balance) Could be implemented on-premise solution or even using cloud solutions such as container orchestration as GKE (Google Kubernetes Engine) https://cloud.google.com/kubernetes-engine/The solution presented below show the prediction done trought one json information passed to the URL &emsp; This API could be deployed in any of the 3 options detailed above<jupyter_code>from IPython.display import Image
Image(filename='./data/Online-Prediction.PNG')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06856613388927496, 'fraction_numerical': 0.00857076673615937, 'mean_word_length': 5.9533011272141705, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '827', 'license': 'MIT', 'n_tokens_mistral': 1287, 'n_tokens_neox': 1238, 'n_words': 455, 'path': 'Marketing_Campaign_optimization/bin/Phase_3_Deployment_options.ipynb', 'repo_name': 'ThiagoBarsante/DataScience_projects', 'size': '4317'}