蟒蛇 & numpy:强制矩阵包含已知范围从 x 到 y 的值?
我使用 numpy 来构建数学代码的原型。我的数学仅包含我执行矩阵算术(乘法、numpy 中的点函数)的概率。据我所知,所有值的范围都从 0 到 1,我想知道我是否可以强制 numpy 在 32/64 位上编码值(节省内存或享受更高的精度),但范围上限固定为 1?
try1 = numpy.array([1.0,0.2564654646546],dtype='f16')
是否可以强制 dtype 的范围从 x 到 y,并且每个值具有相同的内存量?
I use numpy to prototype a mathematical code. My mathematics contain only probabilities on which i perform matrix arithmetics (multiplication, dot function in numpy). As I know that all values range from 0 to 1, I wonder if I can force numpy to code values (saving memory or enjoy more precision) on 32/64bit but ranging with an upper boundary fixed at 1?
try1 = numpy.array([1.0,0.2564654646546],dtype='f16')
Can dtype be forced to range from x to y with a same amount of memory per value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,numpy 数组不支持定点运算,而且我还没有听说有任何添加它的计划。如果您有兴趣使用这些东西,您可以查看 matlab 的 fixed-pt 工具箱,或者,如果您真的喜欢数学,您可以使用整数数据类型并跟踪“点”来编写自己的数学。
浮点的工作方式已经非常简洁了,我不确定仅仅知道数字在 [0,1] 中,你就能获得大量的每位精度。浮点数类似于科学记数法,增加位数主要是为您提供更多的“有效数字”,而不是(仅仅)更大的数字范围。
As far as I know, numpy arrays don't support fixed point arithmetic and I haven't heard of any plans to add it. If you are interested in playing with that stuff, you could check out matlab's fixed-pt toolbox, or if you really love mathematics you can cook your own using integer datatypes and keeping track of the 'point'.
The way floating point works is already pretty neat though and I'm not sure you would gain a heap of precision per bit just with the knowledge that numbers are in [0,1]. Floating point is similar to scientific notation, increasing the number of bits mainly gives you more "significant digits" rather than (just) a larger range of numbers.
我想现在你可以通过以下方式实现这一目标:
I suppose nowadays you can acchieve this with: