The airt-client library has the following two main classes:
Clientfor authenticating and accessing the airt service, and
DataSourcefor encapsulating data sources such as S3 bucket or a database.
We import them from airt.client module as follows:
from airt.client import Client, DataSource
Before you can use the service, you must acquire a username and password for your developer account. Please fill in the following form to get one:
Upon successfully receiving a username/password pair, you have to call the
Client.get_token method in the
Client class for getting an application token.
The username, password, and server address can either be passed explicitly while calling the
Client.get_token method or stored in environment variables AIRT_SERVICE_USERNAME, AIRT_SERVICE_PASSWORD, and AIRT_SERVER_URL.
Additionally, you can store the database username and password in environment variables AIRT_CLIENT_DB_USERNAME and AIRT_CLIENT_DB_PASSWORD as well, or pass them as parameters to the
The below example assumes the username, password, and server address required for getting an access token is stored in the environment variables AIRT_SERVICE_USERNAME, AIRT_SERVICE_PASSWORD, and AIRT_SERVER_URL respectively.
1. Data Source
DataSource objects are used to encapsulate data access. Currently, we support:
database access for MySql, and
files stored in AWS S3 bucket in the Parquet file format.
We plan to add other databases and storage medium in the future.
To create a data source object, you can call either the
DataSource.db method or
DataSource.s3 method as follows:
data_source_db = DataSource.db( host="db.staging.airt.ai", database="test", table="events" ) data_source_s3 = DataSource.s3( uri="s3://test-airt-service/ecommerce_behavior" )
The objects created in such a way are not checked yet. To check them, you should call
All calls to the library are asynchronous and they return immediately. To manage completion, all methods will return a status object indicating the status of the completion. Alternatively, you can monitor the completion status interactively in a progress bar by calling the
status = data_source_s3.pull() status.progress_bar()
100%|██████████| 1/1 [00:40<00:00, 40.45s/it]
After completition, you can display a head of the data to make sure everything is fine:
The prediction engine is specialized for predicting which clients are most likely to have a specified event in future.
We assume the input data includes the following:
a column identifying a client client_column (person, car, business, etc.),
a colum specifying a type of event we will try to predict target_column (buy, checkout, click on form submit, etc.), and
a timestamp column specifying the time of an occured event.
Each row in the data might have additional columns of int, category, float or datetime type and they will be used to make predictions more accurate. E.g. there could be a city associated with each user or type, credit card used for a transaction, smartphone model used to access a mobile app, etc.
Finally, we need to know how much ahead we wish to make predictions for. E.g. if we predict that a client is most likely to buy a product in the next minute, there is not much we can do anyway. We might be more interested in clients that are most likely to buy a product tomorrow so we can send them a special offer or engage them in some other way. That lead time varies widely from application to application and can be in minutes for a web shop or even several weeks for a banking product such as loan. In any case, there is a parameter predict_after that allows you to specify the time period based on your particular needs.
DataSource.train method is asynchronous and can take a few hours to finish depending on your dataset size. You can check the status by calling the
Model.is_ready method or monitor the completion progress interactively by calling the
In the following example, we will train a model to predict which users will perform a purchase event (*purchase) 3 hours before they acctually do it:
from datetime import timedelta model = data_source_s3.train( client_column="user_id", target_column="event_type", target="*purchase", predict_after=timedelta(hours=3), ) model.progress_bar()
100%|██████████| 5/5 [00:00<00:00, 119.56it/s]
After training is complete, you can check the quality of the model by calling the
Finally, you can run the predictions by calling the
Model.predict method is asynchronous and can take a few hours to finish depending on your dataset size. You can check the status by calling the
Prediction.is_ready method or monitor the completion progress interactively by calling the
predictions = model.predict() predictions.progress_bar()
100%|██████████| 3/3 [00:00<00:00, 78.68it/s]
If the dataset is small enough, you can download the result of prediction locally as a Pandas DataFrame object as follows:
In many case, a much better way is to directly push the result to a data source, in the following case to the AWS S3 bucket:
data_source_pred = DataSource.s3( uri="s3://target-bucket" ) predictions.push(data_source_pred) # Alternatively, this should also work # data_source_s3.push(predictions)