Skip to content

Commit

Permalink
added support files for OAF
Browse files Browse the repository at this point in the history
  • Loading branch information
ksturgeon-td committed Aug 18, 2023
1 parent 7ea4d54 commit 6ff8df2
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions Demo_XGB_Scoring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Load dependency packages
import sys
import csv
import numpy as np
import pandas as pd
from xgboost import XGBClassifier, Booster
import warnings

# pickle will issue a caution warning, if model pickling was done with
# different library version than used here. The following disables any warnings
# that might otherwise show in the scriptlog files on the Advanced SQL Engine
# nodes in this case. Yet, do keep an eye for incompatible pickle versions.
warnings.filterwarnings('ignore')

# Know your data: You must know in advance the number and data types of the
# incoming columns from the SQL Engine database!
# For this script, the input expected format is:
colNames = ['txn_id',
'txn_type_CASH_OUT',
'txn_type_CASH_IN',
'txn_type_TRANSFER',
'txn_type_DEBIT',
'txn_type_PAYMENT',
'txn_type_other',
'amount',
'oldbalanceOrig',
'newbalanceOrig',
'oldbalanceDest',
'newbalanceDest',
'isFraud']



model = XGBClassifier()
booster = Booster()
booster.load_model('xgb_model')
model._Booster = booster


d = csv.DictReader(sys.stdin.readlines(), fieldnames = colNames)

df = pd.DataFrame(d, columns = colNames)

# Use try...except to produce an error if something goes wrong in the try block
try:
# Exit gracefully if DataFrame is empty
if df.empty:
sys.exit()

# Specify the rows to be scored by the model and call the predictor.
X_test = df[['txn_type_CASH_OUT', 'txn_type_CASH_IN','txn_type_TRANSFER', 'txn_type_DEBIT','txn_type_PAYMENT', 'txn_type_other','amount','oldbalanceOrig', 'newbalanceOrig','oldbalanceDest', 'newbalanceDest']].astype(float)

y_prob = model.predict_proba(X_test)
df[['prob_0', 'prob_1']] = y_prob

y_pred = model.predict(X_test)
df['prediction'] = y_pred

# Export results to the Database through standard output.
for index, value in df.iterrows():
my_str = str(value['txn_id']) + ',' + str(value['prob_0']) + ',' + str(value['prob_1']) + ',' + str(value['prediction']) + ',' + str(value['isFraud'])
print(my_str)


except (SystemExit):
# Skip exception if system exit requested in try block
pass
except: # Specify in standard error any other error encountered
print("Script Failure :", sys.exc_info()[0], file=sys.stderr)
raise
sys.exit()
Binary file added xgb_model
Binary file not shown.

0 comments on commit 6ff8df2

Please sign in to comment.