<jupyter_start><jupyter_text>Batch Scoring on IBM Cloud Pak for Data as a ServiceWe are going to use this notebook to create and/or run a batch scoring job against a model that has previously been created and deployed to the Watson Machine Learning (WML) instance on Cloud Pak for Data (CPD).  1.0 Install required packagesThere are a couple of Python packages we will use in this notebook.WML Client: http://ibm-wml-api-pyclient.mybluemix.net/<jupyter_code>!pip uninstall watson-machine-learning-client -y | tail -n 1
!pip uninstall watson-machine-learning-client-V4 -y | tail -n 1

!pip install --upgrade ibm-watson-machine-learning==1.0.22 --user --no-cache | tail -n 1
import json
from ibm_watson_machine_learning import APIClient<jupyter_output><empty_output><jupyter_text>2.0 Create Batch Deployment Job  2.1 Instantiate Watson Machine Learning ClientTo authenticate the Watson Machine Learning service on IBM Cloud, you need to provide a platform `api_key` and an endpoint URL. Where the endpoint URL is based on the `location` of the WML instance. To get these values you can use either the IBM Cloud CLI or the IBM Cloud UI.  IBM Cloud CLIYou can use the [IBM Cloud CLI](https://cloud.ibm.com/docs/cli/index.html) to create a platform API Key and retrieve your instance location.- To generate the Cloud API Key, run the following commands:```ibmcloud loginibmcloud iam api-key-create API_KEY_NAME```  - Copy the value of `api_key` from the output.- To retrieve the location of your WML instance, run the following commands:```ibmcloud login --apikey API_KEY -a https://cloud.ibm.comibmcloud resource service-instance "WML_INSTANCE_NAME"```> Note: WML_INSTANCE_NAME is the name of your Watson Machine Learning instance and should be quoted in the command.  - Copy the value of `Location` from the output.  IBM Cloud UITo generate Cloud API key:- Go to the [**Users** section of the Cloud console](https://cloud.ibm.com/iam/users). - From that page, click your name in the top right corner, scroll down to the **API Keys** section, and click **Create an IBM Cloud API key**. - Give your key a name and click **Create**, then copy the created key and to use it below.To retrieve the location of your WML instance:- Go to the [**Resources List** section of the Cloud console](https://cloud.ibm.com/resources).- From that page, expand the **Services** section and find your Watson Machine Learning Instance.- Based on the Location displayed in that page, select one of the following values for location variable:|Displayed Location|Location||-|-||Dallas|us-south||London|eu-gb||Frankfurt|eu-de||Tokyo|jp-tok| **>**<jupyter_code># Be sure to update these credentials before running the cell.
api_key = '<api-key>'
location = '<location>'
wml_credentials = {
    "apikey": api_key,
    "url": 'https://' + location + '.ml.cloud.ibm.com'
}

wml_client = APIClient(wml_credentials)
wml_client.spaces.list()<jupyter_output><empty_output><jupyter_text>2.2 Find Deployment SpaceWe will try to find the `GUID` for the deployment space you want to use and set it as the default space for the client.- **>**> Note: You should copy the name of your deployment space from the output of the previous cell to the variable in the next cell. The deployment space ID will be looked up based on the name specified below. If you do not receive a space GUID as an output to the next cell, do not proceed until you have created a deployment space.<jupyter_code># Be sure to update the name of the space with the one you want to use.
DEPLOYMENT_SPACE_NAME = "<deployment-space-name>"
wml_client.spaces.list()
all_spaces = wml_client.spaces.get_details()['resources']
space_id = None
for space in all_spaces:
    if space['entity']['name'] == DEPLOYMENT_SPACE_NAME:
        space_id = space["metadata"]["id"]
        print("\nDeployment Space ID: ", space_id)

if space_id is None:
    print("WARNING: Your space does not exist. Create a deployment space before proceeding to the next cell.")
# Now set the default space to the GUID for your deployment space. If this is successful, you will see a 'SUCCESS' message.
wml_client.set.default_space(space_id)
# These are the models and deployments we currently have in our deployment space.
wml_client.repository.list_models()
wml_client.deployments.list()<jupyter_output><empty_output><jupyter_text>2.3 Find Batch DeploymentWe will try to find the batch deployment which was created.- **>**>Note: You should copy the deployment name from the output of the previous cell to the variable in this next cell.<jupyter_code>DEPLOYMENT_NAME = "<batch-deployment-name>"
wml_deployments = wml_client.deployments.get_details()
deployment_uid = None
deployment_details = None
for deployment in wml_deployments['resources']:
    if DEPLOYMENT_NAME == deployment['entity']['name']:
        deployment_uid = deployment['metadata']['id']
        deployment_details = deployment
        #print(json.dumps(deployment_details, indent=3))
        break

print("Deployment id: {}".format(deployment_uid))
wml_client.deployments.get_details(deployment_uid)<jupyter_output><empty_output><jupyter_text>2.4 Get Batch Test DataWe will load some data to run the batch predictions**>*** Highlight the cell below by clicking in it, under the first commented line.* Click the `01/00` "Find data" icon in the upper right of the notebook.* Find the project file `German-Credit-Risk-SmallBatchSet.csv` in the `Files` tab. Then click `Insert to code` and choose `pandas DataFrame`.* The code to bring the data into the notebook environment and create a Pandas DataFrame will be added to the cell below.* Run the cell<jupyter_code># Place cursor below and insert the Pandas DataFrame for the German-Credit-Risk-SmallBatchSet.csv<jupyter_output><empty_output><jupyter_text>We'll use the Pandas naming convention df for our DataFrame. Make sure that the cell below uses the name for the dataframe used above. For the locally uploaded file it should look like df_data_1 or df_data_2 or df_data_x.**>**<jupyter_code># Replace df_data_1 with the variable name generated above.
batch_df = df_data_1<jupyter_output><empty_output><jupyter_text>2.5 Create JobWe can now use the information about the deployment and the test data to create a new job against our batch deployment. We submit the data as inline payload and want the results (i.e predictions) stored in a CSV file.<jupyter_code>import time
timestr = time.strftime("%Y%m%d_%H%M%S")
job_payload = {
    wml_client.deployments.ScoringMetaNames.INPUT_DATA: [{
        'fields': batch_df.columns.values.tolist(),
        'values': batch_df.values.tolist()
    }],
    wml_client.deployments.ScoringMetaNames.OUTPUT_DATA_REFERENCE: {
            "type": "data_asset",
            "connection": {},
            "location": {
                "name": "batchres_{}_{}.csv".format(timestr,deployment_uid),
                "description": "results"
            }
    }
}

job = wml_client.deployments.create_job(deployment_id=deployment_uid, meta_props=job_payload)
job_uid = wml_client.deployments.get_job_uid(job)

print('Job uid = {}'.format(job_uid))
wml_client.deployments.list_jobs()<jupyter_output><empty_output><jupyter_text>3.0 Monitor Batch Job StatusThe batch job is an async operation. We can use the identifier to track its progress. Below we will just poll until the job completes (or fails).<jupyter_code>def poll_async_job(client, job_uid):
    import time
    while True:
        job_status = client.deployments.get_job_status(job_uid)
        print(job_status)
        state = job_status['state']
        if state == 'completed' or 'fail' in state:
            return client.deployments.get_job_details(job_uid)
        time.sleep(5)
            
job_details = poll_async_job(wml_client, job_uid)
wml_client.deployments.list_jobs()<jupyter_output><empty_output><jupyter_text>3.1 Check ResultsWith the job complete, we can see the predictions.<jupyter_code>wml_client.deployments.get_job_details()
print(json.dumps(job_details, indent=2))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0899002493765586, 'fraction_numerical': 0.0046134663341645885, 'mean_word_length': 4.684620836286322, 'pattern_counts': {'":': 7, '<': 41, '<?xml version=': 0, '>': 49, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '210568', 'license': 'Apache-2.0', 'n_tokens_mistral': 2386, 'n_tokens_neox': 2328, 'n_words': 894, 'path': 'notebooks/machinelearning-creditrisk-batchscoring.ipynb', 'repo_name': 'malet-ds/Workshop-Digital-Developer-Conference-Data-AI', 'size': '8020'}