<jupyter_start><jupyter_text>Calculating similarity1. set query (which row we are looking at)2. loop through the rest of the paintings calculating similarity to query3. sort similarity scores and save the IDs for the 5 most similar4. build spreadsheet of top 5 comps for each painting ( ID, comp1, comp2, comp3, comp4, comp5 )<jupyter_code>def chi2_distance(histA, histB, eps = 1e-10):
        # compute the chi-squared distance
        dist = 0.5 * np.sum( [ ((a - b) ** 2) / (a + b + eps) 
                              for (a, b) in zip(histA, histB) ] )
        
        # return the chi-squared distance
        return dist
merged['aid'][merged['aid']=='a1983_006']
#
comps = []

#
for i in range(len(merged)):
    
    #
    aid = merged['aid'].at[i]
    query = merged.iloc[i, 1:].tolist()
    
    #
    results = {}
    
    #
    for j in range(len(merged)):
        features = merged.iloc[j, 1:].tolist()
        dist = chi2_distance(query, features)

        results[merged['aid'].at[j]] = dist

    # sort the results smallest to largest
    results = sorted( [(v, k) for (k, v) in results.items()] )

    #
    results[0] = aid
    results = results[:7]
    comps.append(results)
comps[555]
img = Image.open('../Paintings/2000.010.png')
plt.imshow(img);
img = Image.open('../Paintings/1995.017.png')
plt.imshow(img);
full_comps = []

for i in range(len(comps)):
    aid = comps[i][0]
    temp = []
    for j in range(1,6):
        comp = comps[i][j][1]
        temp.append(comp)
    all_sim = [aid] + temp
    full_comps.append(all_sim)
    
    
full_comps[500]
test_df = pd.DataFrame(full_comps, columns=['aid', '1', '2', '3', '4', '5'])
test_df.head()
test_df.to_csv('top_5_sim.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1549053356282272, 'fraction_numerical': 0.03442340791738382, 'mean_word_length': 2.791304347826087, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '570088', 'license': 'MIT', 'n_tokens_mistral': 674, 'n_tokens_neox': 623, 'n_words': 160, 'path': 'src/Notebooks/ColorTextureShapeSimilarity.ipynb', 'repo_name': 'AndrewMarksArt/Content_Based_Image_Retrieval_System', 'size': '1743'}