Regular Batch Inference
The following tutorials are available from the Wallaroo Tutorials Repository.
Stage 4: Regular Batch Inference
In Stage 3: Deploy the Model in Wallaroo, the housing model created and tested in Stage 2: Training Process Automation Setup was uploaded to a Wallaroo instance and added to the pipeline housing-pipe
in the workspace housepricing
. This pipeline can be deployed at any point and time and used with new inferences.
For the purposes of this demo, let’s say that every month we find the newly entered and still-unsold houses and predict their sale price.
The predictions are entered into a staging table, for further inspection before being joined to the primary housing data table.
We show this as a notebook, but this can also be scripted and scheduled, using CRON or some other process.
Resources
The following resources are used as part of this tutorial:
- data
data/seattle_housing_col_description.txt
: Describes the columns used as part data analysis.data/seattle_housing.csv
: Sample data of the Seattle, Washington housing market between 2014 and 2015.
- code
postprocess.py
: Formats the data after inference by the model is complete.simdb.py
: A simulated database to demonstrate sending and receiving queries.wallaroo_client.py
: Additional methods used with the Wallaroo instance to create workspaces, etc.
- models
housing_model_xgb.onnx
: Model created in Stage 2: Training Process Automation Setup.
Steps
This process will use the following steps:
- Connect to Wallaroo: Connect to the Wallaroo instance and the
housepricing
workspace. - Deploy the Pipeline: Deploy the pipeline to prepare it to run inferences.
- Read In New House Listings: Read in the previous month’s house listings and submit them to the pipeline for inference.
- Send Predictions to Results Staging Table: Add the inference results to the results staging table.
Connect to Wallaroo
Connect to the Wallaroo instance and set the housepricing
workspace as the current workspace.
import json
import pickle
import wallaroo
import pandas as pd
import numpy as np
import pyarrow as pa
import datetime
import simdb # module for the purpose of this demo to simulate pulling data from a database
from wallaroo_client import get_workspace
# used to display dataframe information without truncating
from IPython.display import display
pd.set_option('display.max_colwidth', None)
Connect to the Wallaroo Instance
The first step is to connect to Wallaroo through the Wallaroo client. The Python library is included in the Wallaroo install and available through the Jupyter Hub interface provided with your Wallaroo environment.
This is accomplished using the wallaroo.Client()
command, which provides a URL to grant the SDK permission to your specific Wallaroo environment. When displayed, enter the URL into a browser and confirm permissions. Store the connection into a variable that can be referenced later.
If logging into the Wallaroo instance through the internal JupyterHub service, use wl = wallaroo.Client()
. For more information on Wallaroo Client settings, see the Client Connection guide.
# Login through local Wallaroo instance
wl = wallaroo.Client()
def get_workspace(name):
workspace = None
for ws in wl.list_workspaces():
if ws.name() == name:
workspace= ws
if(workspace == None):
workspace = wl.create_workspace(name)
return workspace
def get_pipeline(name):
try:
pipeline = wl.pipelines_by_name(name)[0]
except EntityNotFoundError:
pipeline = wl.build_pipeline(name)
return pipeline
workspace_name = 'housepricing'
model_name = "housepricemodel"
model_file = "./housing_model_xgb.onnx"
pipeline_name = "housing-pipe"
new_workspace = get_workspace(workspace_name)
_ = wl.set_current_workspace(new_workspace)
Deploy the Pipeline
Deploy the housing-pipe
workspace established in Stage 3: Deploy the Model in Wallaroo (03_deploy_model.ipynb
).
pipeline = get_pipeline(pipeline_name)
pipeline.deploy()
name | housing-pipe |
---|---|
created | 2023-09-12 17:35:52.273091+00:00 |
last_updated | 2023-09-12 17:37:27.074611+00:00 |
deployed | True |
tags | |
versions | d957ce8d-9d70-477e-bc03-d58b70cd047a, ba8a411e-9318-4ba5-95f5-22c22be8c064, ab42a8de-3551-4551-bc36-9a71d323f81c |
steps | preprocess |
published | False |
Read In New House Listings
From the data store, load the previous month’s house listing, prepare it as a DataFrame, then submit it for inferencing.
conn = simdb.simulate_db_connection()
# create the query
query = f"select * from {simdb.tablename} where date > DATE(DATE(), '-1 month') AND sale_price is NULL"
print(query)
# read in the data
# can't have null values - turn them into 0
newbatch = pd.read_sql_query(query, conn)
newbatch['sale_price'] = newbatch.sale_price.apply(lambda x: 0)
display(newbatch.shape)
display(newbatch.head(10).loc[:, ["id", "date", "list_price", "bedrooms", "bathrooms", "sqft_living", "sqft_lot"]])
select * from house_listings where date > DATE(DATE(), '-1 month') AND sale_price is NULL
(1090, 22)
id | date | list_price | bedrooms | bathrooms | sqft_living | sqft_lot | |
---|---|---|---|---|---|---|---|
0 | 9215400105 | 2023-08-14 | 450000.0 | 3 | 1.75 | 1250 | 5963 |
1 | 1695900060 | 2023-08-27 | 535000.0 | 4 | 1.00 | 1610 | 2982 |
2 | 9545240070 | 2023-08-14 | 660500.0 | 4 | 2.25 | 2010 | 9603 |
3 | 1432900240 | 2023-08-24 | 205000.0 | 3 | 1.00 | 1610 | 8579 |
4 | 6131600075 | 2023-08-13 | 225000.0 | 3 | 1.00 | 1300 | 8316 |
5 | 1400300055 | 2023-08-14 | 425000.0 | 2 | 1.00 | 770 | 5040 |
6 | 7960900060 | 2023-08-20 | 2900000.0 | 4 | 3.25 | 5050 | 20100 |
7 | 6378500125 | 2023-08-17 | 436000.0 | 2 | 1.00 | 1040 | 7538 |
8 | 2022069200 | 2023-08-21 | 455000.0 | 4 | 2.50 | 2210 | 49375 |
9 | 9412900055 | 2023-08-21 | 405000.0 | 3 | 1.75 | 2390 | 6000 |
# query = {'query': newbatch.to_json()}
result = pipeline.infer(newbatch)
# display(result)
predicted_prices = pd.DataFrame(result['out.variable'].apply(lambda x: x[0])).rename(columns={'out.variable':'prediction'})
display(predicted_prices[0:5])
prediction | |
---|---|
0 | 508255.0 |
1 | 500198.0 |
2 | 539598.0 |
3 | 270739.0 |
4 | 191304.0 |
Send Predictions to Results Staging Table
Take the predicted prices based on the inference results so they can be joined into the house_listings
table.
Once complete, undeploy the pipeline to return the resources back to the Kubernetes environment.
result_table = pd.DataFrame({
'id': newbatch['id'],
'saleprice_estimate': predicted_prices['prediction']
})
display(result_table)
result_table.to_sql('results_table', conn, index=False, if_exists='append')
id | saleprice_estimate | |
---|---|---|
0 | 9215400105 | 508255.0 |
1 | 1695900060 | 500198.0 |
2 | 9545240070 | 539598.0 |
3 | 1432900240 | 270739.0 |
4 | 6131600075 | 191304.0 |
... | ... | ... |
1085 | 3304300300 | 577492.0 |
1086 | 6453550090 | 882930.0 |
1087 | 1760650820 | 271484.0 |
1088 | 3345700207 | 537434.0 |
1089 | 7853420110 | 634226.0 |
1090 rows × 2 columns
# Display the top of the table for confirmation
pd.read_sql_query("select * from results_table limit 5", conn)
id | saleprice_estimate | |
---|---|---|
0 | 9215400105 | 508255.0 |
1 | 1695900060 | 500198.0 |
2 | 9545240070 | 539598.0 |
3 | 1432900240 | 270739.0 |
4 | 6131600075 | 191304.0 |
conn.close()
pipeline.undeploy()
name | housing-pipe |
---|---|
created | 2023-09-12 17:35:52.273091+00:00 |
last_updated | 2023-09-12 17:37:27.074611+00:00 |
deployed | False |
tags | |
versions | d957ce8d-9d70-477e-bc03-d58b70cd047a, ba8a411e-9318-4ba5-95f5-22c22be8c064, ab42a8de-3551-4551-bc36-9a71d323f81c |
steps | preprocess |
published | False |
From here, organizations can automate this process. Other features could be used such as data analysis using Wallaroo assays, or other features such as shadow deployments to test champion and challenger models to find which models provide the best results.