as i try to sort a numpy array i have to change the dtype because i need to sort the data based on a column. But as i change the type the array transforms itself [columns to rows] and duplicates. I am using .astype
you can see below:
>>> edist
array([[2.50000000e+00, 1.45000000e+02, 4.46500000e+03, 1.41958256e+01],
[2.00000000e+00, 1.45500000e+02, 4.46500000e+03, 1.51561499e+01],
[1.50000000e+00, 1.46000000e+02, 4.46500000e+03, 1.60814095e+01],
...,
[1.24828883e+02, 2.34000000e+02, 4.55500000e+03, 1.18762398e+01],
[1.25175876e+02, 2.34500000e+02, 4.55500000e+03, 6.60787582e+00],
[1.25523902e+02, 2.35000000e+02, 4.55500000e+03, 1.16466343e+00]])
>>> edist.astype(typ)
array([[(2.50000000e+00, 2.50000000e+00, 2.50000000e+00, 2.50000000e+00),
(1.45000000e+02, 1.45000000e+02, 1.45000000e+02, 1.45000000e+02),
(4.46500000e+03, 4.46500000e+03, 4.46500000e+03, 4.46500000e+03),
(1.41958256e+01, 1.41958256e+01, 1.41958256e+01, 1.41958256e+01)],
[(2.00000000e+00, 2.00000000e+00, 2.00000000e+00, 2.00000000e+00),
(1.45500000e+02, 1.45500000e+02, 1.45500000e+02, 1.45500000e+02),
(4.46500000e+03, 4.46500000e+03, 4.46500000e+03, 4.46500000e+03),
(1.51561499e+01, 1.51561499e+01, 1.51561499e+01, 1.51561499e+01)],
[(1.50000000e+00, 1.50000000e+00, 1.50000000e+00, 1.50000000e+00),
(1.46000000e+02, 1.46000000e+02, 1.46000000e+02, 1.46000000e+02),
(4.46500000e+03, 4.46500000e+03, 4.46500000e+03, 4.46500000e+03),
(1.60814095e+01, 1.60814095e+01, 1.60814095e+01, 1.60814095e+01)],
...,
[(1.24828883e+02, 1.24828883e+02, 1.24828883e+02, 1.24828883e+02),
(2.34000000e+02, 2.34000000e+02, 2.34000000e+02, 2.34000000e+02),
(4.55500000e+03, 4.55500000e+03, 4.55500000e+03, 4.55500000e+03),
(1.18762398e+01, 1.18762398e+01, 1.18762398e+01, 1.18762398e+01)],
[(1.25175876e+02, 1.25175876e+02, 1.25175876e+02, 1.25175876e+02),
(2.34500000e+02, 2.34500000e+02, 2.34500000e+02, 2.34500000e+02),
(4.55500000e+03, 4.55500000e+03, 4.55500000e+03, 4.55500000e+03),
(6.60787582e+00, 6.60787582e+00, 6.60787582e+00, 6.60787582e+00)],
[(1.25523902e+02, 1.25523902e+02, 1.25523902e+02, 1.25523902e+02),
(2.35000000e+02, 2.35000000e+02, 2.35000000e+02, 2.35000000e+02),
(4.55500000e+03, 4.55500000e+03, 4.55500000e+03, 4.55500000e+03),
(1.16466343e+00, 1.16466343e+00, 1.16466343e+00, 1.16466343e+00)]],
dtype=[('Eucli Dist', '<f8'), ('x', '<f8'), ('y', '<f8'), ('Magn', '<f8')])
typ is the new dtype
typ = [('Eucli Dist', float), ('x', float), ('y', float), ('Magn', float)]
UPDATE So i tried the following:
tes = [tuple(i) for i in edist]
np.reshape(np.array(tes, dtype=typ), (len(tes),1))
resulting in :
array([[( 2.5 , 145. , 4465., 14.19582558)],
[( 2. , 145.5, 4465., 15.15614986)],
[( 1.5 , 146. , 4465., 16.08140945)],
...,
[(124.82888288, 234. , 4555., 11.87623978)],
[(125.17587627, 234.5, 4555., 6.60787582)],
[(125.52390211, 235. , 4555., 1.16466343)]],
dtype=[('Eucli Dist', '<f8'), ('x', '<f8'), ('y', '<f8'), ('Magn', '<f8')])
but i want it to be like:
[[..., ..., ..., ...],
[..., ..., ..., ...]]
(calling the values is the same but...)