-1

I am working on a machine learning task and trying to convert all strings in a set of data to floats using hash() to do this I need to iterate over all the elements of a numpy array whilst not knowing if it is a 2D 3D or 4D array and then change each element. Is there any way to do this without using nested loops?

1
  • Vectorization is a case-by-case activity. You will have to be very specific if you want a proper answer. Commented Feb 2, 2021 at 18:21

1 Answer 1

0

You can try numpu.vectorize, already mentioned here

Note: The vectorize function is provided primarily for convenience, not for performance. The implementation is essentially a for loop.here

arr = np.array([['aba', 'baa', 'bbb'],
              ['xxy', 'xyy', 'yyy']])
v_hash = np.vectorize(hash)
v_hash(arr)
array([[-1538054455328520296, -1528482088733019667, -7962229468338304433],
       [ 5621962119614158870,  1918700875003591346, -3216770211373729154]])
Sign up to request clarification or add additional context in comments.

3 Comments

That is a for loop under the hood.
At least it saves from writing nested loops! Anyway if you have the better solution you can provide.
It's a bad idea. It just hides the slowness under a false veneer of numpyness. I left a comment explaining why I can't. OP did not ask a proper question. You can't vectorize code without knowing what it is

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.