In [1]:
!pip install --upgrade "ibm-watson>=4.0.1"
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]:
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]:
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)
In [9]:
confusion_matrix(test.iloc[:,1], predicted)
Out[9]:
In [ ]: