[英]How to save subarray in npy file?
My data tracks has following shape : 我的数据轨道具有以下形状:
(13044,) Its data types are (13044,)其数据类型为
tracks.dtype.names
('frame_num','mean_x','mean_y','var_x','var_y', 'length', 'scale', 'x_pos','y_pos', 't_pos', 'coords', 'trajectory', 'hog', 'hof', 'mbh_x','mbh_y')
dtype([('frame_num', '<i4'), ('mean_x', '<f4'), ('mean_y', '<f4'), ('var_x', '<f4'), ('var_y', '<f4'), ('length', '<f4'), ('scale', '<f4'), ('x_pos', '<f4'), ('y_pos', '<f4'), ('t_pos', '<f4'), ('coords', '<f4', (16, 2)), ('trajectory', '<f4', (15, 2)), ('hog', '<f4', (96,)), ('hof', '<f4', (108,)), ('mbh_x', '<f4', (96,)), ('mbh_y', '<f4', (96,))])
I need to save (16,2) coordinates of 13044 data into new npy file . 我需要将13044数据的(16,2)坐标保存到新的npy文件中。 I tried following:
我尝试了以下操作:
x=tracks['coords']
for i in range(0,len(tracks)):
y=tracks['coords'][i]
print(y)
np.save('test.npy',y)
The output of y: There data of 13044, I have put some only y的输出:有13044的数据,我只输入了一些
[[182. 92. ]
[182.54565 92.09981 ]
[183.10211 91.61575 ]
[183.64021 92.13559 ]
[184.27351 92.15997 ]
[185.0328 92.20285 ]
[185.6495 92.19383 ]
[185.88063 92.225876]
[186.3553 92.30736 ]
[187.29843 92.38876 ]
[187.89871 92.38898 ]
[188.25539 92.46452 ]
[188.98816 92.39856 ]
[189.5047 92.37273 ]
[189.76077 92.67736 ]
[190.50615 92.31434 ]]
[[187. 92. ]
[187.56187 92.08742 ]
[188.12775 91.60125 ]
[188.64186 91.94049 ]
[189.10121 91.90893 ]
[189.9543 92.00123 ]
[190.43088 92.01...
..... goes on until end
[[265.87213 209.30359]
[266.8972 208.9946 ]
[267.89746 208.38165]
[268.8108 207.88152]
[269.64877 207.46448]
[270.36688 207.13185]
[271.16782 206.77945]
[271.74063 206.21416]
[272.45694 205.88182]
[273.10373 205.73294]
[273.6556 205.66495]
[274.32462 205.54205]
[275.11664 205.4512 ]
[276.0263 205.37993]
[276.99155 205.18765]
[277.99423 205.0822 ]]
While I load test.npy it doesnot save all y .It just save last array of coords: 当我加载test.npy时,它不会保存所有y,而只会保存最后一组坐标:
data='test.npy'
data1=np.load(data)
data1
Output of data1: 数据1的输出:
array([[265.87213, 209.30359],
[266.8972 , 208.9946 ],
[267.89746, 208.38165],
[268.8108 , 207.88152],
[269.64877, 207.46448],
[270.36688, 207.13185],
[271.16782, 206.77945],
[271.74063, 206.21416],
[272.45694, 205.88182],
[273.10373, 205.73294],
[273.6556 , 205.66495],
[274.32462, 205.54205],
[275.11664, 205.4512 ],
[276.0263 , 205.37993],
[276.99155, 205.18765],
[277.99423, 205.0822 ]], dtype=float32)
How can I save all (16,2) coords of 13044 data ? 如何保存13044数据的所有(16,2)坐标?
You can save a numpy array of all of your (16,2) y's: 您可以保存所有(16,2)个y的numpy数组:
ys = []
for i in range(len(tracks)):
y=tracks['coords'][i]
ys.append(y)
print(y)
np.save('test.npy',np.array(ys))
Also note that you are traversing along tracks
with i
, but reading tracks['coord'][i]
each time (could be different lengths). 另请注意,您正在使用
i
遍历tracks
,但是每次都读取tracks['coord'][i]
(可能是不同的长度)。 In addition, if not all y's are in the same length than it would be a problem to create a numpy array out of them, but you can still save a list through np.save
and when loading them use np.load('test.npy').item()
(again, if you're not saving them as np.array
). 另外,如果不是所有的y都具有相同的长度,那么用它们创建一个numpy数组将是一个问题,但是您仍然可以通过
np.save
保存一个列表,并在加载它们时使用np.load('test.npy').item()
(同样,如果您没有将它们np.array
为np.array
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.