IBM Watson如何分析数据框

问题描述 投票:0回答:1

我想使用IBM Watson分析CSV文件。我应该怎么做?我已将其导入为数据框,并尝试对其进行分析,但出现错误。然后,我尝试分析文件本身,但仍然出现错误。我需要帮助来解决此问题。

from ibm_watson import ApiException
from pandas.io.json import json_normalize
try:
    with open('C:\\Users\\Desktop\\WORK\\Example Data.csv') as f:
        response3 = natural_language_understanding.analyze(text = f,features=Features(concepts=ConceptsOptions())).get_result()

except ApiException as ex:
    print ("Method failed with status code" + str(ex.code) + ": " + ex.message)

我收到以下错误:

TypeError: Object of type TextIOWrapper is not JSON serializable

是否有任何方法可以发送数据帧或整个文件以从Watson中进行分析

python nlp ibm-watson watson-nlu
1个回答
0
投票

解决了问题。这是一个菜鸟的错误。我没有使用'f.read()'。一旦输入,代码就会执行。修改后的代码如下所示:

try:
    with open('C:\\Users\\Desktop\\WORK\\Example Data.csv') as f:
        response3 = natural_language_understanding.analyze(text = f.read(),features=Features(concepts=ConceptsOptions())).get_result()

except ApiException as ex:
    print ("Method failed with status code" + str(ex.code) + ": " + ex.message)

现在,如果要分析数据框,则必须将其转换为文本,然后传递给IBM Watson。我使用下面的代码,它为我完成了工作

pi_text = df['Example Content'][0:5].str.cat(sep='. ').encode('ascii', 'ignore')

我正在尝试寻找个性见解。上面的代码将Dataframe转换为文本(我只选择了前五行。如果要使用整个Dataframe,请删除[0:5]

profile = personality_insights.profile(pi_text, content_type='text/plain', accept='application/json').get_result()

以上代码将提供个性见解。请注意,输出为JSON格式

© www.soinside.com 2019 - 2024. All rights reserved.