python convert list to numpy array while preserving the number formats -
my goal convert data numpy array while preserving number formats in original list, clear , proper.
for example, data in list format:
[[24.589888563639835, 13.899891781550952, 4478597, -1], [26.822224204095697, 14.670531752529088, 4644503, -1], [51.450405486761866, 54.770422572665254, 5570870, 0], [44.979065080591504, 54.998835550128852, 6500333, 0], [44.866399274880663, 55.757240813761534, 6513301, 0], [45.535380533604247, 57.790074517001365, 6593281, 0], [44.850372630818214, 54.720574554485822, 6605483, 0], [51.32738085400576, 55.118344981379266, 6641841, 0]]
when convert numpy array,
data = np.asarray(data)
i mathematical notation e
, how can conserve same format in output array?
[[ 2.45898886e+01 1.38998918e+01 4.47859700e+06 -1.00000000e+00] [ 2.68222242e+01 1.46705318e+01 4.64450300e+06 -1.00000000e+00] [ 5.14504055e+01 5.47704226e+01 5.57087000e+06 0.00000000e+00] [ 4.49790651e+01 5.49988356e+01 6.50033300e+06 0.00000000e+00] [ 4.48663993e+01 5.57572408e+01 6.51330100e+06 0.00000000e+00] [ 4.55353805e+01 5.77900745e+01 6.59328100e+06 0.00000000e+00] [ 4.48503726e+01 5.47205746e+01 6.60548300e+06 0.00000000e+00] [ 5.13273809e+01 5.51183450e+01 6.64184100e+06 0.00000000e+00]]
update:
i did :
np.set_printoptions(precision=6,suppress=true)
but still different numbers when pass part of data variable , inside it, , see decimals have changed! why internally changing decimals, why can't hold them is?
simple array creation nested list:
in [133]: data = np.array(alist) in [136]: data.shape out[136]: (8, 4) in [137]: data.dtype out[137]: dtype('float64')
this 2d array, 8 'rows', 4 'columns'; elements stored float.
the list can loaded structured array, defined have mix of float , integer fields. note have convert 'rows' tuples load.
in [139]: dt = np.dtype('f,f,i,i') in [140]: dt out[140]: dtype([('f0', '<f4'), ('f1', '<f4'), ('f2', '<i4'), ('f3', '<i4')]) in [141]: data = np.array([tuple(row) row in alist], dtype=dt) in [142]: data.shape out[142]: (8,) in [143]: data out[143]: array([( 24.58988762, 13.89989185, 4478597, -1), ( 26.82222366, 14.67053223, 4644503, -1), ( 51.45040512, 54.77042389, 5570870, 0), ( 44.97906494, 54.99883652, 6500333, 0), ( 44.86639786, 55.7572403 , 6513301, 0), ( 45.53538132, 57.79007339, 6593281, 0), ( 44.85037231, 54.72057343, 6605483, 0), ( 51.32738113, 55.11834335, 6641841, 0)], dtype=[('f0', '<f4'), ('f1', '<f4'), ('f2', '<i4'), ('f3', '<i4')])
you access fields name, not column number:
in [144]: data['f0'] out[144]: array([ 24.58988762, 26.82222366, 51.45040512, 44.97906494, 44.86639786, 45.53538132, 44.85037231, 51.32738113], dtype=float32) in [145]: data['f3'] out[145]: array([-1, -1, 0, 0, 0, 0, 0, 0], dtype=int32)
compare values display of single columns 2d float array:
in [146]: dataf = np.array(alist) in [147]: dataf[:,0] out[147]: array([ 24.58988856, 26.8222242 , 51.45040549, 44.97906508, 44.86639927, 45.53538053, 44.85037263, 51.32738085]) in [148]: dataf[:,3] out[148]: array([-1., -1., 0., 0., 0., 0., 0., 0.])
the use of structured array makes more sense when there's mix of floats, int, strings or other dtypes.
but bit - wrong pure float version? why important retain integer identity of 2 columns?
Comments
Post a Comment