-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 Answers1

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]])
Epsi95
  • 8,832
  • 1
  • 16
  • 34
  • 1
    That is a `for` loop under the hood. – Mad Physicist Feb 02 '21 at 18:20
  • At least it saves from writing nested loops! Anyway if you have the better solution you can provide. – Epsi95 Feb 02 '21 at 18:23
  • 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 – Mad Physicist Feb 02 '21 at 18:25