【发布时间】:2020-10-19 20:34:18
【问题描述】:
我有一个流派列表
genres_list=['Action', 'Adventure', 'Animation', 'Children', 'Comedy']
我有一个神经网络,可以预测这部电影的类型顺序。根据预测,我保留了前 3 种类型的索引。例如,如果一部电影是 [“Animation”、“Comedy”、“Children”],那么我将有一个类似 [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')})
【问题讨论】:
标签: python pandas list indexing