UFuncNoLoopError: ufunc ‘multiply’ did not contain a loop with signature matching types

Hi Folks,

I have been away from blogging for some time and it’s a pleasure to be back. As I always enjoy sharing and collaborating on what I’ve learned, but sometimes it’s challenging to find time amidst professional and personal commitments to documents my insights.

Enough! Nowadays, I’m working on a couple of LLM use cases and getting my hands dirty with Python. I’ve been spending enough time acquainting myself with OpenAI, LLM, and the Python language, and I love it:)

I came across below error while getting dataframe along with embedding back from csv file and trying to search matching results with cosine-similarity.

numpy.core._exceptions._UFuncNoLoopError: ufunc 'multiply' did not contain a loop with signature matching types (dtype('<U20941'), dtype('<U23')) -> None

The error I’m encountering is due to the fact that when you save the embeddings to a CSV file and then read them back, they are no longer in the original format (i.e. numpy array). Instead, they are read back as strings. Therefore, when you try to perform operations like cosine similarity, it fails because it expects numerical data.

To fix this, you need to convert the embedding back to their original format after reading them from the CSV file. Here’s how you can do it:

import numpy as np
import ast

# Read DataFrame from CSV
df = pd.read_csv('embeddings.csv')

# Convert embeddings from string format back to numpy array
df['ada_v2_embedding'] = df['ada_v2_embedding'].apply(lambda x: np.array(ast.literal_eval(x)))

# Now you can calculate cosine similarity
df["similarities"] = df.ada_v2_embedding.apply(lambda x: cosine_similarity(x, embedding))

In above code snippet, `ast.literal_eval(x)` is used to convert the string representation of the list back to a list, and `np.array()` is used to convert the list to a numpy array. Now, the `cosine_similarity` function should work as expected.

Happy Coding 🙂

Leave a comment