How to convert a Numpy 2D array with object dtype to a regular 2D array of floats

后端 未结 6 1529
太阳男子
太阳男子 2020-12-09 10:02

As part of broader program I am working on, I ended up with object arrays with strings, 3D coordinates and etc all mixed. I know object arrays might not be very favorite in

6条回答
  •  -上瘾入骨i
    2020-12-09 10:48

    This is way faster to just convert your object array to a NumPy float array: arr=np.array(arr, dtype=[('O', np.float)]).astype(np.float) - from there no looping, index it just like you'd normally do on a NumPy array. You'd have to do it in chunks though with your different datatypes arr[:, 1], arr[:,2], etc. Had the same issue with a NumPy tuple object returned from a C++ DLL function - conversion for 17M elements takes <2s.

提交回复
热议问题