Skip to content

バッチ予測

The batch prediction API provides a way to score large datasets using flexible options for intake and output on the Prediction Servers you have already deployed.

The main features are:

  • Flexible options for intake and output.
  • Stream local files and start scoring while still uploading and simultaneously downloading the results.
  • S3間の大きなデータセットのスコアリング。
  • Connect to your database using JDBC with bidirectional streaming of scoring data and results.
  • Intake and output options can be mixed and do not need to match. So scoring from a JDBC source to an S3 target is also an option.
  • Protection against overloading your prediction servers with the option to control the concurrency level for scoring.
  • Prediction explanations can be included (with the option to add thresholds).
  • Passthrough columns are supported to correlate scored data with source data.
  • You can include prediction warnings in the output.

To interact with batch predictions, see the BatchPredictionJob class.

Make batch predictions with a deployment

DataRobot provides a utility function to make batch predictions using a deployment: Deployment.predict_batch.

import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
# To note: `source` can be a file path, a file or a pandas DataFrame
prediction_results_as_dataframe = deployment.predict_batch(
    source="./my_local_file.csv",
) 

Scoring local CSV files

DataRobot provides a utility function for scoring to and from local CSV files: BatchPredictionJob.score_to_file. The first parameter can be either:

  • A path to a CSV dataset
  • A file-like object
  • A Pandas DataFrame

For larger datasets, you should avoid using a DataFrame, as it loads the entire dataset into memory. The other options do not.

import datarobot as dr

deployment_id = '5dc5b1015e6e762a6241f9aa'

dr.BatchPredictionJob.score_to_file(
    deployment_id,
    './data_to_predict.csv',
    './predicted.csv',
) 

The input file is streamed to DataRobot’s API and scoring starts immediately. As soon as results start coming in, they start to be downloaded. The entire call is blocked until the file has been scored.

Scoring from and to S3

DataRobot provides a small utility function for scoring to and from CSV files hosted on S3: BatchPredictionJob.score_s3. This requires that the intake and output buckets share the same credentials (see Credentials and Credential.create_s3) or that their access policy is set to public:

Note that the S3 output functionality has a limit of 100 GB.

import datarobot as dr

deployment_id = '5dc5b1015e6e762a6241f9aa'

cred = dr.Credential.get('5a8ac9ab07a57a0001be501f')

job = dr.BatchPredictionJob.score_s3(
    deployment=deployment_id,
    source_url='s3://mybucket/data_to_predict.csv',
    destination_url='s3://mybucket/predicted.csv',
    credential=cred,
) 

Scoring from and to Azure Cloud Storage

DataRobot provides the same support for Azure through the utility function BatchPredictionJob.score_azure. This requires that you add an Azure connection string to the DataRobot credentials store. (see Credentials and Credential.create_azure)

import datarobot as dr

deployment_id = '5dc5b1015e6e762a6241f9aa'

cred = dr.Credential.get('5a8ac9ab07a57a0001be501f')

job = dr.BatchPredictionJob.score_azure(
    deployment=deployment_id,
    source_url='https://mybucket.blob.core.windows.net/bucket/data_to_predict.csv',
    destination_url='https://mybucket.blob.core.windows.net/results/predicted.csv',
    credential=cred,
) 

Scoring from and to Google Cloud Platform

DataRobot provides the same support for GCP through the utility function BatchPredictionJob.score_gcp. It requires you to add a GCP connection string to the DataRobot credentials store. (See Credentials and Credential.create_gcp.)

import datarobot as dr

deployment_id = '5dc5b1015e6e762a6241f9aa'

cred = dr.Credential.get('5a8ac9ab07a57a0001be501f')

job = dr.BatchPredictionJob.score_gcp(
    deployment=deployment_id,
    source_url='gs:/bucket/data_to_predict.csv',
    destination_url='gs://results/predicted.csv',
    credential=cred,
) 

Manually configure a batch prediction job

If you can’t use any of the utilities above, you are also free to manually configure your job. これには、BatchPredictionJob.scoreintake_settingsおよびoutput_settingsパラメーターを使用して入力と出力のオプションを設定する必要があります。 Credentials may be created with Credentials API.

import datarobot as dr

deployment_id = '5dc5b1015e6e762a6241f9aa'

dr.BatchPredictionJob.score(
    deployment_id,
    intake_settings={
        'type': 's3',
        'url': 's3://public-bucket/data_to_predict.csv',
        'credential_id': '5a8ac9ab07a57a0001be501f',
    },
    output_settings={
        'type': 'localFile',
        'path': './predicted.csv',
    },
) 

Supported intake types

typeフィールドでは、入力アダプターを選択します。 サポートされている値はlocalFiles3azuregcpdatasetjdbcsnowflakesynapsebigquery、およびdatasphereです。 入力と出力のタイプを混在させることができます(例: JDBCから入力しS3へ出力)。

以下のセクションでは、各入力タイプの設定パラメーターについて説明します。

Local file intake

typelocalFileに設定し、fileを通じてスコアリングデータを渡します。fileには、ファイルのようなオブジェクト、CSVファイルへの文字列パス、またはpandas.DataFrameを指定できます。

intake_settings={
    'type': 'localFile',
    'file': './data_to_predict.csv',
} 

S3 CSV intake

S3からのCSV入力では、スコアリングするCSVファイルのS3 URLをurlパラメーターで渡す必要があります。 オプションパラメーター:

  • credential_id:str、保存されているAWS認証情報のID(Credential APIを参照)。
  • endpoint_url:str、デフォルト以外のS3エンドポイントURL。 デフォルトのAWSエンドポイントを使用する場合は省略します。
intake_settings={
    'type': 's3',
    'url': 's3://public-bucket/data_to_predict.csv',
} 

If the bucket is not publicly accessible, you can supply AWS credentials using the following parameters:

  • aws_access_key_id
  • aws_secret_access_key
  • aws_session_token

Save it to the Credential API:

import datarobot as dr

# get to make sure it exists
credential_id = '5a8ac9ab07a57a0001be501f'
cred = dr.Credential.get(credential_id)

intake_settings={
    'type': 's3',
    'url': 's3://private-bucket/data_to_predict.csv',
    'credential_id': cred.credential_id,
} 

JDBC intake

JDBCからの入力では、データベースにデータストア資格情報を作成する必要があります。 データソースを、SQLのquery、またはtableschemacatalogの組み合わせのいずれかで指定します。

  • data_store_id:str、JDBCデータソースに接続されている外部データストアのID。
  • query:str(tableschemacatalogのいずれかが指定された場合はオプション)、予測するデータのSELECT文。
  • table:str(queryが指定されている場合はオプション)、データベーステーブル名。
  • schema:str(queryが指定されている場合はオプション)、データベーススキーマ名。
  • catalog:str(queryが指定されている場合はオプション)、データベースカタログ名(v2.22で追加)。
  • fetch_size:Optional[int]、データベースから読み込む際の行単位のバッチサイズ。 スループットとメモリー使用量のバランスが取れるように調整します。
  • credential_id:Optional[str]、読み取り権限を持つ資格情報のID(資格情報を参照)。
# get to make sure it exists
datastore_id = '5a8ac9ab07a57a0001be5010'
data_store = dr.DataStore.get(datastore_id)

credential_id = '5a8ac9ab07a57a0001be501f'
cred = dr.Credential.get(credential_id)

intake_settings = {
    'type': 'jdbc',
    'table': 'table_name',
    'schema': 'public', # optional, if supported by database
    'catalog': 'master', # optional, if supported by database
    'data_store_id': data_store.id,
    'credential_id': cred.credential_id,
} 

Azure Blob Storageからの入力

Azureからの入力では、S3からの入力と同じパラメーターを使用しますが、urlにはAzure Blob StorageのURLを指定します。 See BatchPredictionJob.score_azure for a utility method, or configure manually:

intake_settings={
    'type': 'azure',
    'url': 'https://storage_account.blob.core.windows.net/container/data_to_predict.csv',
    'credential_id': '5a8ac9ab07a57a0001be501f',
} 

Google Cloud Storageからの入力

GCPからの入力では、S3からの入力と同じパラメーターを使用しますが、urlにはGCSのURLを指定します。 See BatchPredictionJob.score_gcp for a utility method, or configure manually:

intake_settings={
    'type': 'gcp',
    'url': 'gs://bucket/data_to_predict.csv',
    'credential_id': '5a8ac9ab07a57a0001be501f',
} 

SnowflakeやSynapseからの入力

For Snowflake and Synapse data sources, set type to snowflake or synapse and use the same parameters as JDBC intake.

BigQuery intake

BigQuery intake requires you to create a GCS Credential for your database:

# get to make sure it exists
credential_id = '5a8ac9ab07a57a0001be501f'
cred = dr.Credential.get(credential_id)

intake_settings = {
    'type': 'bigquery',
    'dataset': 'dataset_name',
    'table': 'table_or_view_name',
    'bucket': 'bucket_in_gcs',
    'credential_id': cred.credential_id,
} 

AI Catalog intake

typedatasetに設定し、スコアリングデータをdr.Datasetオブジェクトとしてdatasetパラメーターに渡します。 typedatasetの両方を設定する必要があります。データセットIDの文字列のみを渡すことには対応していません。

データセットを作成し、dr.Dataset.get()で取得します。

# get to make sure it exists
dataset_id = '5a8ac9ab07a57a0001be501f'
dataset = dr.Dataset.get(dataset_id)

intake_settings={
    'type': 'dataset',
    'dataset': dataset
} 

Or, if you want a version_id other than the latest, supply your own.

# get to make sure it exists
dataset_id = '5a8ac9ab07a57a0001be501f'
dataset = dr.Dataset.get(dataset_id)

intake_settings={
    'type': 'dataset',
    'dataset': dataset,
    'dataset_version_id': 'another_version_id'
} 

Datasphere intake

Datasphereからの入力では、データベースにデータストア資格情報を作成する必要があります。 typedatasphereに設定し、以下のパラメーターを渡します。

パラメーター タイプ 必須 説明
data_store_id str はい Datasphereデータソースに接続されている外部データストアのID。
table str はい データベーステーブルの名前。
schema str はい データベーススキーマの名前。
credential_id str はい Datasphereデータソースへの読み取り権限を持つ資格情報のID。
# get to make sure it exists
datastore_id = '5a8ac9ab07a57a0001be5011'
data_store = dr.DataStore.get(datastore_id)

credential_id = '5a8ac9ab07a57a0001be501f'
cred = dr.Credential.get(credential_id)

intake_settings = {
    'type': 'datasphere',
    'table': 'table_name',
    'schema': 'DATASPHERE_SPACE_NAME',
    'data_store_id': data_store.id,
    'credential_id': cred.credential_id,
} 

DSS(トレーニングデータ)からの入力

リーダーボードモデルを使用してトレーニングデータのサブセットを評価するには、入力タイプdssを使用します。 また、typetrainingに設定してtimeseries_settingsを設定する必要があります。

  • project_id:str、トレーニングデータを取得するプロジェクト。 プロジェクトへのアクセス権が必要です。
  • partition:str、スコアリングの対象となるトレーニングデータのサブセット。datarobot.enums.TrainingDataSubsetsのいずれか。
intake_settings = {
    'type': 'dss',
    'project_id': '5a8ac9ab07a57a0001be5010',
    'partition': 'holdout',
} 

Supported output types

typeフィールドでは、出力アダプターを選択します。 サポートされている値はlocalFiles3azuregcpjdbcsnowflakesynapsebigquery、およびdatasphereです。

以下のセクションでは、各出力タイプの設定パラメーターについて説明します。

Local file output

typelocalFileに設定します。 オプションのpathパラメーターは、結果の取得方法を制御します。

パラメーター タイプ 必須 説明
path str いいえ スコアリングされたデータをCSVとして保存するパス。

path指定されていない場合、アップロードの完了後にBatchPredictionJob.scoreが返され、BatchPredictionJob.downloadを使用して結果を自分でダウンロードする必要があります。

path指定されている場合、呼び出しはジョブが終了するまでブロックされます。 対象の予測インスタンスで他のジョブが処理されていない場合、アップロード、スコアリング、およびダウンロードは、ジョブ全体の完了を待たずに並列で実行されます。 それ以外の場合、呼び出しは引き続きブロックされますが、生成されるとすぐにスコアリングされたデータのダウンロードを開始します。 pathを指定するのが、予測結果を最も早く取得する方法です。

output_settings={
    'type': 'localFile',
    'path': './predicted.csv',
} 

あるいは、path を省略し、ジョブ完了後に結果をダウンロードします。 ジョブのスコアリングが完了していない場合、BatchPredictionJob.downloadを呼び出すと、これまでにスコアリングされたデータがストリーミングされ、それ以上のデータが使用可能になるまでブロックされます。

You can poll for job completion using BatchPredictionJob.get_status or use BatchPredictionJob.wait_for_completion to wait.

import datarobot as dr

deployment_id = '5dc5b1015e6e762a6241f9aa'

job = dr.BatchPredictionJob.score(
    deployment_id,
    intake_settings={
        'type': 'localFile',
        'file': './data_to_predict.csv',
    },
    output_settings={
        'type': 'localFile',
    },
)

job.wait_for_completion()

with open('./predicted.csv', 'wb') as f:
    job.download(f) 

S3 CSV output

S3のCSV出力では、スコアリングされたデータが保存されるCSVファイルのS3 URLをurlパラメーターで渡す必要があります。 オプションパラメーター:

  • credential_id:str、保存されているAWS認証情報のID(Credential APIを参照)。
  • endpoint_url:str、デフォルト以外のS3エンドポイントURL。 デフォルトのAWSエンドポイントを使用する場合は省略します。
output_settings={
    'type': 's3',
    'url': 's3://public-bucket/predicted.csv',
} 

Most likely, the bucket is not publicly accessible for writes, but you can supply AWS credentials using these parameters:

  • aws_access_key_id
  • aws_secret_access_key
  • aws_session_token

Save it to the Credential API. 次に例を示します。

# get to make sure it exists
credential_id = '5a8ac9ab07a57a0001be501f'
cred = dr.Credential.get(credential_id)

output_settings={
    'type': 's3',
    'url': 's3://private-bucket/predicted.csv',
    'credential_id': cred.credential_id,
} 

JDBC output

Just as for the input, JDBC output requires you to create a DataStore and Credential for your database. You must also specify statement_type, which should be one of datarobot.enums.AVAILABLE_STATEMENT_TYPES:

  • data_store_id:str、JDBCデータソースに接続されている外部データストアのID。
  • table:str、データベーステーブル名。
  • schema:Optional[str]、データベーススキーマ名。
  • catalog:Optional[str]、データベースカタログ名(v2.22で追加)。
  • statement_type:str、作成するINSERT文のタイプ。
  • update_columns:list[string](オプション)、statement_typeがupdateまたはupsertのバリエーションである場合に更新する列名。
  • where_columns:list[string](オプション)、statement_typeがinsertまたはupdateの場合、WHERE句で使用される列名。
  • credential_id:str、書き込み権限を持つ資格情報のID(資格情報を参照)。
# get to make sure it exists
datastore_id = '5a8ac9ab07a57a0001be5010'
data_store = dr.DataStore.get(datastore_id)

credential_id = '5a8ac9ab07a57a0001be501f'
cred = dr.Credential.get(credential_id)

output_settings = {
    'type': 'jdbc',
    'table': 'table_name',
    'schema': 'public', # optional, if supported by database
    'catalog': 'master', # optional, if supported by database
    'statement_type': 'insert',
    'data_store_id': data_store.id,
    'credential_id': cred.credential_id,
} 

BigQuery output

Just as for the input, BigQuery requires you to create a GCS Credential to access BigQuery:

# get to make sure it exists
credential_id = '5a8ac9ab07a57a0001be501f'
cred = dr.Credential.get(credential_id)

output_settings = {
    'type': 'bigquery',
    'dataset': 'dataset_name',
    'table': 'table_name',
    'bucket': 'bucket_in_gcs',
    'credential_id': cred.credential_id,
} 

Datasphere output

Datasphereへの出力では、データベースにデータストア資格情報を作成する必要があります。 typedatasphereに設定し、以下のパラメーターを渡します。

パラメーター タイプ 必須 説明
data_store_id str はい Datasphereデータソースに接続されている外部データストアのID。
table str はい データベーステーブルの名前。
schema str はい データベーススキーマの名前。
credential_id str はい Datasphereのデータソースへの書き込み権限を持つ資格情報のID。
# get to make sure it exists
datastore_id = '5a8ac9ab07a57a0001be5010'
data_store = dr.DataStore.get(datastore_id)

credential_id = '5a8ac9ab07a57a0001be501f'
cred = dr.Credential.get(credential_id)

output_settings = {
    'type': 'datasphere',
    'table': 'table_name',
    'schema': 'DATASPHERE_SPACE_NAME',
    'data_store_id': data_store.id,
    'credential_id': cred.credential_id,
} 

Azure Blob Storageへの出力

Azureへの出力では、S3への出力と同じパラメーターを使用しますが、urlにはAzure Blob StorageのURLを指定します。

output_settings={
    'type': 'azure',
    'url': 'https://storage_account.blob.core.windows.net/container/predicted.csv',
    'credential_id': '5a8ac9ab07a57a0001be501f',
} 

Google Cloud Storageへの出力

GCPへの出力では、S3への出力と同じパラメーターを使用しますが、urlにはGCSのURLを指定します。

output_settings={
    'type': 'gcp',
    'url': 'gs://bucket/predicted.csv',
    'credential_id': '5a8ac9ab07a57a0001be501f',
} 

SnowflakeやSynapseへの出力

For Snowflake and Synapse destinations, set type to snowflake or synapse and use the same parameters as JDBC output.

CSVの設定

csv_settingsBatchPredictionJob.scoreに渡されるオプションの辞書で、入力および出力ファイルのCSV解析を設定します。

  • delimiter:str(オプション、デフォルトは,)、フィールドの区切り文字。 TSVの場合は、文字列tabを使用します。 1文字の文字列またはtabにする必要があります。
  • quotechar:str(オプション、デフォルトは")、区切り文字を含むフィールドを引用符で囲むために使用する文字。
  • encoding:str(オプション、デフォルトはutf-8)、ファイルのエンコーディング(例:shift_jislatin_1、またはmskanji)。

時系列設定

timeseries_settingsは、BatchPredictionJob.scoreおよびBatchPredictionJob.score_with_leaderboard_modelにおける時系列スコアリングを設定します。

  • type:str、forecast(デフォルト)、historical、またはtrainingのいずれか。 forecastは、forecast_pointまたはターゲットのない行を用いて予測を行います。 historicalは、predictions_start_date/predictions_end_dateの範囲内のすべての予測ポイントおよび距離について、予測値を算出します。 trainingは、トレーニングデータのサブセットに対してスコアを算出するものであり、入力タイプdssと組み合わせて使用する必要があります。
  • forecast_point:Optional[datetime.datetime]、データセットの予測ポイント。 省略された場合は、データセットから推測されます。 typeforecastの場合に使用されます。
  • predictions_start_date:Optional[datetime.datetime]、過去の予測の開始日。 省略された場合は、データセットから推測されます。 typehistoricalの場合に使用されます。
  • predictions_end_date:Optional[datetime.datetime]、過去の予測の終了日。 省略された場合は、データセットから推測されます。 typehistoricalの場合に使用されます。
  • relax_known_in_advance_features_check:bool(デフォルトはFalse)。 Trueの場合、予測ウィンドウ内において、事前に既知の特徴量に欠損値が含まれていても許容されます。

BatchPredictionJob.apply_time_series_data_prep_and_scoreでは、typeforecastの場合、forecast_pointが必要です。

スコアリングパラメーター

以下のオプションパラメーターは、BatchPredictionJob.scoreおよび関連メソッドで使用できます。

パラメーター 説明
num_concurrent 同時にスコアリングするチャンクの数。 デフォルトは、デプロイで使用可能なコア数です。 リアルタイムスコアリング用にリソースを確保するには、この値を小さくします。
chunk_size チャンクサイズの戦略、またはバイト単位の固定サイズ。 指定された戦略:auto(フリッパーに応じて固定または動的)、fixed(説明用は1MB、それ以外は5MB)、dynamic。 あるいは、固定のバイトサイズとして整数を指定します。
passthrough_columns 出力に含めるスコアリング列のリスト。 予測をソースデータと関連付ける場合に便利です。
passthrough_columns_set すべてのスコアリング列をそのまま渡すには、allに設定します。 passthrough_columnsよりも優先されます。
max_explanations 予測の説明を計算する特徴量の数。
max_ngram_explanations 計算するN-gramのテキスト説明の数、またはall。 デフォルトでは、N-gramの説明は行われません。
threshold_high このしきい値を上回る予測の説明のみを計算します。 threshold_lowと組み合わせて使用できます。
threshold_low このしきい値を下回る予測の説明のみを計算します。 threshold_highと組み合わせて使用できます。
explanations_mode 多クラスおよびクラスタリングの予測説明のためのモード。 デフォルトでは、予測されたクラスのみを説明します(TopPredictionsMode(1)と同等です)。
prediction_warning_enabled 出力に予測警告を含めます。 連続値モデルでのみサポートされます。
include_prediction_status 出力にprediction_status列を含めます。 デフォルトはFalseです。
skip_drift_tracking このジョブによる予測については、ドリフト追跡をスキップします。 本番環境以外のワークロードに役立ちます。 デフォルトはFalseです。
abort_on_error スコアリングに失敗した行が多すぎる場合は、ジョブを中止します。 すべての行をスコアリングするには、Falseに設定します。 デフォルトはTrueです。
column_names_remapping 出力列名を新しい名前にマッピングする辞書。 列をNoneにマッピングすると、その列は破棄されます。 デフォルトは{}です。
include_probabilities 出力に確率列を返します。 デフォルトはTrueです。
include_probabilities_classes 返すクラス確率列のサブセット。 デフォルトはすべてのクラスです。
prediction_threshold 0.0から1.0までの分類しきい値。このしきい値を超える観測値は、Positiveクラスに分類されます。
download_timeout ローカルファイルのダウンロードが可能になるまで待機する秒数。 無期限に待機するには-1に設定します。 タイムアウトに達した場合、ジョブは中止され、RuntimeError が発生します。
download_read_timeout ダウンロードチャンク間でサーバーからの応答を待機する秒数。
upload_read_timeout ローカルファイルのアップロード後、サーバーからの応答を待機する秒数。

prediction_instance

prediction_instanceは、予測サーバー接続よりも優先されます。 デフォルトはデプロイまたはシステムの設定です。

  • hostName:str、予測インスタンスのホスト名。
  • sslEnabled:bool(オプション、デフォルトはTrue)。 SSLを無効にするには、Falseに設定します。
  • datarobotKey:Optional[str]、マネージドAIクラウドの予測インスタンスにおける組織レベルのDataRobotキー。
  • apiKey:Optional[str]、予測リクエストのAPIキー。 デフォルトはジョブ作成者のAPIキーです。

Copy a previously submitted job

To submit a job using parameters from a job that was previously submitted, use BatchPredictionJob.score_from_existing. The first parameter is the job ID of another job.

import datarobot as dr

previously_submitted_job_id = '5dc5b1015e6e762a6241f9aa'

dr.BatchPredictionJob.score_from_existing(
    previously_submitted_job_id,
) 

Scoring an in-memory Pandas DataFrame

When working with DataFrames, DataRobot provides a method for scoring the data without first writing it to a CSV file and subsequently reading the data back from a CSV file: BatchPredictionJob.score_pandas <datarobot.models.BatchPredictionJob.score_pandas>.

This method also joins the computed predictions into the existing DataFrame. The first parameter is the deployment ID and the second is the DataFrame to score.

import datarobot as dr
import pandas as pd

deployment_id = '5dc5b1015e6e762a6241f9aa'

df = pd.read_csv('testdata/titanic_predict.csv')

job, df = dr.BatchPredictionJob.score_pandas(deployment_id, df) 

The method returns a copy of the job status and the updated DataFrame with the predictions added. So your DataFrame will now contain the following extra columns:

  • Survived_1_PREDICTION
  • Survived_0_PREDICTION
  • Survived_PREDICTION
  • THRESHOLD
  • POSITIVE_CLASS
  • prediction_status
print(df)
     PassengerId  Pclass                                          Name  ... Survived_PREDICTION  THRESHOLD  POSITIVE_CLASS
0            892       3                              Kelly, Mr. James  ...                   0        0.5               1
1            893       3              Wilkes, Mrs. James (Ellen Needs)  ...                   1        0.5               1
2            894       2                     Myles, Mr. Thomas Francis  ...                   0        0.5               1
3            895       3                              Wirz, Mr. Albert  ...                   0        0.5               1
4            896       3  Hirvonen, Mrs. Alexander (Helga E Lindqvist)  ...                   1        0.5               1
..           ...     ...                                           ...  ...                 ...        ...             ...
413         1305       3                            Spector, Mr. Woolf  ...                   0        0.5               1
414         1306       1                  Oliva y Ocana, Dona. Fermina  ...                   0        0.5               1
415         1307       3                  Saether, Mr. Simon Sivertsen  ...                   0        0.5               1
416         1308       3                           Ware, Mr. Frederick  ...                   0        0.5               1
417         1309       3                      Peter, Master. Michael J  ...                   1        0.5               1

[418 rows x 16 columns] 

If you don’t want all of them or if you’re not happy with the names of the added columns, they can be modified using column remapping:

import datarobot as dr
import pandas as pd

deployment_id = '5dc5b1015e6e762a6241f9aa'

df = pd.read_csv('testdata/titanic_predict.csv')

job, df = dr.BatchPredictionJob.score_pandas(
    deployment_id,
    df,
    column_names_remapping={
        'Survived_1_PREDICTION': None,       # discard column
        'Survived_0_PREDICTION': None,       # discard column
        'Survived_PREDICTION': 'predicted',  # rename column
        'THRESHOLD': None,                   # discard column
        'POSITIVE_CLASS': None,              # discard column
    },
) 

Any column mapped to None will be discarded. Any column mapped to a string will be renamed. Any column not mentioned will be kept in the output untouched. Your DataFrame now contains the following extra columns:

  • predicted
  • prediction_status

Refer to the documentation for BatchPredictionJob.score to see the full range of available options.

Batch prediction job definitions

To submit a working Batch Prediction job, you must supply a variety of elements to the datarobot.models.BatchPredictionJob.score() request payload depending on what type of prediction is required. さらに、特定のジョブに使用される入力および出力アダプターのタイプを考慮する必要があります。

Every time a new batch prediction is created, the same amount of information must be stored somewhere outside of DataRobot and resubmitted every time.

NOTE

The name parameter must be unique across your organization. 同じ名前で複数の定義を作成しようとすると、リクエストは失敗します。 If you wish to free up a name, you must first datarobot.models.BatchPredictionJobDefinition.delete() the existing definition before creating this one. Alternatively, you can just datarobot.models.BatchPredictionJobDefinition.update() the existing definition with a new name.

For example, a request could look like:

import datarobot as dr

deployment_id = "5dc5b1015e6e762a6241f9aa"

job = dr.BatchPredictionJob.score(
    deployment_id,
    intake_settings={
        "type": "s3",
        "url": "s3://bucket/container/file.csv",
        "credential_id": "5dc5b1015e6e762a6241f9bb"
    },
    output_settings={
        "type": "s3",
        "url": "s3://bucket/container/output.csv",
        "credential_id": "5dc5b1015e6e762a6241f9bb"
    },
)

job.wait_for_completion()

with open("./predicted.csv", "wb") as f:
    job.download(f) 

ジョブ定義

If your use case requires the same (or similar) type(s) of predictions to be made multiple times, you can choose to create a Job Definition of the batch prediction job and store it for future use.

The method for creating job definitions is datarobot.models.BatchPredictionJobDefinition.create(), which includes the enabled, name, and schedule parameters.

>>> import datarobot as dr
>>> job_spec = {
...    "num_concurrent": 4,
...    "deployment_id": "5dc5b1015e6e762a6241f9aa",
...    "intake_settings": {
...        "url": "s3://foobar/123",
...        "type": "s3",
...        "format": "csv",
...        "credential_id": "5dc5b1015e6e762a6241f9bb"
...    },
...    "output_settings": {
...        "url": "s3://foobar/123",
...        "type": "s3",
...        "format": "csv",
...        "credential_id": "5dc5b1015e6e762a6241f9bb"
...    },
...}
>>> definition = BatchPredictionJobDefinition.create(
...    enabled=False,
...    batch_prediction_job=job_spec,
...    name="some_definition_name",
...    schedule=None
... )
>>> definition
BatchPredictionJobDefinition(foobar) 

Execute a job definition

Manual job execution

To submit a stored job definition for scoring, you can either do so on a scheduled basis, described below, or manually submit the definition ID using datarobot.models.BatchPredictionJobDefinition.run_once():

>>> import datarobot as dr
>>> definition = dr.BatchPredictionJobDefinition.get("5dc5b1015e6e762a6241f9aa")
>>> job = definition.run_once()
>>> job.wait_for_completion() 

Scheduled job execution

A scheduled batch prediction job works just like a regular batch prediction job, but instead DataRobot handles the execution of the job.

In order to schedule the execution of a batch prediction job, a definition must first be created using datarobot.models.BatchPredictionJobDefinition.create(), or updated using datarobot.models.BatchPredictionJobDefinition.update(). In this case, enabled is set to True and a schedule payload is provided.

Alternatively, use a shorthand version with datarobot.models.BatchPredictionJobDefinition.run_on_schedule():

>>> import datarobot as dr
>>> schedule = {
...    "day_of_week": [
...        1
...    ],
...    "month": [
...        "*"
...    ],
...    "hour": [
...        16
...    ],
...    "minute": [
...        0
...    ],
...    "day_of_month": [
...        1
...    ]
...}
>>> definition = dr.BatchPredictionJob.get("5dc5b1015e6e762a6241f9aa")
>>> job = definition.run_on_schedule(schedule) 

If the created job was not enabled previously, this method will also enable it.

The schedule payload

scheduleペイロードは、ジョブを実行する間隔を定義します。これは、必要に応じ、さまざまな方法で組み合わせて複雑なスケジューリング条件を構築することができます。 In all of the elements in the objects, you can supply either an asterisk ["*"] denoting “every” time denomination or an array of integers (e.g. [1, 2, 3]) to define a specific interval.

The schedule payload elements

キー Possible values 説明
["*"] or [0 ... 59] [15, 30, 45] The job will run at these minute values for every hour of the day.
時間 ["*"] or [0 ... 23] [12,23] The hour(s) of the day that the job will run.
["*"] or [1 ... 12] ["jan"] Strings, either 3-letter abbreviations or the full name of the month, can be used interchangeably (e.g., “jan” or “october”).

Months that are not compatible with day_of_month are ignored, for example {"day_of_month": [31], "month":["feb"]}.
day_of_week ["*"] or [0 ... 6] where (Sunday=0) ["sun"] ジョブを実行する曜日。 Strings, either 3-letter abbreviations or the full name of the day, can be used interchangeably (e.g., “sunday”, “Sunday”, “sun”, or “Sun”, all map to [0]).

NOTE: This field is additive with day_of_month, meaning the job will run both on the date specified by day_of_month and the day defined in this field.
day_of_month ["*"] or [1 ... 31] [1, 25] ジョブを実行する日付。 Allowed values are either [1 ... 31] or ["*"] for all days of the month.

NOTE: This field is additive with day_of_week, meaning the job will run both on the date(s) defined in this field and the day specified
by day_of_week (for example, dates 1st, 2nd, 3rd, plus every Tuesday). If day_of_month is set to ["*"] and day_of_week is defined,
the scheduler will trigger on every day of the month that matches day_of_week (for example, Tuesday the 2nd, 9th, 16th, 23rd, 30th).

Invalid dates such as February 31st are ignored.

スケジュールされたジョブを無効化する

ジョブ定義は、enabledTrueに設定された場合にのみ、スケジューラによって実行されます。 If you have a job definition that was previously running as a scheduled job, but should now be stopped, simply datarobot.models.BatchPredictionJobDefinition.delete() to remove it completely, or datarobot.models.BatchPredictionJobDefinition.update() it with enabled=False if you want to keep the definition, but stop the scheduled job from executing at intervals. ジョブが現在実行中の場合、これは関係なく実行を終了します。

>>> import datarobot as dr
>>> definition = dr.BatchPredictionJobDefinition.get("5dc5b1015e6e762a6241f9aa")
>>> definition.delete()