Predictions
In this section you will learn how to use your model do precitions over new data and data stored on your database. The examples below assume a table named iris as follow:
With your model prepared you can easily make predictions inside your database with pgpyml. You can use the predict function to apply your model over the data.
The predict function is the easiest way to deploy your model, you only need to specificate the path on your disk to the model and pass and array of features. This function can be used to predict a single instance or multiple instances at same time. The features must be passed as a nested array, like you usually do in sklearn.
The predict function expects two arguments, the first argument is the path to your trained model, this path must be reachable by your Postgres server. The second argument is a nested array of instances to be predicted, each element of the array will have an element on the output. The output are an text array with the predictions of your model.
Casts
The predict function gives the output as an array of text, this way you can cast the produced output to the suitable type. You can use the "casted" version of predict to obtain the output casted to int or real.
In-table Prediction
You may want to use your model over data that are already stored on your database. To do that you can use the predict_table_row function.
This function expects as the first argument the model you want to use, the second argument is the name of the table where the data is stored, the third argument is an array with the name of the columns that will be used as features by your model, and finally the forth argument is the id of the row you want to classify:
Predict on Insertion
You can use pgpyml to predict data on insertion. You can create a trigger to classify the new data and populate a column on the same table.
Use the classification_trigger function to create a trigger and classify the data when a insert or update occurs:
The first argument is the path to the model, the second argument is the name of the column where the prediction will be stored. Any argument after the second one will be used as the name of the column that holds the features values to be used by the model.
After creating the trigger you can insert new data on the table, and the result of the classification will be saved on the column specified in the second argument:
Next Steps
You can read more about loading and unloading models in the IO Page.