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

后端 未结 6 1531
太阳男子
太阳男子 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条回答
  •  春和景丽
    2020-12-09 10:28

    Based on Jaime's toy example I think you can do this very simply using np.vstack():

    arr = np.array([['one', [1, 2, 3]],['two', [4, 5, 6]]], dtype=np.object)
    float_arr = np.vstack(arr[:, 1]).astype(np.float)
    

    This will work regardless of whether the 'numeric' elements in your object array are 1D numpy arrays, lists or tuples.

提交回复
热议问题