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?
-
Vectorization is a case-by-case activity. You will have to be very specific if you want a proper answer.Mad Physicist– Mad Physicist2021-02-02 18:21:09 +00:00Commented Feb 2, 2021 at 18:21
Add a comment
|
1 Answer
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]])
3 Comments
Mad Physicist
That is a
for loop under the hood.Epsi95
At least it saves from writing nested loops! Anyway if you have the better solution you can provide.
Mad Physicist
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