Using the Natural Language Classifier API in notebooks

You can work collaboratively with your teammates in notebooks in IBM Watson Studio, wrangling text training data and test data, and working with your IBM Watson Natural Language Classifier models by calling the Natural Language Classifier API.

 

Before you begin

Train your model

 

Python notebook example

Here is an example of Python code that classifies text using a trained Natural Language Classifier model.

This sample Python code calls the Natural Language Classifier API external link and can be run in a notebook in IBM Watson Studio.

About the example classifier

In this example, the classifier was trained with this sample training data: Customer messages external link

The classifier classifies a given “customer message” into one of three classes:

  • “question” : The message is asking a question
  • “problem” : The message is describing a problem or failure
  • “hi” : The message is either just social or doesn’t contain any specific technical details

You can see these three classes listed in the sample output. The output also shows a confidence score that indicates how well the given test text fits the class.

Sample code

!pip install --upgrade "watson-developer-cloud>=1.3.0"
from watson_developer_cloud import NaturalLanguageClassifierV1
natural_language_classifier = NaturalLanguageClassifierV1( iam_api_key='<iam_api_key>' )
classes = natural_language_classifier.classify_collection(
    '<model-ID>', 
    [ { 'text' : 'Good to see you' },
      { 'text' : 'Im having trouble loading in my data for some reason wont let me upload files' },
      { 'text' : 'Hi can you tell me how to load a shape file' }
    ]
)
import json
print( json.dumps( classes, indent=3 ) )

For information about how to look up your API key and model ID, see: Building Natural Language Classifier apps.

Sample output

{
   "url": "https://gateway.watsonplatform.net...",
   "classifier_id": "<model-ID>",
   "collection": [
      {
         "classes": [
            {
               "confidence": 1.0,
               "class_name": "hi"
            },
            {
               "confidence": 0.0,
               "class_name": "problem"
            },
            {
               "confidence": 0.0,
               "class_name": "question"
            }
         ],
         "top_class": "hi",
         "text": "Good to see you"
      },
      {
         "classes": [
            {
               "confidence": 0.9945372081125501,
               "class_name": "problem"
            },
            {
               "confidence": 0.00371893839300035,
               "class_name": "question"
            },
            {
               "confidence": 0.0017438534944495111,
               "class_name": "hi"
            }
         ],
         "top_class": "problem",
         "text": "Im having trouble loading in my data for some reason wont let me upload files"
      },
      {
         "classes": [
            {
               "confidence": 0.9940285591527018,
               "class_name": "question"
            },
            {
               "confidence": 0.003222358741777794,
               "class_name": "problem"
            },
            {
               "confidence": 0.0027490821055203723,
               "class_name": "hi"
            }
         ],
         "top_class": "question",
         "text": "Hi can you tell me how to load a shape file"
      }
   ]
}

 

Next steps