title: How to get cosine similiarity for 2 different lists?
body: <p>I'm trying to get cosine similarity for 2 sets of data (with unequal lengths).</p>
  <ol>
  <li>Test set contains 4 random similar images from google.</li>
  <li>Training set contains 1 similar image to test set from google.</li>
  </ol>
  <p>Following the code im using to do the same by converting image to vectors and calculating cosine similarity</p>
  <pre class="lang-py prettyprint-override"><code>import os
  
  from PIL import Image
  from sklearn.metrics.pairwise import cosine_similarity
  
  from img_to_vec import Img2Vec
  import numpy as np
  
  test_path = '/Users/Desktop/img_vec/test'
  train_path = '/Users/Desktop/img_vec/train'
  
  print(&quot;Getting vectors for test images...\n&quot;)
  img2vec = Img2Vec()
  
  # For each test image, we store the filename and vector as key, value in a dictionary
  pics = {}
  for file in os.listdir(test_path):
      filename = os.fsdecode(file)
      img = Image.open(os.path.join(test_path, filename))
      vec = img2vec.get_vec(img)
      pics[filename] = vec
  # print (pics)
  pic_name = {}
  for file1 in os.listdir(train_path):
      filename1 = os.fsdecode(file1)
      img1 = Image.open(os.path.join(train_path, filename1))
      vec1 = img2vec.get_vec(img1)
      pic_name[filename1] = vec1
  # print(pic_name)
  
  vec1 = np.array([pics])
  vec2 = np.array([pic_name])
  
  sims = {}
  for key in list(pics.keys()):
      print(key)
      sims[key] = cosine_similarity(vec1[vec2].reshape((1, -1)), vec1[key].reshape((1,   -1)))[0][0]
  
  d_view = [(v, k) for k, v in sims.items()]
  d_view.sort(reverse=True)
  for v, k in d_view:
      print(v, k)
  </code></pre>
  <p>However, I'm unable to resolve the following error:</p>
  <blockquote>
  <p>sims[key] = cosine_similarity(vec1[vec2].reshape((1, -1)), vec1[key].reshape((1, -1)))[0][0]
  IndexError: arrays used as indices must be of integer (or boolean) type</p>
  </blockquote>
  <p>I tried to compute cosine similarity in Python manually (using numpy) by using a specialised library. It doesn't work. I believe it's an issue with dtype.</p>
  <pre class="lang-py prettyprint-override"><code>import numpy as np
  from sklearn.metrics.pairwise import cosine_similarity
   
  # vectors
  a = np.array([1,2,3])
  b = np.array([1,1,4])
   
  # manually compute cosine similarity
  dot = np.dot(a, b)
  norma = np.linalg.norm(a)
  normb = np.linalg.norm(b)
  cos = dot / (norma * normb)
   
  # use library, operates on sets of vectors
  aa = a.reshape(1,3)
  ba = b.reshape(1,3)
  cos_lib = cosine_similarity(aa, ba)
  </code></pre>
  <p>Any help / guidance / alternative is much appreciated.</p>