- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当使用 Numba 的 @jit
和 Numpy 的 float32
数据类型时,我得到了?截断?问题。这主要是噪音,因为它远远超出了我关心的小数位数 - 大约第 7 或第 8 位 - 但知道发生了什么以及我是否可以修复它仍然是件好事。
顺便说一句,我必须使用 float32
数据类型来节省内存!
这是我用作测试的代码:
import numpy as np
from test_numba import test_numba
np.random.seed(seed=1774);
number = 150;
inArray = np.round(np.float32((np.random.rand(number)-.5)*2),4); #set up a float32 with 4 decimal places
numbaGet = test_numba(inArray); #run it through
print("Get:\t"+str(numbaGet)+" Type: "+str(type(numbaGet)));
print("Want:\t"+str(np.mean(inArray))+" Type: "+str(type(np.mean(inArray)))); #compare to expected
结合以下功能
import numpy as np
from numba import jit #, float32
@jit(nopython=True) #nopython=True, nogil=True, parallel=True, cache=True , nogil=True, parallel=True #float32(float32),
def test_numba(inArray):
#outArray = np.float32(np.mean(inArray)); #forcing float32 did not change it
outArray = np.mean(inArray);
return outArray;
输出结果是:
Get: 0.0982406809926033 Type: <class 'float'>
Want: 0.09824067 Type: <class 'numpy.float32'>
这似乎表明 Numba 正在使它成为 Python float
类(据我所知是 float64
)并进行数学运算,然后不知何故失去了精度。
如果我切换到 float64
,差异会大大减小。
Get: 0.09824066666666667 Type: <class 'float'>
Want: 0.09824066666666668 Type: <class 'numpy.float64'>
不确定我做错了什么。同样,在我的情况下,这是一个可以忽略的问题(从小数点后 4 位开始),但仍然想知道为什么!
最佳答案
原因是,numba 不使用 np.mean
而是用/推出 its own version 代替它:
def array_mean_impl(arr):
# Can't use the naive `arr.sum() / arr.size`, as it would return
# a wrong result on integer sum overflow.
c = zero
for v in np.nditer(arr):
c += v.item()
return c / arr.size
前段时间我给了an answer to a very similar question关于 numpy.mean
和 pandas.mean
之间的区别(使用 bottleneck
)。所以那里所说的一切也适用于这里,请查看它以获取更多详细信息,简而言之:
numba
使用的朴素求和误差为 O(n)
,其中 n
是求和数。O(log(n))
更精确。float32
的区别很明显,float64
的区别不太明显,尽管同样的问题仍然存在。关于python - 使用 Numba 的 @jit 导致数学与 Python 中使用的 Numpy 的 float32 不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54262281/
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Recreating a Dictionary from an IEnumerable 在 Dictiona
是否可以使用命令行版本的 ImageMagick 修剪图像(比如带有 alpha 的 PNG),使输出图像的宽度和高度都是偶数(不是奇数)? 准确地说,应该先修剪输出图像,然后用透明像素填充。我需要这
我有一个订单的Map,可以由许多不同的线程访问。我想控制访问,所以考虑以下简单的数据结构+包装器。 public interface OrderContainer { boolean cont
我有以下代码,现在只是 div 中的一个 Logo ,但我正在尝试添加一些导航单元格,稍后我将对其进行样式设置。问题是,我似乎无法让它们与(除此之外) Logo “一致”,它们总是下降到下一行。我做错
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 9 年前。 Improve this qu
有没有办法将种子值传递给 d3-cloud 或其他基于 javascript 的标签云,以使其在页面加载之间保持一致? 我们的客户希望使用标签云作为导航/发现辅助工具,但由于 d3-cloud 会在每
我有一条由用户使用 D3.js 绘制的路径。 我想在我的用户绘制路径上定义一个破折号数组,但是,随着它改变其形状和长度,破折号的行为不一致并且间隙在移动并变得越来越小。 这是一个代码笔: https:
只是为了研究UINavigationBar和UIStatusBar的UI,我把Navigation Bar Style改成了Black,并且取消勾选Bar visibility,即Shows Navi
我最近在我的家用机器 (OSX 10.9) 和我的远程服务器 (Ubuntu 12.04 64 位) 上安装了 unison。 我在这两个地方都安装了 2.40.102 版本。我在我的 Mac 上使用
我正在使用 migrate 创建 SQL 数据库模式并用初始数据填充它。后来使用 SQLAlchemy 来处理这个数据库。 我如何测试我的 SQLAlchemy 模型是否与 migrate 生成的真实
道歉对这一切来说还是新鲜事。我正在创建一个网页,并在两个单独的 div 中将图像和文本并排放置。我已经设法将它们放在页面上我想要的位置,但是当我调整页面大小时,文本会调整大小,但图像不会。我希望文本底
在翻阅Cassandra和HBase的阅读资料时,我发现Cassandra并不一致,但HBase是一致的。没有找到任何合适的阅读 Material 。 有人可以提供有关此主题的任何博客/文章吗? 最佳
我需要计算 MacOS 中文件夹的大小。该尺寸值必须与 Finder 一致。我尝试了几种方法来做到这一点。但结果总是与Finder不同。 以下方法是我尝试过的。 typedef struct{
问:我可以使用 C++ 中的任何编译时机制来自动验证模板类方法集是否从类特化到特化相匹配? 示例:假设我想要一个类接口(interface),它根据模板值专门化具有非常不同的行为: // forwar
我想使用 SelectKBest 选择前 K 个特征并运行 GaussianNB: selection = SelectKBest(mutual_info_classif, k=300) data_t
我想要一个位于页面中央的 div,其中包含一行(两个单词)的 h1 文本,并且该文本与 div 的长度对齐;意思是,字母留出空间(同时保持它们的大小)以占据 div 的整个宽度,并且不要超出 div。
我试图更新我的服务器,所以我通过 ssh 运行以下命令: sudo do-release-upgrade 我收到以下错误: Errors were encountered while processi
我想验证单应矩阵会给出好的结果,而这个 this answer 有答案 - 但是,我不知道如何实现答案。 那么谁能推荐我如何使用 OpenCV 计算 SVD 并验证第一个奇异值与最后一个奇异值的比率是
我最近更新到 cocoapods 0.36 并对内部规范做了一些更改,现在 podspec 不再有效。我用 0.35 验证了此规范的先前版本 (0.3.8),但使用 0.36 失败。很明显 cocoa
我有两个并排设置的 TableView ,我需要它们同时滚动。因此,当您滚动一个时,另一个也会同时滚动。 我进行了一些搜索,但找不到任何信息,但我认为这一定是有可能的。 我的 TableView 都连
我是一名优秀的程序员,十分优秀!