Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ibm
GitHub Repository: ibm/watson-machine-learning-samples
Path: blob/master/cpd5.2/notebooks/python_sdk/deployments/spark/Use Spark to predict credit risk.ipynb
6412 views
Kernel: watsonx-ai-samples-py-312

Use Spark to predict credit risk with ibm-watsonx-ai

This notebook introduces commands for model persistence to watsonx.ai repository, model deployment, and scoring.

Some familiarity with Python is helpful. This notebook uses Python 3.12 and Apache® Spark 3.5 with OpenJDK 17.

Learning goals

The learning goals of this notebook are:

  • Load a CSV file into an Apache® Spark DataFrame.

  • Explore data.

  • Prepare data for training and evaluation.

  • Persist a pipeline and model in watsonx.ai repository from tar.gz files.

  • Deploy a model for online scoring using Wastson Machine Learning API.

  • Score sample scoring data using the watsonx.ai API.

  • Explore and visualize prediction result using the plotly package.

Contents

This notebook contains the following parts:

  1. Set up

  2. Load and explore data

  3. Persist model

  4. Predict locally

  5. Deploy model

  6. Score model

  7. Clean up

  8. Summary and next steps

1. Set up the environment

Before you use the sample code in this notebook, you must perform the following setup tasks:

  • Contact with your Cloud Pak for Data administrator and ask them for your account credentials

Install dependencies

Note: ibm-watsonx-ai documentation can be found here.

%pip install -U wget | tail -n 1 %pip install -U "pyspark==3.5.5" | tail -n 1 %pip install -U ibm-watsonx-ai | tail -n 1
Successfully installed wget-3.2 Successfully installed py4j-0.10.9.7 pyspark-3.5.5 Successfully installed anyio-4.9.0 certifi-2025.4.26 charset-normalizer-3.4.2 h11-0.16.0 httpcore-1.0.9 httpx-0.28.1 ibm-cos-sdk-2.14.1 ibm-cos-sdk-core-2.14.1 ibm-cos-sdk-s3transfer-2.14.1 ibm-watsonx-ai-1.3.18 idna-3.10 jmespath-1.0.1 lomond-0.3.3 numpy-2.2.5 pandas-2.2.3 pytz-2025.2 requests-2.32.2 sniffio-1.3.1 tabulate-0.9.0 typing_extensions-4.13.2 tzdata-2025.2 urllib3-2.4.0

Note: If you're encountering ModuleNotFoundError: No module named 'distutils' exception, install the setuptools package and try again.

Define credentials

Authenticate the watsonx.ai Runtime service on IBM Cloud Pak for Data. You need to provide the admin's username and the platform url.

username = "PASTE YOUR USERNAME HERE" url = "PASTE THE PLATFORM URL HERE"

Use the admin's api_key to authenticate watsonx.ai Runtime services:

import getpass from ibm_watsonx_ai import Credentials credentials = Credentials( username=username, api_key=getpass.getpass("Enter your watsonx.ai API key and hit enter: "), url=url, instance_id="openshift", version="5.2", )

Alternatively you can use the admin's password:

import getpass from ibm_watsonx_ai import Credentials if "credentials" not in locals() or not credentials.api_key: credentials = Credentials( username=username, password=getpass.getpass("Enter your watsonx.ai password and hit enter: "), url=url, instance_id="openshift", version="5.2", )

Create APIClient instance

from ibm_watsonx_ai import APIClient client = APIClient(credentials)

Working with spaces

First of all, you need to create a space that will be used for your work. If you do not have space already created, you can use {PLATFORM_URL}/ml-runtime/spaces?context=icp4data to create one.

  • Click New Deployment Space

  • Create an empty space

  • Go to space Settings tab

  • Copy space_id and paste it below

Tip: You can also use SDK to prepare the space for your work. More information can be found here.

Action: Assign space ID below

space_id = "PASTE YOUR SPACE ID HERE"

You can use list method to print all existing spaces.

client.spaces.list(limit=10)

To be able to interact with all resources available in watsonx.ai, you need to set space which you will be using.

client.set.default_space(space_id)
'SUCCESS'

Test Spark

try: from pyspark.sql import SparkSession except: print( "Error: Spark runtime is missing. If you are using IBM watsonx, change the notebook runtime to Spark." ) raise

2. Load and explore data

In this section you will load the data as an Apache® Spark DataFrame and perform a basic exploration.

The csv file for German Credit Risk is available on the same repository as this notebook. Load the file to Apache® Spark DataFrame using code below.

import os from wget import download sample_dir = "spark_sample_model" if not os.path.isdir(sample_dir): os.mkdir(sample_dir) filename = os.path.join(sample_dir, "credit_risk_training.csv") if not os.path.isfile(filename): filename = download( "https://github.com/IBM/watsonx-ai-samples/raw/master/cpd5.2/data/credit_risk/credit_risk_training.csv", out=sample_dir, )
spark = SparkSession.builder.getOrCreate() df_data = ( spark.read.format("org.apache.spark.sql.execution.datasources.csv.CSVFileFormat") .option("header", "true") .option("inferSchema", "true") .load(filename) )
Setting default log level to "WARN". To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel). 25/05/16 09:56:49 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable

Explore the loaded data by using the following Apache® Spark DataFrame methods:

  • print schema

  • print top ten records

  • count all records

df_data.printSchema()
root |-- CheckingStatus: string (nullable = true) |-- LoanDuration: integer (nullable = true) |-- CreditHistory: string (nullable = true) |-- LoanPurpose: string (nullable = true) |-- LoanAmount: integer (nullable = true) |-- ExistingSavings: string (nullable = true) |-- EmploymentDuration: string (nullable = true) |-- InstallmentPercent: integer (nullable = true) |-- Sex: string (nullable = true) |-- OthersOnLoan: string (nullable = true) |-- CurrentResidenceDuration: integer (nullable = true) |-- OwnsProperty: string (nullable = true) |-- Age: integer (nullable = true) |-- InstallmentPlans: string (nullable = true) |-- Housing: string (nullable = true) |-- ExistingCreditsCount: integer (nullable = true) |-- Job: string (nullable = true) |-- Dependents: integer (nullable = true) |-- Telephone: string (nullable = true) |-- ForeignWorker: string (nullable = true) |-- Risk: string (nullable = true)

As you can see, the data contains 21 fields. Risk field is the one we would like to predict (label).

df_data.show(n=5, truncate=False, vertical=True)
-RECORD 0------------------------------------------ CheckingStatus | 0_to_200 LoanDuration | 31 CreditHistory | credits_paid_to_date LoanPurpose | other LoanAmount | 1889 ExistingSavings | 100_to_500 EmploymentDuration | less_1 InstallmentPercent | 3 Sex | female OthersOnLoan | none CurrentResidenceDuration | 3 OwnsProperty | savings_insurance Age | 32 InstallmentPlans | none Housing | own ExistingCreditsCount | 1 Job | skilled Dependents | 1 Telephone | none ForeignWorker | yes Risk | No Risk -RECORD 1------------------------------------------ CheckingStatus | less_0 LoanDuration | 18 CreditHistory | credits_paid_to_date LoanPurpose | car_new LoanAmount | 462 ExistingSavings | less_100 EmploymentDuration | 1_to_4 InstallmentPercent | 2 Sex | female OthersOnLoan | none CurrentResidenceDuration | 2 OwnsProperty | savings_insurance Age | 37 InstallmentPlans | stores Housing | own ExistingCreditsCount | 2 Job | skilled Dependents | 1 Telephone | none ForeignWorker | yes Risk | No Risk -RECORD 2------------------------------------------ CheckingStatus | less_0 LoanDuration | 15 CreditHistory | prior_payments_delayed LoanPurpose | furniture LoanAmount | 250 ExistingSavings | less_100 EmploymentDuration | 1_to_4 InstallmentPercent | 2 Sex | male OthersOnLoan | none CurrentResidenceDuration | 3 OwnsProperty | real_estate Age | 28 InstallmentPlans | none Housing | own ExistingCreditsCount | 2 Job | skilled Dependents | 1 Telephone | yes ForeignWorker | no Risk | No Risk -RECORD 3------------------------------------------ CheckingStatus | 0_to_200 LoanDuration | 28 CreditHistory | credits_paid_to_date LoanPurpose | retraining LoanAmount | 3693 ExistingSavings | less_100 EmploymentDuration | greater_7 InstallmentPercent | 3 Sex | male OthersOnLoan | none CurrentResidenceDuration | 2 OwnsProperty | savings_insurance Age | 32 InstallmentPlans | none Housing | own ExistingCreditsCount | 1 Job | skilled Dependents | 1 Telephone | none ForeignWorker | yes Risk | No Risk -RECORD 4------------------------------------------ CheckingStatus | no_checking LoanDuration | 28 CreditHistory | prior_payments_delayed LoanPurpose | education LoanAmount | 6235 ExistingSavings | 500_to_1000 EmploymentDuration | greater_7 InstallmentPercent | 3 Sex | male OthersOnLoan | none CurrentResidenceDuration | 3 OwnsProperty | unknown Age | 57 InstallmentPlans | none Housing | own ExistingCreditsCount | 2 Job | skilled Dependents | 1 Telephone | none ForeignWorker | yes Risk | Risk only showing top 5 rows
print("Number of records: " + str(df_data.count()))
Number of records: 5000

As you can see, the data set contains 5000 records.

2.1 Prepare data

In this subsection you will split your data into: train, test and predict datasets.

splitted_data = df_data.randomSplit([0.8, 0.18, 0.02], 24) train_data = splitted_data[0] test_data = splitted_data[1] predict_data = splitted_data[2] print("Number of training records: " + str(train_data.count())) print("Number of testing records : " + str(test_data.count())) print("Number of prediction records : " + str(predict_data.count()))
Number of training records: 4005 Number of testing records : 901 Number of prediction records : 94

As you can see our data has been successfully split into three datasets:

  • The train data set, which is the largest group, is used for training.

  • The test data set will be used for model evaluation and is used to test the assumptions of the model.

  • The predict data set will be used for prediction.

3. Persist model

In this section you will learn how to store your pipeline and model in watsonx.ai repository by using Python client libraries.

Note: Apache® Spark 3.5 is required.

3.1: Save pipeline and model

In this subsection you will learn how to save pipeline and model artifacts to your watsonx.ai instance.

Download pipeline and model archives

import os from wget import download sample_dir = "spark_sample_model" if not os.path.isdir(sample_dir): os.mkdir(sample_dir) pipeline_filename = os.path.join(sample_dir, "credit_risk_spark_pipeline.tar.gz") if not os.path.isfile(pipeline_filename): pipeline_filename = download( "https://github.com/IBM/watsonx-ai-samples/raw/master/cpd5.2/models/spark/credit-risk/model/credit_risk_spark_pipeline.tar.gz", out=sample_dir, ) model_filename = os.path.join(sample_dir, "credit_risk_spark_model.gz") if not os.path.isfile(model_filename): model_filename = download( "https://github.com/IBM/watsonx-ai-samples/raw/master/cpd5.2/models/spark/credit-risk/model/credit_risk_spark_model.gz", out=sample_dir, )

Store piepline and model

To be able to store your Spark model, you need to provide a training data reference, this will allow to read the model schema automatically.

training_data_references = [ { "type": "fs", "connection": {}, "location": {}, "schema": { "id": "training_schema", "fields": [ { "metadata": {}, "name": "CheckingStatus", "nullable": True, "type": "string", }, { "metadata": {}, "name": "LoanDuration", "nullable": True, "type": "integer", }, { "metadata": {}, "name": "CreditHistory", "nullable": True, "type": "string", }, { "metadata": {}, "name": "LoanPurpose", "nullable": True, "type": "string", }, { "metadata": {}, "name": "LoanAmount", "nullable": True, "type": "integer", }, { "metadata": {}, "name": "ExistingSavings", "nullable": True, "type": "string", }, { "metadata": {}, "name": "EmploymentDuration", "nullable": True, "type": "string", }, { "metadata": {}, "name": "InstallmentPercent", "nullable": True, "type": "integer", }, {"metadata": {}, "name": "Sex", "nullable": True, "type": "string"}, { "metadata": {}, "name": "OthersOnLoan", "nullable": True, "type": "string", }, { "metadata": {}, "name": "CurrentResidenceDuration", "nullable": True, "type": "integer", }, { "metadata": {}, "name": "OwnsProperty", "nullable": True, "type": "string", }, {"metadata": {}, "name": "Age", "nullable": True, "type": "integer"}, { "metadata": {}, "name": "InstallmentPlans", "nullable": True, "type": "string", }, {"metadata": {}, "name": "Housing", "nullable": True, "type": "string"}, { "metadata": {}, "name": "ExistingCreditsCount", "nullable": True, "type": "integer", }, {"metadata": {}, "name": "Job", "nullable": True, "type": "string"}, { "metadata": {}, "name": "Dependents", "nullable": True, "type": "integer", }, { "metadata": {}, "name": "Telephone", "nullable": True, "type": "string", }, { "metadata": {}, "name": "ForeignWorker", "nullable": True, "type": "string", }, { "metadata": {"modeling_role": "target"}, "name": "Risk", "nullable": True, "type": "string", }, ], }, } ]
published_model_details = client.repository.store_model( model=model_filename, meta_props={ client.repository.ModelMetaNames.NAME: "Credit Risk model", client.repository.ModelMetaNames.TYPE: "mllib_3.5", client.repository.ModelMetaNames.SOFTWARE_SPEC_ID: client.software_specifications.get_id_by_name( "spark-mllib_3.5" ), client.repository.ModelMetaNames.TRAINING_DATA_REFERENCES: training_data_references, client.repository.ModelMetaNames.LABEL_FIELD: "Risk", }, training_data=train_data, pipeline=pipeline_filename, )
model_id = client.repository.get_model_id(published_model_details) model_id
be89594e-92c1-4a16-b68e-28c31e7ce943
client.repository.get_model_details(model_id)
{'metadata': {'name': 'Credit Risk model', 'space_id': '8a13841b-df99-4b4d-bf2a-161ad2e33980', 'resource_key': 'abf26368-d357-4137-b769-93cf580f7ddd', 'id': 'be89594e-92c1-4a16-b68e-28c31e7ce943', 'created_at': '2025-05-16T07:57:15Z', 'rov': {'member_roles': {'1000331001': {'user_iam_id': '1000331001', 'roles': ['OWNER']}}}, 'owner': '1000331001'}, 'entity': {'software_spec': {'id': 'e8cd7001-fd05-5eea-b697-0a3bff9cf51f'}, 'type': 'mllib_3.5', 'training_data_references': [{'type': 'fs', 'connection': {}, 'location': {}, 'schema': {'id': 'training_schema', 'fields': [{'name': 'CheckingStatus', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'LoanDuration', 'type': 'integer', 'nullable': True, 'metadata': {}}, {'name': 'CreditHistory', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'LoanPurpose', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'LoanAmount', 'type': 'integer', 'nullable': True, 'metadata': {}}, {'name': 'ExistingSavings', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'EmploymentDuration', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'InstallmentPercent', 'type': 'integer', 'nullable': True, 'metadata': {}}, {'name': 'Sex', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'OthersOnLoan', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'CurrentResidenceDuration', 'type': 'integer', 'nullable': True, 'metadata': {}}, {'name': 'OwnsProperty', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'Age', 'type': 'integer', 'nullable': True, 'metadata': {}}, {'name': 'InstallmentPlans', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'Housing', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'ExistingCreditsCount', 'type': 'integer', 'nullable': True, 'metadata': {}}, {'name': 'Job', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'Dependents', 'type': 'integer', 'nullable': True, 'metadata': {}}, {'name': 'Telephone', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'ForeignWorker', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'Risk', 'type': 'string', 'nullable': True, 'metadata': {'modeling_role': 'target'}}]}}], 'schemas': {'input': [{'id': '1', 'type': 'struct', 'fields': [{'name': 'CheckingStatus', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'LoanDuration', 'type': 'integer', 'nullable': True, 'metadata': {}}, {'name': 'CreditHistory', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'LoanPurpose', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'LoanAmount', 'type': 'integer', 'nullable': True, 'metadata': {}}, {'name': 'ExistingSavings', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'EmploymentDuration', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'InstallmentPercent', 'type': 'integer', 'nullable': True, 'metadata': {}}, {'name': 'Sex', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'OthersOnLoan', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'CurrentResidenceDuration', 'type': 'integer', 'nullable': True, 'metadata': {}}, {'name': 'OwnsProperty', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'Age', 'type': 'integer', 'nullable': True, 'metadata': {}}, {'name': 'InstallmentPlans', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'Housing', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'ExistingCreditsCount', 'type': 'integer', 'nullable': True, 'metadata': {}}, {'name': 'Job', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'Dependents', 'type': 'integer', 'nullable': True, 'metadata': {}}, {'name': 'Telephone', 'type': 'string', 'nullable': True, 'metadata': {}}, {'name': 'ForeignWorker', 'type': 'string', 'nullable': True, 'metadata': {}}]}], 'output': []}, 'label_column': 'Risk'}}

Get saved model metadata from watsonx.ai.

Tip: Use client.repository.ModelMetaNames.show() to get the list of available props.

client.repository.ModelMetaNames.show()
------------------------ ---- -------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ META_PROP NAME TYPE REQUIRED SCHEMA NAME str Y DESCRIPTION str N INPUT_DATA_SCHEMA list N {'id(required)': 'string', 'fields(required)': [{'name(required)': 'string', 'type(required)': 'string', 'nullable(optional)': 'string'}]} TRAINING_DATA_REFERENCES list N [{'name(optional)': 'string', 'type(required)': 'string', 'connection(required)': {'endpoint_url(required)': 'string', 'access_key_id(required)': 'string', 'secret_access_key(required)': 'string'}, 'location(required)': {'bucket': 'string', 'path': 'string'}, 'schema(optional)': {'id(required)': 'string', 'fields(required)': [{'name(required)': 'string', 'type(required)': 'string', 'nullable(optional)': 'string'}]}}] TEST_DATA_REFERENCES list N [{'name(optional)': 'string', 'type(required)': 'string', 'connection(required)': {'endpoint_url(required)': 'string', 'access_key_id(required)': 'string', 'secret_access_key(required)': 'string'}, 'location(required)': {'bucket': 'string', 'path': 'string'}, 'schema(optional)': {'id(required)': 'string', 'fields(required)': [{'name(required)': 'string', 'type(required)': 'string', 'nullable(optional)': 'string'}]}}] OUTPUT_DATA_SCHEMA dict N {'id(required)': 'string', 'fields(required)': [{'name(required)': 'string', 'type(required)': 'string', 'nullable(optional)': 'string'}]} LABEL_FIELD str N TRANSFORMED_LABEL_FIELD str N TAGS list N ['string', 'string'] SIZE dict N {'in_memory(optional)': 'string', 'content(optional)': 'string'} PIPELINE_ID str N RUNTIME_ID str N TYPE str Y CUSTOM dict N DOMAIN str N HYPER_PARAMETERS dict N METRICS list N IMPORT dict N {'name(optional)': 'string', 'type(required)': 'string', 'connection(required)': {'endpoint_url(required)': 'string', 'access_key_id(required)': 'string', 'secret_access_key(required)': 'string'}, 'location(required)': {'bucket': 'string', 'path': 'string'}} TRAINING_LIB_ID str N MODEL_DEFINITION_ID str N SOFTWARE_SPEC_ID str N TF_MODEL_PARAMS dict N FAIRNESS_INFO dict N MODEL_LOCATION dict N FRAMEWORK str N VERSION str N ------------------------ ---- -------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3.2: Load model

In this subsection you will learn how to load back saved model from specified instance of watsonx.ai.

from pyspark.ml.pipeline import PipelineModel loaded_model: PipelineModel = client.repository.load(model_id)

Verify that the model was loaded correctly.

assert isinstance(loaded_model, PipelineModel)

4. Predict locally

In this section you will learn how to score test data using loaded model.

4.1: Make local prediction using previously loaded model and test data

In this subsection you will score predict_data data set.

predictions = loaded_model.transform(predict_data)
25/05/16 09:57:59 WARN SparkStringUtils: Truncated the string representation of a plan since it was too large. This behavior can be adjusted by setting 'spark.sql.debug.maxToStringFields'.

Preview the results by calling the show() method on the predictions DataFrame.

predictions.show(5, vertical=True)
-RECORD 0---------------------------------------- CheckingStatus | 0_to_200 LoanDuration | 4 CreditHistory | all_credits_paid_... LoanPurpose | education LoanAmount | 936 ExistingSavings | less_100 EmploymentDuration | less_1 InstallmentPercent | 2 Sex | male OthersOnLoan | none CurrentResidenceDuration | 2 OwnsProperty | savings_insurance Age | 41 InstallmentPlans | bank Housing | rent ExistingCreditsCount | 1 Job | unskilled Dependents | 1 Telephone | none ForeignWorker | yes Risk | No Risk CheckingStatus_IX | 2.0 CreditHistory_IX | 3.0 LoanPurpose_IX | 7.0 ExistingSavings_IX | 0.0 EmploymentDuration_IX | 3.0 Sex_IX | 0.0 OthersOnLoan_IX | 0.0 OwnsProperty_IX | 0.0 InstallmentPlans_IX | 2.0 Housing_IX | 1.0 Job_IX | 1.0 Telephone_IX | 0.0 ForeignWorker_IX | 0.0 label | 0.0 features | [2.0,3.0,7.0,0.0,... rawPrediction | [17.8582417779453... probability | [0.89291208889726... prediction | 0.0 predictedLabel | No Risk -RECORD 1---------------------------------------- CheckingStatus | 0_to_200 LoanDuration | 4 CreditHistory | all_credits_paid_... LoanPurpose | furniture LoanAmount | 250 ExistingSavings | less_100 EmploymentDuration | 1_to_4 InstallmentPercent | 2 Sex | female OthersOnLoan | none CurrentResidenceDuration | 1 OwnsProperty | real_estate Age | 19 InstallmentPlans | bank Housing | rent ExistingCreditsCount | 1 Job | unskilled Dependents | 1 Telephone | none ForeignWorker | yes Risk | No Risk CheckingStatus_IX | 2.0 CreditHistory_IX | 3.0 LoanPurpose_IX | 1.0 ExistingSavings_IX | 0.0 EmploymentDuration_IX | 0.0 Sex_IX | 1.0 OthersOnLoan_IX | 0.0 OwnsProperty_IX | 2.0 InstallmentPlans_IX | 2.0 Housing_IX | 1.0 Job_IX | 1.0 Telephone_IX | 0.0 ForeignWorker_IX | 0.0 label | 0.0 features | [2.0,3.0,1.0,0.0,... rawPrediction | [18.6217190953704... probability | [0.93108595476852... prediction | 0.0 predictedLabel | No Risk -RECORD 2---------------------------------------- CheckingStatus | 0_to_200 LoanDuration | 7 CreditHistory | all_credits_paid_... LoanPurpose | car_new LoanAmount | 3091 ExistingSavings | less_100 EmploymentDuration | less_1 InstallmentPercent | 3 Sex | female OthersOnLoan | none CurrentResidenceDuration | 2 OwnsProperty | savings_insurance Age | 28 InstallmentPlans | none Housing | own ExistingCreditsCount | 2 Job | skilled Dependents | 1 Telephone | none ForeignWorker | yes Risk | No Risk CheckingStatus_IX | 2.0 CreditHistory_IX | 3.0 LoanPurpose_IX | 0.0 ExistingSavings_IX | 0.0 EmploymentDuration_IX | 3.0 Sex_IX | 1.0 OthersOnLoan_IX | 0.0 OwnsProperty_IX | 0.0 InstallmentPlans_IX | 0.0 Housing_IX | 0.0 Job_IX | 0.0 Telephone_IX | 0.0 ForeignWorker_IX | 0.0 label | 0.0 features | (20,[0,1,4,5,13,1... rawPrediction | [17.0900288758532... probability | [0.85450144379266... prediction | 0.0 predictedLabel | No Risk -RECORD 3---------------------------------------- CheckingStatus | 0_to_200 LoanDuration | 9 CreditHistory | prior_payments_de... LoanPurpose | furniture LoanAmount | 250 ExistingSavings | less_100 EmploymentDuration | less_1 InstallmentPercent | 1 Sex | male OthersOnLoan | none CurrentResidenceDuration | 3 OwnsProperty | savings_insurance Age | 31 InstallmentPlans | none Housing | own ExistingCreditsCount | 1 Job | skilled Dependents | 1 Telephone | yes ForeignWorker | yes Risk | No Risk CheckingStatus_IX | 2.0 CreditHistory_IX | 0.0 LoanPurpose_IX | 1.0 ExistingSavings_IX | 0.0 EmploymentDuration_IX | 3.0 Sex_IX | 0.0 OthersOnLoan_IX | 0.0 OwnsProperty_IX | 0.0 InstallmentPlans_IX | 0.0 Housing_IX | 0.0 Job_IX | 0.0 Telephone_IX | 1.0 ForeignWorker_IX | 0.0 label | 0.0 features | (20,[0,2,4,11,13,... rawPrediction | [17.3233263549217... probability | [0.86616631774608... prediction | 0.0 predictedLabel | No Risk -RECORD 4---------------------------------------- CheckingStatus | 0_to_200 LoanDuration | 10 CreditHistory | prior_payments_de... LoanPurpose | car_new LoanAmount | 1797 ExistingSavings | greater_1000 EmploymentDuration | 4_to_7 InstallmentPercent | 3 Sex | male OthersOnLoan | none CurrentResidenceDuration | 3 OwnsProperty | savings_insurance Age | 28 InstallmentPlans | none Housing | own ExistingCreditsCount | 1 Job | skilled Dependents | 1 Telephone | none ForeignWorker | yes Risk | No Risk CheckingStatus_IX | 2.0 CreditHistory_IX | 0.0 LoanPurpose_IX | 0.0 ExistingSavings_IX | 3.0 EmploymentDuration_IX | 1.0 Sex_IX | 0.0 OthersOnLoan_IX | 0.0 OwnsProperty_IX | 0.0 InstallmentPlans_IX | 0.0 Housing_IX | 0.0 Job_IX | 0.0 Telephone_IX | 0.0 ForeignWorker_IX | 0.0 label | 0.0 features | (20,[0,3,4,13,14,... rawPrediction | [16.7833363666722... probability | [0.83916681833361... prediction | 0.0 predictedLabel | No Risk only showing top 5 rows

By tabulating a count, you can see which product line is the most popular.

predictions.select("predictedLabel").groupBy("predictedLabel").count().show( truncate=False )
+--------------+-----+ |predictedLabel|count| +--------------+-----+ |No Risk |71 | |Risk |23 | +--------------+-----+

5. Deploy model

In this step, we will create both an online deployment and a batch deployment of Apache® Spark model using ibm-watsonx-ai. Depending on your use-case, only one deployment out of these two might be necessary. You can learn more about batch deployments here.

Note: You can also use REST API to deploy and score. For more information about REST APIs, see the Swagger Documentation.

5.1: Create online scoring endpoint

Create online deployment for published model

online_deployment = client.deployments.create( model_id, meta_props={ client.deployments.ConfigurationMetaNames.NAME: "Credit Risk model online deployment", client.deployments.ConfigurationMetaNames.ONLINE: {}, }, )
###################################################################################### Synchronous deployment creation for id: 'be89594e-92c1-4a16-b68e-28c31e7ce943' started ###################################################################################### initializing Note: online_url is deprecated and will be removed in a future release. Use serving_urls instead. . ready ----------------------------------------------------------------------------------------------- Successfully finished deployment creation, deployment_id='ebdff64f-b927-4fde-9258-51ec3f6a4200' -----------------------------------------------------------------------------------------------

You can retrieve now your online deployment ID

online_deployment_id = client.deployments.get_id(online_deployment)

You can also list all deployments in your space

client.deployments.list()

If you want to get additional information on your deployment, you can do it as below

client.deployments.get_details(online_deployment_id)

5.2 Create batch scoring endpoint

Create batch deployment for published model

batch_deployment = client.deployments.create( artifact_uid=model_id, meta_props={ client.deployments.ConfigurationMetaNames.NAME: "Credit Risk model batch deployment", client.deployments.ConfigurationMetaNames.BATCH: {}, client.deployments.ConfigurationMetaNames.HARDWARE_SPEC: { "name": "S", "num_nodes": 1, }, }, )
###################################################################################### Synchronous deployment creation for id: 'be89594e-92c1-4a16-b68e-28c31e7ce943' started ###################################################################################### ready. ----------------------------------------------------------------------------------------------- Successfully finished deployment creation, deployment_id='cf34b4ee-21a1-498e-a675-9479066bb39a' -----------------------------------------------------------------------------------------------

You can retrieve now your batch deployment ID

batch_deployment_id = client.deployments.get_id(batch_deployment)

You can also list all deployments in your space

client.deployments.list()

If you want to get additional information on your deployment, you can do it as below

client.deployments.get_details(batch_deployment_id)
{'entity': {'asset': {'id': 'be89594e-92c1-4a16-b68e-28c31e7ce943'}, 'batch': {}, 'chat_enabled': False, 'custom': {}, 'deployed_asset_type': 'model', 'hardware_spec': {'id': 'e7ed1d6c-2e89-42d7-aed5-863b972c1d2b', 'name': 'S', 'num_nodes': 1}, 'name': 'Credit Risk model batch deployment', 'space_id': '8a13841b-df99-4b4d-bf2a-161ad2e33980', 'status': {'state': 'ready'}}, 'metadata': {'created_at': '2025-05-16T07:58:53.582Z', 'id': 'cf34b4ee-21a1-498e-a675-9479066bb39a', 'modified_at': '2025-05-16T07:58:53.582Z', 'name': 'Credit Risk model batch deployment', 'owner': '1000331001', 'space_id': '8a13841b-df99-4b4d-bf2a-161ad2e33980'}}

6. Score model

Prepare scoring payload

import json fields = [ "CheckingStatus", "LoanDuration", "CreditHistory", "LoanPurpose", "LoanAmount", "ExistingSavings", "EmploymentDuration", "InstallmentPercent", "Sex", "OthersOnLoan", "CurrentResidenceDuration", "OwnsProperty", "Age", "InstallmentPlans", "Housing", "ExistingCreditsCount", "Job", "Dependents", "Telephone", "ForeignWorker", ] values = [ [ "no_checking", 13, "credits_paid_to_date", "car_new", 1343, "100_to_500", "1_to_4", 2, "female", "none", 3, "savings_insurance", 46, "none", "own", 2, "skilled", 1, "none", "yes", ], [ "no_checking", 24, "prior_payments_delayed", "furniture", 4567, "500_to_1000", "1_to_4", 4, "male", "none", 4, "savings_insurance", 36, "none", "free", 2, "management_self-employed", 1, "none", "yes", ], [ "0_to_200", 26, "all_credits_paid_back", "car_new", 863, "less_100", "less_1", 2, "female", "co-applicant", 2, "real_estate", 38, "none", "own", 1, "skilled", 1, "none", "yes", ], [ "0_to_200", 14, "no_credits", "car_new", 2368, "less_100", "1_to_4", 3, "female", "none", 3, "real_estate", 29, "none", "own", 1, "skilled", 1, "none", "yes", ], [ "0_to_200", 4, "no_credits", "car_new", 250, "less_100", "unemployed", 2, "female", "none", 3, "real_estate", 23, "none", "rent", 1, "management_self-employed", 1, "none", "yes", ], [ "no_checking", 17, "credits_paid_to_date", "car_new", 832, "100_to_500", "1_to_4", 2, "male", "none", 2, "real_estate", 42, "none", "own", 1, "skilled", 1, "none", "yes", ], [ "no_checking", 33, "outstanding_credit", "appliances", 5696, "unknown", "greater_7", 4, "male", "co-applicant", 4, "unknown", 54, "none", "free", 2, "skilled", 1, "yes", "yes", ], [ "0_to_200", 13, "prior_payments_delayed", "retraining", 1375, "100_to_500", "4_to_7", 3, "male", "none", 3, "real_estate", 37, "none", "own", 2, "management_self-employed", 1, "none", "yes", ], ] meta_props = { client.deployments.ScoringMetaNames.INPUT_DATA: [ { "fields": fields, "values": values, } ] }

5.1 Online deployment scoring

Scoring of online deployments can be performed using the score method.

predictions = client.deployments.score(online_deployment_id, meta_props) print(json.dumps(predictions, indent=2))
{ "predictions": [ { "fields": [ "CheckingStatus", "LoanDuration", "CreditHistory", "LoanPurpose", "LoanAmount", "ExistingSavings", "EmploymentDuration", "InstallmentPercent", "Sex", "OthersOnLoan", "CurrentResidenceDuration", "OwnsProperty", "Age", "InstallmentPlans", "Housing", "ExistingCreditsCount", "Job", "Dependents", "Telephone", "ForeignWorker", "CheckingStatus_IX", "CreditHistory_IX", "LoanPurpose_IX", "ExistingSavings_IX", "EmploymentDuration_IX", "Sex_IX", "OthersOnLoan_IX", "OwnsProperty_IX", "InstallmentPlans_IX", "Housing_IX", "Job_IX", "Telephone_IX", "ForeignWorker_IX", "features", "rawPrediction", "probability", "prediction", "predictedLabel" ], "values": [ [ "no_checking", 13, "credits_paid_to_date", "car_new", 1343, "100_to_500", "1_to_4", 2, "female", "none", 3, "savings_insurance", 46, "none", "own", 2, "skilled", 1, "none", "yes", 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, [ 20, [ 1, 3, 5, 13, 14, 15, 16, 17, 18, 19 ], [ 1.0, 1.0, 1.0, 13.0, 1343.0, 2.0, 3.0, 46.0, 2.0, 1.0 ] ], [ 14.298844113312466, 5.701155886687534 ], [ 0.7149422056656233, 0.2850577943343767 ], 0.0, "No Risk" ], [ "no_checking", 24, "prior_payments_delayed", "furniture", 4567, "500_to_1000", "1_to_4", 4, "male", "none", 4, "savings_insurance", 36, "none", "free", 2, "management_self-employed", 1, "none", "yes", 0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 2.0, 0.0, 0.0, [ 20, [ 2, 3, 9, 10, 13, 14, 15, 16, 17, 18, 19 ], [ 1.0, 2.0, 2.0, 2.0, 24.0, 4567.0, 4.0, 4.0, 36.0, 2.0, 1.0 ] ], [ 13.06676919836477, 6.93323080163523 ], [ 0.6533384599182386, 0.34666154008176153 ], 0.0, "No Risk" ], [ "0_to_200", 26, "all_credits_paid_back", "car_new", 863, "less_100", "less_1", 2, "female", "co-applicant", 2, "real_estate", 38, "none", "own", 1, "skilled", 1, "none", "yes", 2.0, 3.0, 0.0, 0.0, 3.0, 1.0, 1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, [ 2.0, 3.0, 0.0, 0.0, 3.0, 1.0, 1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26.0, 863.0, 2.0, 2.0, 38.0, 1.0, 1.0 ], [ 17.26065854270966, 2.739341457290342 ], [ 0.863032927135483, 0.1369670728645171 ], 0.0, "No Risk" ], [ "0_to_200", 14, "no_credits", "car_new", 2368, "less_100", "1_to_4", 3, "female", "none", 3, "real_estate", 29, "none", "own", 1, "skilled", 1, "none", "yes", 2.0, 4.0, 0.0, 0.0, 0.0, 1.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, [ 20, [ 0, 1, 5, 7, 13, 14, 15, 16, 17, 18, 19 ], [ 2.0, 4.0, 1.0, 2.0, 14.0, 2368.0, 3.0, 3.0, 29.0, 1.0, 1.0 ] ], [ 17.80975737817367, 2.190242621826325 ], [ 0.8904878689086837, 0.10951213109131627 ], 0.0, "No Risk" ], [ "0_to_200", 4, "no_credits", "car_new", 250, "less_100", "unemployed", 2, "female", "none", 3, "real_estate", 23, "none", "rent", 1, "management_self-employed", 1, "none", "yes", 2.0, 4.0, 0.0, 0.0, 4.0, 1.0, 0.0, 2.0, 0.0, 1.0, 2.0, 0.0, 0.0, [ 2.0, 4.0, 0.0, 0.0, 4.0, 1.0, 0.0, 2.0, 0.0, 1.0, 2.0, 0.0, 0.0, 4.0, 250.0, 2.0, 3.0, 23.0, 1.0, 1.0 ], [ 18.38016125542918, 1.6198387445708178 ], [ 0.9190080627714591, 0.08099193722854091 ], 0.0, "No Risk" ], [ "no_checking", 17, "credits_paid_to_date", "car_new", 832, "100_to_500", "1_to_4", 2, "male", "none", 2, "real_estate", 42, "none", "own", 1, "skilled", 1, "none", "yes", 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, [ 20, [ 1, 3, 7, 13, 14, 15, 16, 17, 18, 19 ], [ 1.0, 1.0, 2.0, 17.0, 832.0, 2.0, 2.0, 42.0, 1.0, 1.0 ] ], [ 16.887908351466088, 3.112091648533915 ], [ 0.8443954175733043, 0.15560458242669573 ], 0.0, "No Risk" ], [ "no_checking", 33, "outstanding_credit", "appliances", 5696, "unknown", "greater_7", 4, "male", "co-applicant", 4, "unknown", 54, "none", "free", 2, "skilled", 1, "yes", "yes", 0.0, 2.0, 4.0, 4.0, 2.0, 0.0, 1.0, 3.0, 0.0, 2.0, 0.0, 1.0, 0.0, [ 0.0, 2.0, 4.0, 4.0, 2.0, 0.0, 1.0, 3.0, 0.0, 2.0, 0.0, 1.0, 0.0, 33.0, 5696.0, 4.0, 4.0, 54.0, 2.0, 1.0 ], [ 2.252422102778986, 17.747577897221014 ], [ 0.1126211051389493, 0.8873788948610507 ], 1.0, "Risk" ], [ "0_to_200", 13, "prior_payments_delayed", "retraining", 1375, "100_to_500", "4_to_7", 3, "male", "none", 3, "real_estate", 37, "none", "own", 2, "management_self-employed", 1, "none", "yes", 2.0, 0.0, 8.0, 1.0, 1.0, 0.0, 0.0, 2.0, 0.0, 0.0, 2.0, 0.0, 0.0, [ 2.0, 0.0, 8.0, 1.0, 1.0, 0.0, 0.0, 2.0, 0.0, 0.0, 2.0, 0.0, 0.0, 13.0, 1375.0, 3.0, 3.0, 37.0, 2.0, 1.0 ], [ 15.860708756328307, 4.1392912436716935 ], [ 0.7930354378164154, 0.20696456218358467 ], 0.0, "No Risk" ] ] } ] }

Batch deployment scoring

In order to score a model in batch deployment, a job needs to be created.

job = client.deployments.create_job(batch_deployment_id, meta_props=meta_props)

After submitting your job, you can retrieve its ID

job_id = client.deployments.get_job_id(job)

You can also list all jobs in your space.

client.deployments.list_jobs()

If you want to get additional information on your job, you can do it as below.

client.deployments.get_job_details(job_id)
{'entity': {'deployment': {'id': 'cf34b4ee-21a1-498e-a675-9479066bb39a'}, 'platform_job': {'job_id': '23311794-512a-430e-a738-510be83946a0', 'run_id': '7576c94b-2bd3-4fff-8039-4938f9ecd0d9'}, 'scoring': {'input_data': [{'fields': ['CheckingStatus', 'LoanDuration', 'CreditHistory', 'LoanPurpose', 'LoanAmount', 'ExistingSavings', 'EmploymentDuration', 'InstallmentPercent', 'Sex', 'OthersOnLoan', 'CurrentResidenceDuration', 'OwnsProperty', 'Age', 'InstallmentPlans', 'Housing', 'ExistingCreditsCount', 'Job', 'Dependents', 'Telephone', 'ForeignWorker'], 'values': [['no_checking', 13, 'credits_paid_to_date', 'car_new', 1343, '100_to_500', '1_to_4', 2, 'female', 'none', 3, 'savings_insurance', 46, 'none', 'own', 2, 'skilled', 1, 'none', 'yes'], ['no_checking', 24, 'prior_payments_delayed', 'furniture', 4567, '500_to_1000', '1_to_4', 4, 'male', 'none', 4, 'savings_insurance', 36, 'none', 'free', 2, 'management_self-employed', 1, 'none', 'yes'], ['0_to_200', 26, 'all_credits_paid_back', 'car_new', 863, 'less_100', 'less_1', 2, 'female', 'co-applicant', 2, 'real_estate', 38, 'none', 'own', 1, 'skilled', 1, 'none', 'yes'], ['0_to_200', 14, 'no_credits', 'car_new', 2368, 'less_100', '1_to_4', 3, 'female', 'none', 3, 'real_estate', 29, 'none', 'own', 1, 'skilled', 1, 'none', 'yes'], ['0_to_200', 4, 'no_credits', 'car_new', 250, 'less_100', 'unemployed', 2, 'female', 'none', 3, 'real_estate', 23, 'none', 'rent', 1, 'management_self-employed', 1, 'none', 'yes'], ['no_checking', 17, 'credits_paid_to_date', 'car_new', 832, '100_to_500', '1_to_4', 2, 'male', 'none', 2, 'real_estate', 42, 'none', 'own', 1, 'skilled', 1, 'none', 'yes'], ['no_checking', 33, 'outstanding_credit', 'appliances', 5696, 'unknown', 'greater_7', 4, 'male', 'co-applicant', 4, 'unknown', 54, 'none', 'free', 2, 'skilled', 1, 'yes', 'yes'], ['0_to_200', 13, 'prior_payments_delayed', 'retraining', 1375, '100_to_500', '4_to_7', 3, 'male', 'none', 3, 'real_estate', 37, 'none', 'own', 2, 'management_self-employed', 1, 'none', 'yes']]}], 'status': {'completed_at': '', 'running_at': '', 'state': 'queued'}}}, 'metadata': {'created_at': '2025-05-16T07:59:35.555Z', 'id': '61aa2dd0-2afd-4183-9186-e7269d1e3a68', 'name': 'name_1cbbd867-93c4-4227-b2d5-659e3ff8aaf6', 'space_id': '8a13841b-df99-4b4d-bf2a-161ad2e33980'}}

Here you can check status of your batch scoring.

import time elapsed_time = 0 while ( client.deployments.get_job_status(job_id).get("state") != "completed" and elapsed_time < 300 ): print(f" Current state: {client.deployments.get_job_status(job_id).get('state')}") elapsed_time += 10 time.sleep(10) if client.deployments.get_job_status(job_id).get("state") == "completed": print(f" Current state: {client.deployments.get_job_status(job_id).get('state')}") job_details_do = client.deployments.get_job_details(job_id) print(job_details_do) else: print("Job hasn't completed successfully in 5 minutes.")
Current state: queued Current state: queued Current state: queued Current state: queued Current state: running Current state: completed {'entity': {'deployment': {'id': 'cf34b4ee-21a1-498e-a675-9479066bb39a'}, 'platform_job': {'job_id': '23311794-512a-430e-a738-510be83946a0', 'run_id': '7576c94b-2bd3-4fff-8039-4938f9ecd0d9'}, 'scoring': {'input_data': [{'fields': ['CheckingStatus', 'LoanDuration', 'CreditHistory', 'LoanPurpose', 'LoanAmount', 'ExistingSavings', 'EmploymentDuration', 'InstallmentPercent', 'Sex', 'OthersOnLoan', 'CurrentResidenceDuration', 'OwnsProperty', 'Age', 'InstallmentPlans', 'Housing', 'ExistingCreditsCount', 'Job', 'Dependents', 'Telephone', 'ForeignWorker'], 'values': [['no_checking', 13, 'credits_paid_to_date', 'car_new', 1343, '100_to_500', '1_to_4', 2, 'female', 'none', 3, 'savings_insurance', 46, 'none', 'own', 2, 'skilled', 1, 'none', 'yes'], ['no_checking', 24, 'prior_payments_delayed', 'furniture', 4567, '500_to_1000', '1_to_4', 4, 'male', 'none', 4, 'savings_insurance', 36, 'none', 'free', 2, 'management_self-employed', 1, 'none', 'yes'], ['0_to_200', 26, 'all_credits_paid_back', 'car_new', 863, 'less_100', 'less_1', 2, 'female', 'co-applicant', 2, 'real_estate', 38, 'none', 'own', 1, 'skilled', 1, 'none', 'yes'], ['0_to_200', 14, 'no_credits', 'car_new', 2368, 'less_100', '1_to_4', 3, 'female', 'none', 3, 'real_estate', 29, 'none', 'own', 1, 'skilled', 1, 'none', 'yes'], ['0_to_200', 4, 'no_credits', 'car_new', 250, 'less_100', 'unemployed', 2, 'female', 'none', 3, 'real_estate', 23, 'none', 'rent', 1, 'management_self-employed', 1, 'none', 'yes'], ['no_checking', 17, 'credits_paid_to_date', 'car_new', 832, '100_to_500', '1_to_4', 2, 'male', 'none', 2, 'real_estate', 42, 'none', 'own', 1, 'skilled', 1, 'none', 'yes'], ['no_checking', 33, 'outstanding_credit', 'appliances', 5696, 'unknown', 'greater_7', 4, 'male', 'co-applicant', 4, 'unknown', 54, 'none', 'free', 2, 'skilled', 1, 'yes', 'yes'], ['0_to_200', 13, 'prior_payments_delayed', 'retraining', 1375, '100_to_500', '4_to_7', 3, 'male', 'none', 3, 'real_estate', 37, 'none', 'own', 2, 'management_self-employed', 1, 'none', 'yes']]}], 'predictions': [{'fields': ['CheckingStatus', 'LoanDuration', 'CreditHistory', 'LoanPurpose', 'LoanAmount', 'ExistingSavings', 'EmploymentDuration', 'InstallmentPercent', 'Sex', 'OthersOnLoan', 'CurrentResidenceDuration', 'OwnsProperty', 'Age', 'InstallmentPlans', 'Housing', 'ExistingCreditsCount', 'Job', 'Dependents', 'Telephone', 'ForeignWorker', 'CheckingStatus_IX', 'CreditHistory_IX', 'LoanPurpose_IX', 'ExistingSavings_IX', 'EmploymentDuration_IX', 'Sex_IX', 'OthersOnLoan_IX', 'OwnsProperty_IX', 'InstallmentPlans_IX', 'Housing_IX', 'Job_IX', 'Telephone_IX', 'ForeignWorker_IX', 'features', 'rawPrediction', 'probability', 'prediction', 'predictedLabel'], 'values': [['no_checking', 13, 'credits_paid_to_date', 'car_new', 1343, '100_to_500', '1_to_4', 2, 'female', 'none', 3, 'savings_insurance', 46, 'none', 'own', 2, 'skilled', 1, 'none', 'yes', 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, [20, [1, 3, 5, 13, 14, 15, 16, 17, 18, 19], [1.0, 1.0, 1.0, 13.0, 1343.0, 2.0, 3.0, 46.0, 2.0, 1.0]], [14.298844113312466, 5.701155886687534], [0.7149422056656233, 0.2850577943343767], 0.0, 'No Risk'], ['no_checking', 24, 'prior_payments_delayed', 'furniture', 4567, '500_to_1000', '1_to_4', 4, 'male', 'none', 4, 'savings_insurance', 36, 'none', 'free', 2, 'management_self-employed', 1, 'none', 'yes', 0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 2.0, 0.0, 0.0, [20, [2, 3, 9, 10, 13, 14, 15, 16, 17, 18, 19], [1.0, 2.0, 2.0, 2.0, 24.0, 4567.0, 4.0, 4.0, 36.0, 2.0, 1.0]], [13.06676919836477, 6.93323080163523], [0.6533384599182386, 0.34666154008176153], 0.0, 'No Risk'], ['0_to_200', 26, 'all_credits_paid_back', 'car_new', 863, 'less_100', 'less_1', 2, 'female', 'co-applicant', 2, 'real_estate', 38, 'none', 'own', 1, 'skilled', 1, 'none', 'yes', 2.0, 3.0, 0.0, 0.0, 3.0, 1.0, 1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, [2.0, 3.0, 0.0, 0.0, 3.0, 1.0, 1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26.0, 863.0, 2.0, 2.0, 38.0, 1.0, 1.0], [17.26065854270966, 2.739341457290342], [0.863032927135483, 0.1369670728645171], 0.0, 'No Risk'], ['0_to_200', 14, 'no_credits', 'car_new', 2368, 'less_100', '1_to_4', 3, 'female', 'none', 3, 'real_estate', 29, 'none', 'own', 1, 'skilled', 1, 'none', 'yes', 2.0, 4.0, 0.0, 0.0, 0.0, 1.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, [20, [0, 1, 5, 7, 13, 14, 15, 16, 17, 18, 19], [2.0, 4.0, 1.0, 2.0, 14.0, 2368.0, 3.0, 3.0, 29.0, 1.0, 1.0]], [17.80975737817367, 2.190242621826325], [0.8904878689086837, 0.10951213109131627], 0.0, 'No Risk'], ['0_to_200', 4, 'no_credits', 'car_new', 250, 'less_100', 'unemployed', 2, 'female', 'none', 3, 'real_estate', 23, 'none', 'rent', 1, 'management_self-employed', 1, 'none', 'yes', 2.0, 4.0, 0.0, 0.0, 4.0, 1.0, 0.0, 2.0, 0.0, 1.0, 2.0, 0.0, 0.0, [2.0, 4.0, 0.0, 0.0, 4.0, 1.0, 0.0, 2.0, 0.0, 1.0, 2.0, 0.0, 0.0, 4.0, 250.0, 2.0, 3.0, 23.0, 1.0, 1.0], [18.38016125542918, 1.6198387445708178], [0.9190080627714591, 0.08099193722854091], 0.0, 'No Risk'], ['no_checking', 17, 'credits_paid_to_date', 'car_new', 832, '100_to_500', '1_to_4', 2, 'male', 'none', 2, 'real_estate', 42, 'none', 'own', 1, 'skilled', 1, 'none', 'yes', 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, [20, [1, 3, 7, 13, 14, 15, 16, 17, 18, 19], [1.0, 1.0, 2.0, 17.0, 832.0, 2.0, 2.0, 42.0, 1.0, 1.0]], [16.887908351466088, 3.112091648533915], [0.8443954175733043, 0.15560458242669573], 0.0, 'No Risk'], ['no_checking', 33, 'outstanding_credit', 'appliances', 5696, 'unknown', 'greater_7', 4, 'male', 'co-applicant', 4, 'unknown', 54, 'none', 'free', 2, 'skilled', 1, 'yes', 'yes', 0.0, 2.0, 4.0, 4.0, 2.0, 0.0, 1.0, 3.0, 0.0, 2.0, 0.0, 1.0, 0.0, [0.0, 2.0, 4.0, 4.0, 2.0, 0.0, 1.0, 3.0, 0.0, 2.0, 0.0, 1.0, 0.0, 33.0, 5696.0, 4.0, 4.0, 54.0, 2.0, 1.0], [2.252422102778986, 17.747577897221014], [0.1126211051389493, 0.8873788948610507], 1.0, 'Risk'], ['0_to_200', 13, 'prior_payments_delayed', 'retraining', 1375, '100_to_500', '4_to_7', 3, 'male', 'none', 3, 'real_estate', 37, 'none', 'own', 2, 'management_self-employed', 1, 'none', 'yes', 2.0, 0.0, 8.0, 1.0, 1.0, 0.0, 0.0, 2.0, 0.0, 0.0, 2.0, 0.0, 0.0, [2.0, 0.0, 8.0, 1.0, 1.0, 0.0, 0.0, 2.0, 0.0, 0.0, 2.0, 0.0, 0.0, 13.0, 1375.0, 3.0, 3.0, 37.0, 2.0, 1.0], [15.860708756328307, 4.1392912436716935], [0.7930354378164154, 0.20696456218358467], 0.0, 'No Risk']]}], 'status': {'completed_at': '2025-05-16T08:00:44.000Z', 'running_at': '2025-05-16T08:00:42.000Z', 'state': 'completed'}}}, 'metadata': {'created_at': '2025-05-16T07:59:35.555Z', 'id': '61aa2dd0-2afd-4183-9186-e7269d1e3a68', 'modified_at': '2025-05-16T08:00:44.552Z', 'name': 'name_1cbbd867-93c4-4227-b2d5-659e3ff8aaf6', 'space_id': '8a13841b-df99-4b4d-bf2a-161ad2e33980'}}

After the job completes, you can retrieve its scoring data

job_details = client.deployments.get_job_details(job_id) print(json.dumps(job_details, indent=2))
{ "entity": { "deployment": { "id": "cf34b4ee-21a1-498e-a675-9479066bb39a" }, "platform_job": { "job_id": "23311794-512a-430e-a738-510be83946a0", "run_id": "7576c94b-2bd3-4fff-8039-4938f9ecd0d9" }, "scoring": { "input_data": [ { "fields": [ "CheckingStatus", "LoanDuration", "CreditHistory", "LoanPurpose", "LoanAmount", "ExistingSavings", "EmploymentDuration", "InstallmentPercent", "Sex", "OthersOnLoan", "CurrentResidenceDuration", "OwnsProperty", "Age", "InstallmentPlans", "Housing", "ExistingCreditsCount", "Job", "Dependents", "Telephone", "ForeignWorker" ], "values": [ [ "no_checking", 13, "credits_paid_to_date", "car_new", 1343, "100_to_500", "1_to_4", 2, "female", "none", 3, "savings_insurance", 46, "none", "own", 2, "skilled", 1, "none", "yes" ], [ "no_checking", 24, "prior_payments_delayed", "furniture", 4567, "500_to_1000", "1_to_4", 4, "male", "none", 4, "savings_insurance", 36, "none", "free", 2, "management_self-employed", 1, "none", "yes" ], [ "0_to_200", 26, "all_credits_paid_back", "car_new", 863, "less_100", "less_1", 2, "female", "co-applicant", 2, "real_estate", 38, "none", "own", 1, "skilled", 1, "none", "yes" ], [ "0_to_200", 14, "no_credits", "car_new", 2368, "less_100", "1_to_4", 3, "female", "none", 3, "real_estate", 29, "none", "own", 1, "skilled", 1, "none", "yes" ], [ "0_to_200", 4, "no_credits", "car_new", 250, "less_100", "unemployed", 2, "female", "none", 3, "real_estate", 23, "none", "rent", 1, "management_self-employed", 1, "none", "yes" ], [ "no_checking", 17, "credits_paid_to_date", "car_new", 832, "100_to_500", "1_to_4", 2, "male", "none", 2, "real_estate", 42, "none", "own", 1, "skilled", 1, "none", "yes" ], [ "no_checking", 33, "outstanding_credit", "appliances", 5696, "unknown", "greater_7", 4, "male", "co-applicant", 4, "unknown", 54, "none", "free", 2, "skilled", 1, "yes", "yes" ], [ "0_to_200", 13, "prior_payments_delayed", "retraining", 1375, "100_to_500", "4_to_7", 3, "male", "none", 3, "real_estate", 37, "none", "own", 2, "management_self-employed", 1, "none", "yes" ] ] } ], "predictions": [ { "fields": [ "CheckingStatus", "LoanDuration", "CreditHistory", "LoanPurpose", "LoanAmount", "ExistingSavings", "EmploymentDuration", "InstallmentPercent", "Sex", "OthersOnLoan", "CurrentResidenceDuration", "OwnsProperty", "Age", "InstallmentPlans", "Housing", "ExistingCreditsCount", "Job", "Dependents", "Telephone", "ForeignWorker", "CheckingStatus_IX", "CreditHistory_IX", "LoanPurpose_IX", "ExistingSavings_IX", "EmploymentDuration_IX", "Sex_IX", "OthersOnLoan_IX", "OwnsProperty_IX", "InstallmentPlans_IX", "Housing_IX", "Job_IX", "Telephone_IX", "ForeignWorker_IX", "features", "rawPrediction", "probability", "prediction", "predictedLabel" ], "values": [ [ "no_checking", 13, "credits_paid_to_date", "car_new", 1343, "100_to_500", "1_to_4", 2, "female", "none", 3, "savings_insurance", 46, "none", "own", 2, "skilled", 1, "none", "yes", 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, [ 20, [ 1, 3, 5, 13, 14, 15, 16, 17, 18, 19 ], [ 1.0, 1.0, 1.0, 13.0, 1343.0, 2.0, 3.0, 46.0, 2.0, 1.0 ] ], [ 14.298844113312466, 5.701155886687534 ], [ 0.7149422056656233, 0.2850577943343767 ], 0.0, "No Risk" ], [ "no_checking", 24, "prior_payments_delayed", "furniture", 4567, "500_to_1000", "1_to_4", 4, "male", "none", 4, "savings_insurance", 36, "none", "free", 2, "management_self-employed", 1, "none", "yes", 0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 2.0, 0.0, 0.0, [ 20, [ 2, 3, 9, 10, 13, 14, 15, 16, 17, 18, 19 ], [ 1.0, 2.0, 2.0, 2.0, 24.0, 4567.0, 4.0, 4.0, 36.0, 2.0, 1.0 ] ], [ 13.06676919836477, 6.93323080163523 ], [ 0.6533384599182386, 0.34666154008176153 ], 0.0, "No Risk" ], [ "0_to_200", 26, "all_credits_paid_back", "car_new", 863, "less_100", "less_1", 2, "female", "co-applicant", 2, "real_estate", 38, "none", "own", 1, "skilled", 1, "none", "yes", 2.0, 3.0, 0.0, 0.0, 3.0, 1.0, 1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, [ 2.0, 3.0, 0.0, 0.0, 3.0, 1.0, 1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26.0, 863.0, 2.0, 2.0, 38.0, 1.0, 1.0 ], [ 17.26065854270966, 2.739341457290342 ], [ 0.863032927135483, 0.1369670728645171 ], 0.0, "No Risk" ], [ "0_to_200", 14, "no_credits", "car_new", 2368, "less_100", "1_to_4", 3, "female", "none", 3, "real_estate", 29, "none", "own", 1, "skilled", 1, "none", "yes", 2.0, 4.0, 0.0, 0.0, 0.0, 1.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, [ 20, [ 0, 1, 5, 7, 13, 14, 15, 16, 17, 18, 19 ], [ 2.0, 4.0, 1.0, 2.0, 14.0, 2368.0, 3.0, 3.0, 29.0, 1.0, 1.0 ] ], [ 17.80975737817367, 2.190242621826325 ], [ 0.8904878689086837, 0.10951213109131627 ], 0.0, "No Risk" ], [ "0_to_200", 4, "no_credits", "car_new", 250, "less_100", "unemployed", 2, "female", "none", 3, "real_estate", 23, "none", "rent", 1, "management_self-employed", 1, "none", "yes", 2.0, 4.0, 0.0, 0.0, 4.0, 1.0, 0.0, 2.0, 0.0, 1.0, 2.0, 0.0, 0.0, [ 2.0, 4.0, 0.0, 0.0, 4.0, 1.0, 0.0, 2.0, 0.0, 1.0, 2.0, 0.0, 0.0, 4.0, 250.0, 2.0, 3.0, 23.0, 1.0, 1.0 ], [ 18.38016125542918, 1.6198387445708178 ], [ 0.9190080627714591, 0.08099193722854091 ], 0.0, "No Risk" ], [ "no_checking", 17, "credits_paid_to_date", "car_new", 832, "100_to_500", "1_to_4", 2, "male", "none", 2, "real_estate", 42, "none", "own", 1, "skilled", 1, "none", "yes", 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, [ 20, [ 1, 3, 7, 13, 14, 15, 16, 17, 18, 19 ], [ 1.0, 1.0, 2.0, 17.0, 832.0, 2.0, 2.0, 42.0, 1.0, 1.0 ] ], [ 16.887908351466088, 3.112091648533915 ], [ 0.8443954175733043, 0.15560458242669573 ], 0.0, "No Risk" ], [ "no_checking", 33, "outstanding_credit", "appliances", 5696, "unknown", "greater_7", 4, "male", "co-applicant", 4, "unknown", 54, "none", "free", 2, "skilled", 1, "yes", "yes", 0.0, 2.0, 4.0, 4.0, 2.0, 0.0, 1.0, 3.0, 0.0, 2.0, 0.0, 1.0, 0.0, [ 0.0, 2.0, 4.0, 4.0, 2.0, 0.0, 1.0, 3.0, 0.0, 2.0, 0.0, 1.0, 0.0, 33.0, 5696.0, 4.0, 4.0, 54.0, 2.0, 1.0 ], [ 2.252422102778986, 17.747577897221014 ], [ 0.1126211051389493, 0.8873788948610507 ], 1.0, "Risk" ], [ "0_to_200", 13, "prior_payments_delayed", "retraining", 1375, "100_to_500", "4_to_7", 3, "male", "none", 3, "real_estate", 37, "none", "own", 2, "management_self-employed", 1, "none", "yes", 2.0, 0.0, 8.0, 1.0, 1.0, 0.0, 0.0, 2.0, 0.0, 0.0, 2.0, 0.0, 0.0, [ 2.0, 0.0, 8.0, 1.0, 1.0, 0.0, 0.0, 2.0, 0.0, 0.0, 2.0, 0.0, 0.0, 13.0, 1375.0, 3.0, 3.0, 37.0, 2.0, 1.0 ], [ 15.860708756328307, 4.1392912436716935 ], [ 0.7930354378164154, 0.20696456218358467 ], 0.0, "No Risk" ] ] } ], "status": { "completed_at": "2025-05-16T08:00:44.000Z", "running_at": "2025-05-16T08:00:42.000Z", "state": "completed" } } }, "metadata": { "created_at": "2025-05-16T07:59:35.555Z", "id": "61aa2dd0-2afd-4183-9186-e7269d1e3a68", "modified_at": "2025-05-16T08:00:44.552Z", "name": "name_1cbbd867-93c4-4227-b2d5-659e3ff8aaf6", "space_id": "8a13841b-df99-4b4d-bf2a-161ad2e33980" } }

6. Clean up

If you want to clean up all created assets:

  • experiments

  • trainings

  • pipelines

  • model definitions

  • models

  • functions

  • deployments

please follow up this sample notebook.

7. Summary and next steps

You successfully completed this notebook! You learned how to use Apache Spark machine learning as well as Watson Machine Learning for model creation and deployment.

Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.

Authors

Amadeusz Masny, Python Software Developer at watsonx.ai

Rafał Chrzanowski, Software Engineer Intern at watsonx.ai.

Copyright © 2020-2025 IBM. This notebook and its source code are released under the terms of the MIT License.