In [1]:
import os
from boto.s3.connection import S3Connection
from boto.s3.key import Key
import h2o
import numpy as np
import pandas as pd
from tabulate import tabulate
from sqlalchemy import create_engine


# initialize the model scoring server
h2o.init(nthreads=1,max_mem_size=1, start_h2o=True, strict_version_check = False)

def predict_churn(State,AccountLength,AreaCode,Phone,IntlPlan,VMailPlan,VMailMessage,DayMins,DayCalls,DayCharge,EveMins,EveCalls,EveCharge,NightMins,NightCalls,NightCharge,IntlMins,IntlCalls,IntlCharge,CustServCalls):
    # connect to the model scoring service
    h2o.init(nthreads=1,max_mem_size=1, start_h2o=True, strict_version_check = False)

    # open the downloaded model
    ChurnPredictor = h2o.load_model(path='AutoML-leader') 

    # define a feature vector to evaluate with the model
    newData = pd.DataFrame({'State' : State,
                            'Account Length' : AccountLength,
                            'Area Code' : AreaCode,
                            'Phone' : Phone,
                            'Int\'l Plan' : IntlPlan,
                            'VMail Plan' : VMailPlan,
                            'VMail Message' : VMailMessage,
                            'Day Mins' : DayMins,
                            'Day Calls' : DayCalls,
                            'Day Charge' : DayCharge,
                            'Eve Mins' : EveMins,
                            'Eve Calls' : EveCalls,
                            'Eve Charge' : EveCharge,
                            'Night Mins' : NightMins,
                            'Night Calls' : NightCalls,
                            'Night Charge' : NightCharge,
                            'Intl Mins' :IntlMins,
                            'Intl Calls' : IntlCalls,
                            'Intl Charge' : IntlCharge,
                            'CustServ Calls' : CustServCalls}, index=[0])
    
    # evaluate the feature vector using the model
    predictions = ChurnPredictor.predict(h2o.H2OFrame(newData))
    predictionsOut = h2o.as_list(predictions, use_pandas=False)
    prediction = predictionsOut[1][0]
    probabilityChurn = predictionsOut[1][1]
    probabilityRetain = predictionsOut[1][2]
    
    engine = create_engine("mysql+mysqldb://brett:"+'Admin123!'+"@104.197.234.210/customers")
    predictionsToDB = h2o.as_list(predictions, use_pandas=True)
    predictionsToDB.to_sql(con=engine, name='predictions', if_exists='append')
    
    return "Prediction: " + str(prediction) + " |Probability to Churn: " + str(probabilityChurn) + " |Probability to Retain: " + str(probabilityRetain)


Checking whether there is an H2O instance running at http://localhost:54321..... not found.
Attempting to start a local H2O server...
  Java Version: java version "1.7.0_151"; OpenJDK Runtime Environment (IcedTea 2.6.11) (7u151-2.6.11-2~deb8u1); OpenJDK 64-Bit Server VM (build 24.151-b01, mixed mode)
  Starting server from /usr/local/lib/python2.7/dist-packages/h2o/backend/bin/h2o.jar
  Ice root: /tmp/tmp1o3K_I
  JVM stdout: /tmp/tmp1o3K_I/h2o_unknownUser_started_from_python.out
  JVM stderr: /tmp/tmp1o3K_I/h2o_unknownUser_started_from_python.err
  Server is running at http://127.0.0.1:54321
Connecting to H2O server at http://127.0.0.1:54321... successful.
H2O cluster uptime: 04 secs
H2O cluster version: 3.16.0.2
H2O cluster version age: 3 months and 21 days !!!
H2O cluster name: H2O_from_python_unknownUser_hzuvv6
H2O cluster total nodes: 1
H2O cluster free memory: 910 Mb
H2O cluster total cores: 8
H2O cluster allowed cores: 1
H2O cluster status: accepting new members, healthy
H2O connection url: http://127.0.0.1:54321
H2O connection proxy: None
H2O internal security: False
H2O API Extensions: AutoML, XGBoost, Algos, Core V3, Core V4
Python version: 2.7.9 final

In [3]:
predict_churn(State="AZ",AccountLength=2,AreaCode="123",Phone="123",IntlPlan="yes",VMailPlan="yes",VMailMessage=25,DayMins=25,DayCalls=25,DayCharge=25,EveMins=25,EveCalls=25,EveCharge=25,NightMins=25,NightCalls=25,NightCharge=25,IntlMins=25,IntlCalls=25,IntlCharge=25,CustServCalls=25)


Checking whether there is an H2O instance running at http://localhost:54321. connected.
H2O cluster uptime: 3 mins 05 secs
H2O cluster version: 3.16.0.2
H2O cluster version age: 3 months and 21 days !!!
H2O cluster name: H2O_from_python_unknownUser_hzuvv6
H2O cluster total nodes: 1
H2O cluster free memory: 910 Mb
H2O cluster total cores: 8
H2O cluster allowed cores: 1
H2O cluster status: locked, healthy
H2O connection url: http://localhost:54321
H2O connection proxy: None
H2O internal security: False
H2O API Extensions: AutoML, XGBoost, Algos, Core V3, Core V4
Python version: 2.7.9 final
Parse progress: |█████████████████████████████████████████████████████████| 100%
gbm prediction progress: |████████████████████████████████████████████████| 100%
Out[3]:
'Prediction: Churn |Probability to Churn: 0.9288465327386934 |Probability to Retain: 0.0711534672613065'