In [1]:
!pip install --upgrade "ibm-watson>=4.0.1"


Collecting ibm-watson>=4.0.1
  Downloading https://files.pythonhosted.org/packages/0e/9d/6ffcc65ec8fde09bb942aa2024cb1f9aeaca03ad4b4ec16ab9ad6955492b/ibm-watson-4.1.0.tar.gz (324kB)
     |████████████████████████████████| 327kB 6.1MB/s eta 0:00:01
Requirement already satisfied, skipping upgrade: requests<3.0,>=2.0 in /opt/conda/envs/Python36/lib/python3.6/site-packages (from ibm-watson>=4.0.1) (2.21.0)
Requirement already satisfied, skipping upgrade: python_dateutil>=2.5.3 in /opt/conda/envs/Python36/lib/python3.6/site-packages (from ibm-watson>=4.0.1) (2.7.5)
Collecting websocket-client==0.48.0 (from ibm-watson>=4.0.1)
  Downloading https://files.pythonhosted.org/packages/8a/a1/72ef9aa26cfe1a75cee09fc1957e4723add9de098c15719416a1ee89386b/websocket_client-0.48.0-py2.py3-none-any.whl (198kB)
     |████████████████████████████████| 204kB 23.3MB/s eta 0:00:01
Collecting ibm_cloud_sdk_core==1.0.0 (from ibm-watson>=4.0.1)
  Downloading https://files.pythonhosted.org/packages/e8/43/a13a5956c69b7becce7a0df6d2340c1e32322df3b39f57a3b33dc4645a34/ibm-cloud-sdk-core-1.0.0.tar.gz
Requirement already satisfied, skipping upgrade: urllib3<1.25,>=1.21.1 in /opt/conda/envs/Python36/lib/python3.6/site-packages (from requests<3.0,>=2.0->ibm-watson>=4.0.1) (1.24.1)
Requirement already satisfied, skipping upgrade: idna<2.9,>=2.5 in /opt/conda/envs/Python36/lib/python3.6/site-packages (from requests<3.0,>=2.0->ibm-watson>=4.0.1) (2.8)
Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in /opt/conda/envs/Python36/lib/python3.6/site-packages (from requests<3.0,>=2.0->ibm-watson>=4.0.1) (2019.11.28)
Requirement already satisfied, skipping upgrade: chardet<3.1.0,>=3.0.2 in /opt/conda/envs/Python36/lib/python3.6/site-packages (from requests<3.0,>=2.0->ibm-watson>=4.0.1) (3.0.4)
Requirement already satisfied, skipping upgrade: six>=1.5 in /opt/conda/envs/Python36/lib/python3.6/site-packages (from python_dateutil>=2.5.3->ibm-watson>=4.0.1) (1.12.0)
Collecting PyJWT>=1.7.1 (from ibm_cloud_sdk_core==1.0.0->ibm-watson>=4.0.1)
  Downloading https://files.pythonhosted.org/packages/87/8b/6a9f14b5f781697e51259d81657e6048fd31a113229cf346880bb7545565/PyJWT-1.7.1-py2.py3-none-any.whl
Building wheels for collected packages: ibm-watson, ibm-cloud-sdk-core
  Building wheel for ibm-watson (setup.py) ... done
  Stored in directory: /home/dsxuser/.cache/pip/wheels/cc/33/18/90271d1fb07b27d9958bd89d5e4858196c844b69b089050cdc
  Building wheel for ibm-cloud-sdk-core (setup.py) ... done
  Stored in directory: /home/dsxuser/.cache/pip/wheels/12/e5/b5/90c69cc81edf5b95cbec373c83c3530266fdae8266037380df
Successfully built ibm-watson ibm-cloud-sdk-core
Installing collected packages: websocket-client, PyJWT, ibm-cloud-sdk-core, ibm-watson
Successfully installed PyJWT-1.7.1 ibm-cloud-sdk-core-1.0.0 ibm-watson-4.1.0 websocket-client-0.48.0

In [2]:
# @hidden_cell
import json
from ibm_watson import NaturalLanguageClassifierV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

authenticator = IAMAuthenticator('akM7V1ISmAWD_JbOWeBFgfOvgCtBX6H6g9FfM4dTf4Vb',url='https://iam.cloud.ibm.com/identity/token')
nlc = NaturalLanguageClassifierV1(authenticator)
nlc.set_default_headers({'url':'https://gateway.watsonplatform.net/natural-language-classifier/api'})

In [3]:
import types
import pandas as pd
from botocore.client import Config
import ibm_boto3

def __iter__(self): return 0

# @hidden_cell
# The following code accesses a file in your IBM Cloud Object Storage. It includes your credentials.
# You might want to remove those credentials before you share the notebook.
client_d583ed9871cd4c0f989be20d8872167e = ibm_boto3.client(service_name='s3',
    ibm_api_key_id='UWesfz1VTTZeniaqrn7yuKnXLHifp180n9DtgSaLgowe',
    ibm_auth_endpoint="https://iam.ng.bluemix.net/oidc/token",
    config=Config(signature_version='oauth'),
    endpoint_url='https://s3-api.us-geo.objectstorage.service.networklayer.com')

body = client_d583ed9871cd4c0f989be20d8872167e.get_object(Bucket='laboratriowatsonstudio-donotdelete-pr-pbreu1ixybb27s',Key='test_cerveja.csv')['Body']
# add missing __iter__ method, so pandas accepts body as file-like object
if not hasattr(body, "__iter__"): body.__iter__ = types.MethodType( __iter__, body )

test = pd.read_csv(body, header=None)
test.head()


Out[3]:
0 1
0 Trigo é o vilão dos alimentos! Elimine cerveja... saude
1 Com a redução de viagens, e de emissão de carb... ambiente
2 Cerveja com arte, sustentabilidade, re-uso ou ... ambiente
3 Cerveja evita engordar, aponta pesquisa <a tar... saude
4 Depois de uma corrida de kart, uma alcatra na ... comportamento

In [4]:
predicted = []
for i in range(0,test.shape[0]): 
    classes = nlc.classify('94904ex626-nlc-66',test.iloc[i,0]).get_result()
    predicted.append(classes['top_class'])

In [5]:
resultados = pd.DataFrame({'real': test.iloc[:,1], 'predicted': predicted})

In [6]:
resultados.head()


Out[6]:
real predicted
0 saude saude
1 ambiente ambiente
2 ambiente ambiente
3 saude saude
4 comportamento comportamento

In [7]:
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
report = classification_report(test.iloc[:,1], predicted)

In [8]:
print(report)


               precision    recall  f1-score   support

     ambiente       1.00      0.96      0.98        23
comportamento       0.81      0.81      0.81        21
 conhecimento       0.76      0.84      0.80        19
     economia       1.00      0.95      0.98        22
  responsavel       1.00      0.95      0.97        19
        saude       0.87      0.91      0.89        22

    micro avg       0.90      0.90      0.90       126
    macro avg       0.91      0.90      0.90       126
 weighted avg       0.91      0.90      0.91       126


In [9]:
confusion_matrix(test.iloc[:,1], predicted)


Out[9]:
array([[22,  0,  1,  0,  0,  0],
       [ 0, 17,  2,  0,  0,  2],
       [ 0,  3, 16,  0,  0,  0],
       [ 0,  0,  1, 21,  0,  0],
       [ 0,  0,  0,  0, 18,  1],
       [ 0,  1,  1,  0,  0, 20]])

In [ ]: