-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'
3
  • Yes, it doesn't have such a method. What's the problem here? Commented Jun 27, 2019 at 11:11
  • 1
    As error says numpy array doesn't have any attribute called append. You can use list to append values Commented Jun 27, 2019 at 11:11
  • Don't use np.append. It's hard to use correctly, and slow when used repeatedly. Commented Jun 27, 2019 at 15:26

2 Answers 2

4

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

cos_dist = np.append(cos_dist, [float(cosine_distances(vec,data[-1]))])
Sign up to request clarification or add additional context in comments.

Comments

0

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

There's a similar discussion in this thread

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.