python将列表转换为numpy数组,同时保留数字格式 [英] python convert list to numpy array while preserving the number formats

查看:850
本文介绍了python将列表转换为numpy数组,同时保留数字格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是将我的数据转换为numpy数组,同时将数字格式保留在原始列表中,清晰明了.

My goal is to convert my data into numpy array while preserving the number formats in the original list, clear and proper.


例如, 这是我的列表格式数据:


for example, this is my 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]]

当我将其转换为numpy数组时,

when i do convert it to numpy array,

data = np.asarray(data)

我得到数学符号e,如何在输出数组中保留相同的格式?

i get mathematical notation e, how can I conserve the same format in my 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)

,但是当我将部分数据传递给另一个变量然后在其中查找时,我仍然得到不同的数字,我发现小数点已经改变了!为什么在内部更改小数,为什么不能仅保留小数呢?

推荐答案

从嵌套列表创建简单数组:

Simple array creation from the nested list:

In [133]: data = np.array(alist)
In [136]: data.shape
Out[136]: (8, 4)
In [137]: data.dtype
Out[137]: dtype('float64')

这是一个二维数组,8个行",4个列";所有元素都存储为float.

This is a 2d array, 8 'rows', 4 'columns'; all elements are stored as float.

可以将列表加载到结构化数组中,该数组定义为混合使用float和integer字段.请注意,为此负载,我必须将行"转换为元组.

The list can be loaded into a structured array, that is defined to have a mix of float and integer fields. Note that I have to convert the 'rows' to tuples for this 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) for 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 by 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)

将这些值与2d浮点数组中的单列显示进行比较:

Compare those values with the display of single columns from the 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.])

当混合使用浮点数,整数,字符串或其他dtypes时,使用结构化数组更有意义.

The use of a structured array makes more sense when there's a mix of floats, int, strings or other dtypes.

但是要备份一下-纯浮动版本有什么问题?为什么保留2列的整数身份很重要?

But to back up a bit - what is wrong with the pure float version? Why is important to retain the integer identity of 2 columns?

这篇关于python将列表转换为numpy数组,同时保留数字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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