numpy.vstack丢失精度float16

发布于 2025-02-04 05:27:06 字数 1133 浏览 1 评论 0原文

我正在尝试使用仅作为精确数字的数字进行线性回归的精确计算。没有numpy,它的工作正常,但是对于大量项目,numpy的性能更好,这就是为什么我需要使用numpy。 但是问题是,当我为X轴构建矩阵时,我会看到Bellow时失去了小数的精度。

我该如何修复?我的意思是,对于矩阵变量,仅返回一个数字作为精确的数字。

import numpy as np
import pandas as pd

dataset = [[17.3,71.7],[19.3,48.3],[19.5,88.3]]
df = pd.DataFrame({
    'force': [item[0] for item in dataset],
    'push_up':[item[1] for item in dataset]
})

df_x = np.array([item for item in df['force']],dtype=np.float16)
df_y = np.array([item for item in df['push_up']],dtype=np.float16)

print([np.round(item, decimals=1) for item in df['force']])
#check precision

#here is the issue! the return lose my 1 decimal point precision. 
# notice !No matter if I use this printed array above.
# also tried using this array construction to reconvert to 1 decimal precision but no success
#print( [np.float16(np.format_float_positional(item, precision=1)) for item in df['force']] )


matrix = np.vstack([df_x, np.ones(len(df_x))]).T
print(matrix[0][0])
#this print "17.296875" that is totally different from 17.3
#print(matrix[2][0]) #uncomment this to see that the half precision is not lost at all

I'm trying to perform a precise calculation for linear regression using only one digit as precise number. without numpy it works just fine but numpy performs better for large amount of items that's why I need use numpy.
But the issue is that when I build the matrix for the X axis I lose my decimal precision as you can see bellow.

How can I fix it? I mean, to the matrix variable return only one digit as precise number.

import numpy as np
import pandas as pd

dataset = [[17.3,71.7],[19.3,48.3],[19.5,88.3]]
df = pd.DataFrame({
    'force': [item[0] for item in dataset],
    'push_up':[item[1] for item in dataset]
})

df_x = np.array([item for item in df['force']],dtype=np.float16)
df_y = np.array([item for item in df['push_up']],dtype=np.float16)

print([np.round(item, decimals=1) for item in df['force']])
#check precision

#here is the issue! the return lose my 1 decimal point precision. 
# notice !No matter if I use this printed array above.
# also tried using this array construction to reconvert to 1 decimal precision but no success
#print( [np.float16(np.format_float_positional(item, precision=1)) for item in df['force']] )


matrix = np.vstack([df_x, np.ones(len(df_x))]).T
print(matrix[0][0])
#this print "17.296875" that is totally different from 17.3
#print(matrix[2][0]) #uncomment this to see that the half precision is not lost at all

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的旅程 2025-02-11 05:27:07

要控制dtypecondenate(以及所有'stack')中,参数必须匹配:

In [274]: np.vstack([np.array([1,2,3], 'float16'), np.ones(3,'float16')])
Out[274]: 
array([[1., 2., 3.],
       [1., 1., 1.]], dtype=float16)

nones的默认dtype is float64 < /code>:

In [275]: np.vstack([np.array([1,2,3], 'float16'), np.ones(3)])
Out[275]: 
array([[1., 2., 3.],
       [1., 1., 1.]])

In [276]: _.dtype
Out[276]: dtype('float64')

但是如注释中所述,使用float16在表面上是一个圆形。

In [278]: np.vstack([np.array([1.234235,2.9999,3], 'float16'), np.ones(3,'float16')])
Out[278]: 
array([[1.234, 3.   , 3.   ],
       [1.   , 1.   , 1.   ]], dtype=float16)

转置不会更改值或dtype。

To control dtype in concatenate (and all 'stack'), the arguments have to match:

In [274]: np.vstack([np.array([1,2,3], 'float16'), np.ones(3,'float16')])
Out[274]: 
array([[1., 2., 3.],
       [1., 1., 1.]], dtype=float16)

Default dtype for ones is float64:

In [275]: np.vstack([np.array([1,2,3], 'float16'), np.ones(3)])
Out[275]: 
array([[1., 2., 3.],
       [1., 1., 1.]])

In [276]: _.dtype
Out[276]: dtype('float64')

But as noted in the comments, use of float16 is only superficially a rounding.

In [278]: np.vstack([np.array([1.234235,2.9999,3], 'float16'), np.ones(3,'float16')])
Out[278]: 
array([[1.234, 3.   , 3.   ],
       [1.   , 1.   , 1.   ]], dtype=float16)

The transpose does not change values or dtype.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文