text_html_output_1.png
def get_recommendations(indices, cosine_sim, index, df, top_n=5):
    sim_index = indices[index]
    sim_scores = list(enumerate(cosine_sim[sim_index]))
    sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
    sim_scores = sim_scores[1:top_n + 1]
    index_rec = [i[0] for i in sim_scores]
    index_sim = [i[1] for i in sim_scores]
    return (indices.iloc[index_rec].index, index_sim)
cosine_sim = 1 - pairwise_distances(df_embds_vgg16, metric='cosine')
indices = pd.Series(range(len(df)), index=df.index)
mean_sim_scores_vgg = []
complete_indices_vgg = []
complete_scores_vgg = []
for index in range(df.shape[0]):
    try:
        index, sim_array = get_recommendations(indices, cosine_sim, index, df, top_n=5)
        mean_score = np.mean(sim_array)
        mean_sim_scores_vgg.append(mean_score)
        complete_indices_vgg.append(index)
        complete_scores_vgg.append(sim_array)
    except Exception as ex:
        print(f'Following exception : {ex} occured at the index : {index}')