如何在npy文件中保存子数组? [英] How to save subarray in npy file?

查看:76
本文介绍了如何在npy文件中保存子数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据轨迹具有以下形状:

My data tracks has following shape :

(13044,)它的数据类型是

(13044,) Its data types are

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,))])

我需要将 13044 数据的 (16,2) 坐标保存到新的 npy 文件中.我尝试了以下操作:

I need to save (16,2) coordinates of 13044 data into new npy file . I tried following:

x=tracks['coords']
for i in range(0,len(tracks)):
    y=tracks['coords'][i]
    print(y)
    np.save('test.npy',y)

y的输出:有13044的数据,我只放了一些

The output of y: There data of 13044, I have put some only

[[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 ]]

当我加载 test.npy 时,它并没有保存所有 y .它只是保存了最后一个坐标数组:

While I load test.npy it doesnot save all y .It just save last array of coords:

data='test.npy'
data1=np.load(data)
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)

如何保存 13044 数据的所有 (16,2) 坐标?

How can I save all (16,2) coords of 13044 data ?

推荐答案

您可以保存所有 (16,2) y 的 numpy 数组:

You can save a numpy array of all of your (16,2) y's:

ys = []
for i in range(len(tracks)):
    y=tracks['coords'][i]
    ys.append(y)
    print(y)
np.save('test.npy',np.array(ys))

另请注意,您使用 i 沿着 tracks 遍历,但每次都读取 tracks['coord'][i](可能长度不同).此外,如果不是所有 y 的长度都相同,那么从它们中创建一个 numpy 数组就会出现问题,但您仍然可以通过 np.save 保存列表,并在加载它们时使用np.load('test.npy').item()(同样,如果你没有将它们保存为 np.array).

Also note that you are traversing along tracks with i, but reading tracks['coord'][i] each time (could be different lengths). 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).

这篇关于如何在npy文件中保存子数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆