Prediction Operators
Learning Objectives​
- Understand Prediction Operator
What is Prediction Operator​
Prediction Operator is a tool to make, adjust, or generate a prediction.
- Make: you can call a prediction within the platform and receive a result.
- Adjust: it allows you to adjust the data before making the prediction, check it for suitability, or post-process the results.
- Generate: it enables the generation of additional predictions.
Main parts of Prediction Operator​
Prediction Operator consists of:
- Initialize: a function that is used during the initialization of prediction operator deployment to upload all the necessary data for its operation.
- Predict: a function that makes predictions and contains the logic of processing them.
Create a Prediction Operator​
We will use a classic example of predicting iris species from an iris dataset in this example.
-
First, let's create a project that will make a prediction of species and deploy its model.
- To create a Prediction Operator in Abacus.AI, we need to use the API.
- Inside the project, you go to Deployments>Prediction Operator>Create Prediction Operator (blue button in the top right corner)
- There we give it a name and press “Create New Prediction Operator”.
- This should open an Abacus.AI notebook with the prepopulated ID of the prediction operator we are creating and the project we are using. This notebook is a base example of how to create and update a Prediction Operator.
-
Then, we will update initialize function that will return average measurements by species in training data, (note that “fg” is passed as pandas dataframe and there is no need to convert it):
def initialize(fg):
return fg.groupby('species').mean().to_dict('index')
- Next, update predict function. It consists of checking data. Usage of initialized dataset, and improvement of it by AI for readability. Please notice that the query parameter is expected to be in dict format. Also all the parameters that will be used need to be initialized inside the function (for example we need to provide
project_idagain while it is present in notebook)
def predict(data, query):
from abacusai import ApiClient, ApiException
client = ApiClient()
project_id = 'your_project_id'
#check if data have required columns
missing_columns = set(['petal_length', 'petal_width', 'sepal_length', 'sepal_width']) - set(query.keys())
if missing_columns:
return client.evaluate_prompt(f'Tell that AI misses {missing_columns} columns to make a decision', temperature=0.5).content
#get deployment token
dep_token = client.list_deployment_tokens(project_id)[0]
#make prediction with model
predictions = client.predict(dep_token, 'dfjsaddf', query)
#convert it to the most probable specimen based on prediction
speciment = max(predictions['species'], key=predictions['species'].get)
#return answer enhanced by AI for readability
return client.evaluate_prompt(f'Tell that for parameters {query} AI thinks it is likely {speciment}. Usually this specimen average measurments are {data[speciment]}', temperature=0.5).content
- To check that everything works, we need to run following commands:
fg = client.describe_feature_group_by_table_name('iris')
data = initialize(fg.load_as_pandas())
print(predict(data,dat.iloc[115,:].to_dict()))
- If everything works let’s update the prediction Operator with our functions:
operator = client.update_prediction_operator_from_functions(
prediction_operator_id=prediction_operator_id,
initialize_function=initialize,
predict_function=predict,
# If multiple, the order needs to match the initialize function
feature_group_ids=[fg.id],
package_requirements=[],
# choose one of 16, 32, 64 and 128
memory=16
)
operator
- And deploy it:
deployment = client.deploy_prediction_operator(prediction_operator_id=prediction_operator_id)
When the prediction operator is deployed, it is possible to check it inside the platform and get suggestions on Python/curl code for further usage inside the target platform. For this go to Deployments>Prediction API, select your Prediction Operator deployment in top dropout. Then you can change between python and curl API. You may use default values to see our check for values working, or try actual values like
{"sepal_length":6.4,"sepal_width":3.2,"petal_length":5.3,"petal_width":2.3}