数组中的数值在转换为NumPy数组时发生变化。

huangapple go评论123阅读模式
英文:

values in the array change when turned to numpy array

问题

这种情况发生是因为在NumPy数组的默认输出格式中,数字以科学计数法显示。要停止这种情况,你可以使用NumPy的set_printoptions函数来更改打印选项,将科学计数法关闭。以下是如何做到这一点的示例代码:

import numpy as np

# 设置NumPy打印选项,关闭科学计数法
np.set_printoptions(suppress=True)

# 将数据从pandas DataFrame移动到NumPy数组
pylist = train_df.iloc[1:, 3:].values.tolist()
train_X = np.array(pylist)

# 打印NumPy数组
print(train_X[0])

这将关闭科学计数法,使你的NumPy数组以普通的浮点数形式打印出来,就像你的第一个打印结果一样。

英文:

I have data stored in a pandas DataFrame
that I move to a numpy array using the following code

# used to be train_X = np.array(train_df.iloc[1:,3:].values.tolist())
# but was split for me to find he source of change  
pylist = train_df.iloc[1:,3:].values.tolist()
print(pylist[0])
train_X = np.array(pylist)
print(train_X[0])

the first print returns :

[0.0, 0.0, 0.0, 0.0, 1.0, 504.0, 0.0, 2.0, 8.0, 0.0, 0.0, 0.0, 0.0, 2.0, 8.0, 0.0, 189.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 85143.0, 57219.0, 62511.267857142804, 2649.26669430866]

the second print after the I move it to a Numpy array returns this

[0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
 1.00000000e+00 5.04000000e+02 0.00000000e+00 2.00000000e+00
 8.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
 0.00000000e+00 2.00000000e+00 8.00000000e+00 0.00000000e+00
 1.89000000e+02 0.00000000e+00 0.00000000e+00 0.00000000e+00
 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
 0.00000000e+00 0.00000000e+00 8.51430000e+04 5.72190000e+04
 6.25112679e+04 2.64926669e+03]

why does this happen ? and how do I stop it

答案1

得分: 1

如评论中所述,NumPy将数据表示为指数表示法。如果您想更改打印方式,可以这样做:

import numpy as np

np.set_printoptions(precision=2)
pylist = train_df.iloc[1:,3:].values.tolist()
print(pylist[0])
train_X = np.array(pylist)
print(train_X[0])
英文:

As mentioned in the comments, NumPy represents the data to exponential notation. If you would like to change the way it's printed, you can do:

import numpy as np

np.set_printoptions(precision=2)
pylist = train_df.iloc[1:,3:].values.tolist()
print(pylist[0])
train_X = np.array(pylist)
print(train_X[0])

答案2

得分: 0

这是因为 numpy 提供了与 pandas 相比的数值值的完整表示。您可以使用方法 np.set_print_options(precision=2)

英文:

This happens because numpy provides the full notation of a numeric value as compared to pandas. You can use the method np.setprint_oprtions(precision=2)

huangapple
  • 本文由 发表于 2020年1月6日 22:45:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定