Page cover

💻ML API's

SDK has methods which can be used to upload dataset and add logs to the dataset

Initialization

Create a project first to get the project_id.

The first step is to instantiate an Client object with an API key and a project ID. This is successively used to authenticate every call made using the SDK.

from censius.ml import CensiusClient, ModelType, DatasetType, ExplanationType, Dataset

client = CensiusClient(api_key = *YOUR_API_KEY*, project_id = *YOUR_PROJECT_ID*)

Register Dataset

register_dataset()

You can use the register_dataset API to register a dataset to the Censius platform.

Download the Titanic dataset that we use in the example here.

Arguments
Type
Description

name

string

The name of the dataset

Required

file

dataframe

File that stores feature values. Right now, only CSV files are supported.

Required

features

list<dict>

The list of columns for the dataset. It is a list of dictionaries, each containing two keys, one name and the other type. Valid values of type is DatasetType.STRING, DatasetType.INT, DatasetType.BOOLEAN, DatasetType.DECIMAL.

Required

timestamp

dict

The default timestamp column to be processed, if it is part of the dataset. The accepted timestamp types are DatasetType.UNIX_MS which represent unix format in milliseconds

Optional

Optional for categorical variable representation for feature

  • categorical = Boolean

  • category_map = Map(key, actual)

register_model()

You can use this API to register a new model to the Censius platform. For subsequent updates to the model with new versions, register_new_model_version() should be called.

Arguments
Type
Description
Mandate

model_id

string

The ID of the model

Required

model_name

string

The name of the model

Required

model_type

enum

This is the type of the targets of the model. Currently supported value is ModelType.BINARY_CLASSIFICATION and ModelType.REGRESSION

Required

model_version

string

A string to represent the version of the model

Required

training_info

dict

Recording the ID of the dataset the model is trained on

Required

targets

list<string>

These are the columns the model predicts.

Required

features

list<string>

They are the columns the model uses to predict the targets

Required

register_new_model_version()

You can use this API to add a new version to an existing model. Example, “v2” of a model.

Arguments
Type
Description

model_id

string

The ID of the model

Required

model_version

string

A string to represent the version of the model

Required

training_info

dict

Recording the ID of the dataset the model is trained on

Required

targets

list<string>

These are the columns the model predicts.

Required

features

list<string>

They are the columns the model uses to predict the targets

Required

Logging predictions, features, and explanations

log()

This function enables logging individual predictions, features (and optionally explanations). It can be integrated as part of the production environment to log these values as predictions are made.

Arguments
Type
Description

prediction_id

string

The ID of this prediction log. This can be used to update the actual of this log later

Required

model_id

string

The model ID against which you want to log the prediction

Required

model_version

string

The version of the model against which you want to log the prediction

Required

features

dict

A dict with feature names as keys and processed feature values as values.

Required

prediction

dict

A dictionary containing feature headings as keys, and a dict that contains two keys, label and optionally, confidence as values. For example, ”Loan Status”: {”label”: 2, "confidence": 0.2}

Required

timestamp

int

UNIX epoch timestamp in milliseconds or time.time.now() to indicate the current time.

Required

actual

dict

A dictionary containing actual for the prediction log. The keys are the target features and the values are the ground truth values of the feature.

Optional

When using time.time.now() remember that the time is calculated in UTC on the client side, not the server side.

Logs are currently being aggregated every 60 mins by default. This can be changed in custom deployments—reach out to us if you need a different frequency.


log_actual()

If the actual wasn't available when log() was called, it can be updated at a later time using log_actual(). This can be the case for certain types of models where the ground truth isn't immediately available.

Arguments
Type
Description

prediction_id

int

The prediction ID against which you want to update the actual

Required

actual

dict

A dictionary containing actual to be updated. The keys are the target feature headings and the values are the ground truth values of the feature.

Required

model_id

string

The model ID for the prediction for which you need to update the actual

Required

model_version

string

The model version for the prediction for which you need to update the actual

Required



log_explanations()

Arguments
Type
Description

prediction_id

int

The prediction ID against which you want to update the actual

Required

model_id

string

The model ID for the prediction for which you need to update the actual

Required

model_version

string

The model version for the prediction for which you need to update the actual

Required

explanation_type

enum

The type of explanation. Currently supports ExplanationType.SHAP

Required

explanation_values

dict

A dictionary containing features and their explanations. The keys are the target feature headings and the values are the explanation values.

Required


Bulk Log Insertion

bulk_log()

This function enables you to send the predictions, actuals, and explanations logs in bulk. It can be integrated as part of the production environment where you are collecting the model logs and send them all together in a single insertion call (something like once-in-a-day frequency).

💡

The following cases have to be part of bulk_log calls

  • All logs (predictions, actuals, and explanations) details must be present in the bulk_log call.

  • Combination of predictions, and explanations.

  • Just the predictions.

  • Just the actuals (predictions have to be sent prior)

  • Combination of *actuals, and explanations. (predictions have to be sent prior) Note: Just the explanation logging is not accepted

Arguments
Type
Description

input

Pandas Dataframe

Pandas DataFrame of bulk logs containing predictions, actuals, and explanations values.

Required

model_id

string

The model ID against which you want to log the bulk insertion.

Required

model_version

string

The version of the model for which you want to log the bulk insertion.

Required

prediction_id_column

string

Name of the <ID> column in input DataFrame. The values of this columns must be NOT NULL & Unique.

Required

predictions

object

The object used is Prediction.Tabular this collect information regarding the predictions and feature columns in the input DataFrame. More details in Prediction.Tabular table below.

optional

actuals

string

Name of the column in input DataFrame which refers to the values of Actual.

optional

explanations

object

The object used is Explanation.Tabular this collect information regarding the explanations values, explanation type, and feature columns in the input DataFrame. More details in Explanation.Tabular table below.

optional

Prediction.Tabular

Arguments
Type
Description

timestamp_column

timestamp

Name of the column which specify the timestamp for each prediction in the input DataFrame.

Required

prediction_column

string

Name of the column which specify the Prediction values in the input DataFrame. This column must be NOT NULL.

Required

prediction_confidence_column

float

Name of the column which specify the prediction_score value in the input DataFrame. This column must be NOT NULL.

Required

features

list<object>

List of object with a mapping of registered features to column names in the input DataFrame. Example: {"feature": "Age" , "input_column": "age_in_years"}

Here, “Age” was mentioned while registering model, and “age_in_years” is a column in DataFrame which corresponds to “Age” feature values in bulk_logs. | Optional

Explanation.Tabular

Arguments
Type
Description

type

enum

The type of explanation. Currently supports ExplanationType.SHAP

Required

explanation_mapper

list<object>

List of object with a mapping of registered features to column names in the input DataFrame. Example: {"feature": "Age" , "input_column": "age_shap"}

Here, “Age” was mentioned while registering model and “age_shap” is a column in DataFrame which corresponds to SHAP values of “Age” feature in bulk_logs.

Required

Download the Titanic dataset bulk log sample file here.

Last updated

Was this helpful?