【问题标题】:numpy append changes int to float and adds zerosnumpy append 将 int 更改为 float 并添加零
【发布时间】:2016-12-14 15:54:23
【问题描述】:

我需要将范围转换为连续数字。范围以整数为单位,结果应该相同。这是我目前所拥有的:

将 numpy 导入为 np

mydata = np.array (
[49123400, 49123499],
[33554333, 33554337])

numbers_list = np.empty((0))
base_dir = "/foo.csv"

for x in mydata:
    numbers = np.arange(x[0], x[1]+1)
    numbers_list = np.append(numbers_list, numbers, axis=0)
np.savetxt(base_dir, numbers_list, delimiter=";")

我希望看到的是这样的列表:

49123400,
49123401,
49123402,...
49123499,
33554333,
33554334,...
33554399

但我得到的是:

4.912340000000000000e+11 and so on...

我哪里错了?为什么我在进行追加时会从 int 变为 float?

【问题讨论】:

  • mydatanumbers_listnumbers 的数据类型是什么?
  • mydata: int64, numbers: int64, numbers_list变成float64
  • 注意:base_dir 与 basedir。当我运行你的代码时输出是正确的,在 4.91E+07 范围内。
  • 我看到你编辑了错字;你确定错字不是造成混乱的原因吗?您查看的文件是否正确?
  • 是的,我确信这不是问题所在。在原版中,还可以。我不得不直接输入代码,因为我无法复制/粘贴它并打错字。对不起。

标签: python numpy


【解决方案1】:

要学习的一个重要教训是,您应该始终为您的问题选择正确的数据结构。在大多数情况下,如果您想追加/连接,那么 是错误的选择,除非您可以简单地设置最终数组(及其最终形状)并通过设置切片来更改它。

在这种情况下,显而易见的选择是使用普通的 listrange

mydata = [[49123400, 49123499],
          [33554333, 33554337]]

mynewdata = []
for sublist in mydata:
    mynewdata.extend(range(sublist[0], sublist[1]+1))

>>> mynewdata
  [49123400, 49123401, 49123402, 49123403, 49123404, 49123405,
   49123406, 49123407, 49123408, 49123409, 49123410, 49123411,
   49123412, 49123413, 49123414, 49123415, 49123416, 49123417,
   49123418, 49123419, 49123420, 49123421, 49123422, 49123423,
   49123424, 49123425, 49123426, 49123427, 49123428, 49123429,
   49123430, 49123431, 49123432, 49123433, 49123434, 49123435,
   49123436, 49123437, 49123438, 49123439, 49123440, 49123441,
   49123442, 49123443, 49123444, 49123445, 49123446, 49123447,
   49123448, 49123449, 49123450, 49123451, 49123452, 49123453,
   49123454, 49123455, 49123456, 49123457, 49123458, 49123459,
   49123460, 49123461, 49123462, 49123463, 49123464, 49123465,
   49123466, 49123467, 49123468, 49123469, 49123470, 49123471,
   49123472, 49123473, 49123474, 49123475, 49123476, 49123477,
   49123478, 49123479, 49123480, 49123481, 49123482, 49123483,
   49123484, 49123485, 49123486, 49123487, 49123488, 49123489,
   49123490, 49123491, 49123492, 49123493, 49123494, 49123495,
   49123496, 49123497, 49123498, 49123499, 33554333, 33554334,
   33554335, 33554336, 33554337]

这可以简单地转换为numpy.array

>>> np.array(mynewdata)
array([49123400, 49123401, 49123402, 49123403, 49123404, 49123405,
       49123406, 49123407, 49123408, 49123409, 49123410, 49123411,
       49123412, 49123413, 49123414, 49123415, 49123416, 49123417,
       49123418, 49123419, 49123420, 49123421, 49123422, 49123423,
       49123424, 49123425, 49123426, 49123427, 49123428, 49123429,
       49123430, 49123431, 49123432, 49123433, 49123434, 49123435,
       49123436, 49123437, 49123438, 49123439, 49123440, 49123441,
       49123442, 49123443, 49123444, 49123445, 49123446, 49123447,
       49123448, 49123449, 49123450, 49123451, 49123452, 49123453,
       49123454, 49123455, 49123456, 49123457, 49123458, 49123459,
       49123460, 49123461, 49123462, 49123463, 49123464, 49123465,
       49123466, 49123467, 49123468, 49123469, 49123470, 49123471,
       49123472, 49123473, 49123474, 49123475, 49123476, 49123477,
       49123478, 49123479, 49123480, 49123481, 49123482, 49123483,
       49123484, 49123485, 49123486, 49123487, 49123488, 49123489,
       49123490, 49123491, 49123492, 49123493, 49123494, 49123495,
       49123496, 49123497, 49123498, 49123499, 33554333, 33554334,
       33554335, 33554336, 33554337])

甚至直接写入文件而不用关心数组:

with open('yourfile', 'w') as file:
    file.write(str(mynewdata).replace(',', ';'))

最后是关于为什么将整数转换为 floats 的说明:

>>> np.empty((0))
array([], dtype=float64)

np.empty 创建一个浮点数组,因此追加/连接将始终生成float 数组。如果你想要一个整数数组,请使用np.empty(0, int)

>>> np.empty(0, int)
array([], dtype=int64)

【讨论】:

  • 嗨,这真的很有帮助。您不仅提供了解决方案,而且还解释了问题所在。我在最初的帖子中发现了另一个错误:mydata[[xxxxx]] 实际上必须是 mydata([xxx])。由于我必须在 Excel 中转发结果,它有助于将替换部分更改为 replace(',', ';\n'),因此我将它们很好地列出。德克萨斯州。你的解决方案很多。
【解决方案2】:

在这种情况下,在迭代会话中逐步完成它会有所帮助,并在每个步骤中查看 shapedtype

In [254]: mydata = np.array( [
     ...: [49123400, 49123499],
     ...: [33554333, 33554337]])
In [255]: mydata
Out[255]: 
array([[49123400, 49123499],
       [33554333, 33554337]])
In [256]: mydata.shape
Out[256]: (2, 2)
In [257]: mydata.dtype
Out[257]: dtype('int32')
In [258]: numbers_list = np.empty((0))
In [259]: numbers_list
Out[259]: array([], dtype=float64)

请注意,numbers_list 是一个浮点数组。考虑为empty 提供dtype

In [260]: x=mydata[0]
In [261]: numbers = np.arange(x[0],x[1]+1)
In [262]: numbers.dtype
Out[262]: dtype('int32')
In [263]: numbers.shape
Out[263]: (100,)
In [264]: numbers_list = np.append(numbers_list, numbers, axis=0)
In [265]: numbers_list.shape
Out[265]: (100,)
In [266]: numbers_list.dtype
Out[266]: dtype('float64')

连接这两个数组后,结果的dtype为numbers_list

因此,更改 empty dtype 应该保留 int dtype。

我一直在反对np.append。这是其滥用的另一个例子。它只是np.concatenate 的一种形式,通常不能很好地替代列表追加

我建议建立一个列表并使用一个连接

In [267]: numbers_list = [np.arange(x[0],x[1]+1) for x in mydata]
In [268]: len(numbers_list)
Out[268]: 2
In [269]: np.concatenate(numbers_list)
Out[269]: 
array([49123400, 49123401, 49123402, 49123403, 49123404, 49123405,
       49123406, 49123407, 49123408, 49123409, 49123410, 49123411,
       49123412, 49123413, 49123414, 49123415, 49123416, 49123417,
       49123418, 49123419, 49123420, 49123421, 49123422, 49123423,
       49123424, 49123425, 49123426, 49123427, 49123428, 49123429,
  ...
       49123496, 49123497, 49123498, 49123499, 33554333, 33554334,
       33554335, 33554336, 33554337])
In [270]: _.shape
Out[270]: (105,)

由于您使用savetxt 来写入数字,请查看它的fmt 参数。默认为科学记数法。

使用正确的fmt,您将获得整数:

In [272]: arr=np.concatenate(numbers_list)
In [273]: np.savetxt('test.txt',arr,fmt='%d',delimiter=',')
In [274]: cat test.txt
49123400
49123401
49123402
49123403
49123404

【讨论】:

    【解决方案3】:

    我在将列附加到 numpy 数组时遇到了同样的问题。我正在使用np.arange() 函数创建一个包含一列的示例数组,然后我将列附加到它,但数据变得混乱,如您所见:

    [[  0.00000000e+00  -1.56000000e+00]
    [  1.00000000e+00   2.43000000e+00]
    [  2.00000000e+00  -9.40000000e-01]
    ..., 
    [  4.99700000e+03  -1.99000000e+00]
    [  4.99800000e+03   4.10000000e-01]
    [  4.99900000e+03  -7.00000000e-02]]
    

    即使通过确保 dtypes 的相等性,问题也没有解决,但最终通过使用 np.zeros() 而不是 np.arange() 得到解决。

    【讨论】:

      猜你喜欢
      • 2015-07-31
      • 2019-01-07
      • 2021-03-06
      • 1970-01-01
      • 2013-03-11
      • 2023-01-28
      • 2014-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多