Himal Acharya
Himal Acharya

Reputation: 1016

How to save subarray in npy file?

My data tracks has following shape :

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

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)

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

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

Output of data1:

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 ?

Upvotes: 0

Views: 257

Answers (1)

Ron U
Ron U

Reputation: 518

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

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).

Upvotes: 1

Related Questions