-2

I am making a model using word2vec. After training the model i was using cosine similarity. But i am getting the following error. I am using python 3 The code I used is as follows:

import numpy as np
from sklearn.metrics.pairwise import cosine_distances
cos_dist =[]
cos_dist =[cos_dist]
cos_dist = np.array(cos_dist).reshape(1, -1)
for vec in data[:-1]:
    cos_dist.append(float(cosine_distances(vec,data[-1])))

I am getting the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call 
last)
<ipython-input-14-ef6e7efe7eaa> in <module>
      5 cos_dist = np.array(cos_dist).reshape(1, -1)
      6 for vec in data[:-1]:
----> 7     cos_dist.append(float(cosine_distances(vec,data[-1])))
      8 
      9 

AttributeError: 'numpy.ndarray' object has no attribute 'append'
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Aditya ojha
  • 47
  • 2
  • 5

2 Answers2

4

You can use np.append which doesn't work inplace:

cos_dist = np.append(cos_dist, [float(cosine_distances(vec,data[-1]))])
zipa
  • 27,316
  • 6
  • 40
  • 58
0

You can use numpy.concatenate(list1, list2) or numpy.append().

There's a similar discussion in this thread

lsfischer
  • 344
  • 2
  • 14