用元素名称替换索引,同时保留名称的顺序而不是索引的顺序

尼克斯

我有一个流派列表

genres_list=['Action', 'Adventure', 'Animation', 'Children', 'Comedy']

而且我有神经网络可以预测这部电影的流派顺序。根据预测,我保留了前3种类型的索引。因此,例如,如果电影是[“动画”,“喜剧”,“儿童”],那么我的预测将像[2 4 3]。然后,用初始列表中的名称替换索引[2 4 3]。

我的当前输出是[“ Animation”,“ Children”,“ Comedy”],因为第一个索引已被第一次替换。但是由于保持正确的流派顺序(顺序)很重要,我希望最终输出像[“ Animation”,“ Comedy”,“ Children”]->正确的索引预测

我的功能(产生不良结果)

def predict_genre_tags(model, genres_list):
        
    test_sequence_actors = X_test_seq_actors[0:0+1]
    
    test_sequence_plot = X_test_seq_plot[0:0+1]
    
    test_sequence_features = X_test_seq_features[0:0+1]
    
    test_sequence_reviews = X_test_seq_reviews[0:0+1]
    
    text_prediction = model.predict([test_sequence_actors, test_sequence_plot, test_sequence_features, test_sequence_reviews])
    
    [float(i) for i in text_prediction[0]]
    
    tag_probabilities = text_prediction[0][np.argsort(text_prediction[0])[-3:]]
    
    indexes = np.argsort(text_prediction[0])[::-1][:3] #keep the genres with the top 3 probabilities and their index.
    
    print(indexes) # indexes= [2 4 3] based on my description
    
    predicted_tags = []
    
    for i, tag in enumerate(genres_list): #here is my problem...because the first inside the loop is the first replaced
        if i in indexes:
            predicted_tags.append(genres_list[i])
    
    return predicted_tags
df_predictions = pd.DataFrame({'Movie Title':pd.Series("Toy Story", dtype='str'),
                               'Predicted Genre tags (top 3)':pd.Series([predict_genre_tags(model, genres_list)], dtype='str') #which yields ["Animation", "Children", "Comedy"] genres in incorrect order,
                               'Real Genre tags':pd.Series(["Animation", "Comedy", "Children"], dtype='str')})
螳螂

您可以简单地做到这一点predicted_tags = [genres_list[i] for i in indexes]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章