运行系统时具有相同的输出

我已经实现了情感检测分析,我已经成功地训练了我的模型,然后我完成了预测部分,我在一个列表中得到了我的答案,现在我试图只有一个答案,我想有一个最大的答案,但我对每个输出都有相同的答案。有人可以帮我纠正我的错误吗?


这是我的代码:


        with open('output2.json', 'w') as f:

            json.dump(new_data, f)


selection1 = new_data['selection1']

#creating empty list to be able to create a dataframe

names = []

dates = []

commentss = []

labels = []

hotelname = []

for item in selection1:

    name = item['name']

    hotelname.append(name)

    #print ('>>>>>>>>>>>>>>>>>> ', name)

    Date = item['reviews']

    for d in Date:

        names.append(name)

        #convert date from 'january 12, 2020' to 2020-01-02

        date = pd.to_datetime(d['date']).strftime("%Y-%m-%d")

        #adding date to the empty list dates[]

        dates.append(date)

        #print('>>>>>>>>>>>>>>>>>> ', date)

    CommentID = item['reviews']

    for com in CommentID:

        comment = com['review']

        lcomment = comment.lower()  # converting all to lowercase

        result = re.sub(r'\d+', '', lcomment)  # remove numbers

        results = (result.translate(

        str.maketrans('', '', string.punctuation))).strip()  # remove punctuations and white spaces

        comments = remove_stopwords(results)

        commentss.append(comment)

        print('>>>>>>',comments)


    #add the words in comments that are already present in the keys of dictionary

        encoded_samples = [[word2id[word] for word in comments if word in word2id.keys()]]



    # Padding

        encoded_samples = keras.preprocessing.sequence.pad_sequences(encoded_samples, maxlen=max_words)


     # Make predictions

        label_probs, attentions = model_with_attentions.predict(encoded_samples)

        label_probs = {id2label[_id]: prob for (label, _id), prob in zip(label2id.items(), label_probs[0])}

        labels.append(label_probs)

       #Get word attentions using attenion vector

        print(label_probs)

        print(max(label_probs))

你能看到吗,它无处不在,它给了我“信任”,但是我希望显示列表中的最高分,我需要在我的代码中更正什么?


qq_笑_17
浏览 131回答 1
1回答

潇湘沐

您正在字典label_probs上使用 max 函数,该函数返回字典中按字母顺序排列的最大键。为了达到预期的结果,您必须,取代:print(max(label_probs))跟:print(max(label_probs, key=lambda k: label_probs[k]))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python