Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Numpy Error : Implicit conversion to a NumPy array is not allowed. Please use `.get()` to construct a NumPy array explicitly

I ‘am trying to find similar vector with spacy and numpy. I found the code following url :
Mapping word vector to the most similar/closest word using spaCy

But I’m getting type error

import numpy as np

your_word = "country"

ms = nlp.vocab.vectors.most_similar(
    np.asarray([nlp.vocab.vectors[nlp.vocab.strings[your_word]]]), 
    n=10, )

words = [nlp.vocab.strings[w] for w in ms[0][0]] distances = ms[2] print(words)

error :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[139], line 6
      1 import numpy as np
      3 your_word = "country"
      5 ms = nlp.vocab.vectors.most_similar(
----> 6     np.asarray([nlp.vocab.vectors[nlp.vocab.strings[your_word]]]), 
      7     n=10,
      8 )
     10 words = [nlp.vocab.strings[w] for w in ms[0][0]]
     11 distances = ms[2]

File cupy/_core/core.pyx:1475, in cupy._core.core._ndarray_base.__array__()

TypeError: Implicit conversion to a NumPy array is not allowed. Please use `.get()` to construct a NumPy array explicitly.

I’m using gpu, how can I fix this?

>Solution :

From the

Reference – Mapping word vector to the most similar/closest word using spaCy
Reference – https://docs.cupy.dev/en/stable/reference/ndarray.html

You need to convert CuPy arrays to be explicitly converted to NumPy arrays before operations on the CPU

Edit lets make reshape the word vector into a 2D array with one row and multiple columns

import numpy as np

your_word = "country"


word_vector = nlp.vocab.vectors[nlp.vocab.strings[your_word]]


word_vector_2d = word_vector.reshape(1, -1)  # '-1' lets numpy infer the correct size for the second dimension


ms = nlp.vocab.vectors.most_similar(
    word_vector_2d, 
    n=10,
)


words = [nlp.vocab.strings[w] for w in ms[0][0]]
distances = ms[2]
print(words)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading