Home > Enterprise >  extract a ndarray from a np.void array
extract a ndarray from a np.void array

Time:03-05

the npy file I used ⬆️ https://github.com/mangomangomango0820/DataAnalysis/blob/master/NumPy/NumPyEx/NumPy_Ex1_3Dscatterplt.npy

2. after loading the npy file,

data = np.load('NumPy_Ex1_3Dscatterplt.npy')
'''
[([   2,    2, 1920,  480],) ([   1,    3, 1923,  480],)
 ......
 ([   3,    3, 1923,  480],)]
 
 
⬆️ data.shape, (69,)
⬆️ data.shape, (69,)
⬆️ data.dtype, [('f0', '<i8', (4,))]
⬆️ type(data), <class 'numpy.ndarray'>
⬆️ type(data[0]), <class 'numpy.void'>
'''

you can see for each row, e.g. data[0],its type is <class 'numpy.void'>

I wish to get a ndarray based on the data above, looking like this ⬇️

[[   2    2 1920  480]
...
 [   3    3 1923  480]]

the way I did is ⬇️

all = np.array([data[i][0] for i in range(data.shape[0])])

'''
[[   2    2 1920  480]
...
 [   3    3 1923  480]]
'''

I am wondering if there's a smarter way to process the numpy.void class data and achieve the expected results.

CodePudding user response:

Here is the trick

data_clean = np.array(data.tolist())
print(data_clean)
print(data_clean.shape)

Output

[[[   2    2 1920  480]]

...............

 [[   3    3 1923  480]]]
(69, 1, 4)

In case if you dont like the extra 1 dimension in between, you can squeeze like this

data_sqz = data_clean.squeeze()
print(data_sqz)
print(data_sqz.shape)

Output

...
 [   3    3 1923  480]]
(69, 4)

  • Related