我有形状[13044,]与其中“COORDS”给出轨迹数据的坐标的轨迹数据的NPY文件。它有16个dtypes与以下
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,))])
tracks[0]
给
(15, 186.33582, 92.24783, 2.6624618, 0.22911347, 9.241288, 1., 0.51759946, 0.38436598, 0.04166667, [[182. , 92. ], [182.5449 , 92.09226 ], [183.10237 , 91.61799 ], [183.64798 , 92.135086], [184.26779 , 92.16737 ], [185.04352 , 92.200066], [185.68985 , 92.19117 ], [185.94293 , 92.222855], [186.41994 , 92.30446 ], [187.35497 , 92.39461 ], [187.94893 , 92.39315 ], [188.32193 , 92.47165 ], [189.05319 , 92.40751 ], [189.58992 , 92.37913 ], [189.8503 , 92.6842 ], [190.59416 , 92.30396 ]], [[ 0.05896436, 0.0099837 ],...
第一个元素是最后frame_number,从10种th元素它给轨迹的坐标16从开始第一帧的第二坐标和最后是第15帧的
[182.5449 , 92.09226 ], 1st frame feature point
[183.10237 , 91.61799 ],trajectory of that feature point at second frame..
[183.64798 , 92.135086], [184.26779 , 92.16737 ], [185.04352 , 92.200066], [185.68985 , 92.19117 ], [185.94293 , 92.222855], [186.41994 , 92.30446 ], [187.35497 , 92.39461 ], [187.94893 , 92.39315 ], [188.32193 , 92.47165 ], [189.05319 , 92.40751 ], [189.58992 , 92.37913 ], [189.8503 , 92.6842 ], [190.59416 , 92.30396] trajectory of feature point at last frame
tracks[500]
(19, 101.504395, 133.30353, 4.431484, 0.41493973, 14.826186, 1.9999999, 0.28195664, 0.55543137, 0.06388889, [[ 93.99999 , 133.99998 ], [ 95.10257 , 133.91016 ], [ 96.17397 , 133.8509 ], [ 97.26843 , 133.84372 ], [ 98.360085, 133.75041 ], [ 99.31606 , 133.46017 ], [100.22412 , 133.29907 ], [101.10831 , 133.0922 ], [102.029564, 132.93976 ], [102.92373 , 132.95956 ], [103.90272 , 132.80684 ], [104.84618 , 132.84692 ], [105.69725 , 132.90834 ], [106.740944, 133.00175 ], [107.67655 , 133.08873 ], [108.69984 , 133.09813 ]], [[ 0.07436688,....
第一元件 - 最后一帧是19从10 th元素它给轨迹的16个坐标
[95.10257 , 133.91016 ],5th frame of a feature point
[ 96.17397 , 133.8509 ], [ 97.26843 , 133.84372 ], [ 98.360085, 133.75041 ], [ 99.31606 , 133.46017 ], [100.22412 , 133.29907 ], [101.10831 , 133.0922 ], [102.029564, 132.93976 ], [102.92373 , 132.95956 ], [103.90272 , 132.80684 ], [104.84618 , 132.84692 ], [105.69725 , 132.90834 ], [106.740944, 133.00175 ], [107.67655 , 133.08873 ], -18 th frmae
[108.69984 , 133.09813 ]-19 th frame of that trajectory point
我现在需要保存NPY为:帧数坐标,........,最后的帧数坐标
例
[5, 95.10257 , 133.91016 ][ 6, 96.17397 , 133.8509 ], [7, 97.26843 , 133.84372 ], [ 8, 98.360085, 133.75041 ], [9, 99.31606 , 133.46017 ], [10, 100.22412 , 133.29907 ], [11, 101.10831 , 133.0922 ], [12, 102.029564, 132.93976 ], [13, 102.92373 , 132.95956 ], [14, 103.90272 , 132.80684 ], [15, 104.84618 , 132.84692 ], [16, 105.69725 , 132.90834 ], [17, 106.740944, 133.00175 ], [18, 107.67655 , 133.08873 ],
[19,108.69984 , 133.09813 ]
这样,我想在NPY文件进行保存。只是我想和框架_num新NPY文件,协调并删除其他数据
EDITED
f=np.zeros((len(tracks),15,3),float)
n_tracks,n_pts,_=tracks['coords'].shape
for i in range(len(tracks)):
get_frame_num=tracks['frame_num'][i]
for j in range((get_frame_num-14),get_frame_num+1):
print(j)
f[:,:,0]=float(j)
f[:,:,1:]=tracks['coords'][:n_tracks,:n_pts-1]
在阵列[0],第一列是0,0,0。该结果后,我想切换到1,2,3在阵列[1],第一列是1,1,1-。这个结果后,我想改变2,3,4为坐标的轨迹,使用嵌套的for循环使得IPython中崩溃
我会尽力来说明如何像你这样的阵列可以使用。
首先,我必须使与化合物D型细胞的阵列。你的一个简化:
In [729]: dt = np.dtype([('frame','i'), ('coord', 'f', (3,2))])
In [730]: arr = np.zeros((5,), dtype=dt)
In [731]: arr['frame'] = np.arange(5)
In [732]: arr['coord'].shape
Out[732]: (5, 3, 2)
In [733]: arr['coord'] = np.arange(30).reshape(5,3,2)
In [734]: arr
Out[734]:
array([(0, [[ 0., 1.], [ 2., 3.], [ 4., 5.]]),
(1, [[ 6., 7.], [ 8., 9.], [10., 11.]]),
(2, [[12., 13.], [14., 15.], [16., 17.]]),
(3, [[18., 19.], [20., 21.], [22., 23.]]),
(4, [[24., 25.], [26., 27.], [28., 29.]])],
dtype=[('frame', '<i4'), ('coord', '<f4', (3, 2))])
arr['frame']
和arr['coord']
会是什么样子,你可以从你的阵列中提取的字段。
我不太清楚你想要加入的帧数和COORDS,但这里有一个猜测。首先要空数组来保存连接的数据。
In [735]: x = np.zeros((5,3,3),float)
而从arr
复制值:
In [736]: x[:,:,0] = arr['frame']
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-736-7270535f17a6> in <module>()
----> 1 x[:,:,0] = arr['frame']
ValueError: could not broadcast input array from shape (5) into shape (5,3)
哎呀。 x[:,:,0]
是(5,3)阵列,而frame
是(5);我需要改变(5)到(5,1),因此它可以被广播到(5,3):
In [737]: x[:,:,0] = arr['frame'][:,None]
In [738]: x[:,:,1:] = arr['coord'] # (5,3,2) both sides
In [739]: x
Out[739]:
array([[[ 0., 0., 1.],
[ 0., 2., 3.],
[ 0., 4., 5.]],
[[ 1., 6., 7.],
[ 1., 8., 9.],
[ 1., 10., 11.]],
[[ 2., 12., 13.],
[ 2., 14., 15.],
[ 2., 16., 17.]],
[[ 3., 18., 19.],
[ 3., 20., 21.],
[ 3., 22., 23.]],
[[ 4., 24., 25.],
[ 4., 26., 27.],
[ 4., 28., 29.]]])
np.save(filename, x)
应该正常工作。 np.save
适用于任何numpy的阵列,即使这个3D之一。无需迭代为您在其他问题的尝试。 np.save
可以处理arr
了。
我还可以通过级联构建x
。但首先我必须向frame
复制到兼容的形状。记住coord
为(5,3,2):
In [744]: f = np.repeat(arr['frame'][:,None],3,axis=1)
In [745]: f.shape
Out[745]: (5, 3)
In [746]: y = np.concatenate((f[:,:,None], arr['coord']), axis=2)
In [747]: y.shape
Out[747]: (5, 3, 3)
In [748]: np.allclose(x,y)
Out[748]: True
`