为什么将浮点列表转换为numpy数组会改变一些变量的值? [英] Why converting a float list into numpy array changes the value of some variables?

查看:219
本文介绍了为什么将浮点列表转换为numpy数组会改变一些变量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  with open('dataset / train / problem.csv','r' )作为p:
raw_x = csv.reader(p)
data_x = []
在raw_x中为ix:
data_x.append([float(i)for i in ix ])
print(data_x)

这将输出以下输出:

  [[217.0,118.0,0.63,755.0,1071.0],[217.0,118.0,0.63,755.0,1071.0],...] 

现在我试图将这个结构转换为一个浮点数组,使我可以使用scikit-学习作为观察输入。但是,当我尝试跟随

  X = np.array(data_x)
print(X)

结果如下:

  2.17000000e + 02 1.18000000e + 02 6.30000000e-01 7.55000000e + 02 
1.07100000e + 03]
...
[2.17000000e + 02 1.18000000e + 02 6.30000000e-01 7.55000000e + 02
1.07100000e + 03]

但十进制值不正确。

一直试图弄清楚为什么会发生这种情况,因为源数组也是浮动的。我曾尝试提供 type = float astype ,但似乎没有任何效果。

谢谢!

解决方案

数组中的值相同。例如, 2.1700000000e + 2 2.17 x 10 ^ 2 217 ,与原始数组相同。



numpy数组使用科学记数法代替标准小数形式。 b

So I have following code snippet:

with open('dataset/train/problem.csv', 'r') as p:
raw_x = csv.reader(p)
data_x = []
for ix in raw_x:
    data_x.append([float(i) for i in ix])
print(data_x)

This prints the following output:

[[217.0, 118.0, 0.63, 755.0, 1071.0], [217.0, 118.0, 0.63, 755.0, 1071.0],...]

Now I am trying to convert this structure into a numpy array of floats so that I can use it with scikit-learn as an observation input. But when I try doing following

X = np.array(data_x)
print(X)

It gives the following result:

[  2.17000000e+02   1.18000000e+02   6.30000000e-01   7.55000000e+02
1.07100000e+03]
...
[  2.17000000e+02   1.18000000e+02   6.30000000e-01   7.55000000e+02
1.07100000e+03]

It's still float but the decimal values are not correct.

Been trying to figure out why this is happening as the source array is also in floats. I have tried providing type=float and astype as well but nothing seems to work.

Thanks!

解决方案

The values in the array are the same. For example, 2.1700000000e+2 is 2.17 x 10^2, or 217, which is the same as in your original array.

The numpy array uses scientific notation instead of standard decimal form.

这篇关于为什么将浮点列表转换为numpy数组会改变一些变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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