-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,420
  • 1
  • 12
  • 30