- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在 Python 的 numpy 中发现了一些有趣的东西。 ma.average
比 arr.mean
慢很多(arr 是一个数组)
>>> arr = np.full((3, 3), -9999, dtype=float)
array([[-9999., -9999., -9999.],
[-9999., -9999., -9999.],
[-9999., -9999., -9999.]])
%timeit np.ma.average(arr, axis=0)
The slowest run took 49.32 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 191 µs per loop
%timeit arr.mean(axis=0)
The slowest run took 6.63 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 7.41 µs per loop
随机数
arr = np.random.random((3,3))
%timeit arr.mean(axis=0)
The slowest run took 6.17 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 7.78 µs per loop
%timeit np.ma.average(arr, axis=0)
1000 loops, best of 3: 186 µs per loop
--> 慢了将近 24 倍。
numpy.ma.average(a, axis=None, weights=None, returned=False)
Return the
weighted
average of array over the given axis.
numpy.mean(a, axis=None, dtype=None, out=None, keepdims)
Compute the arithmetic mean along the specified axis.
为什么 ma.average
比 arr.mean
慢那么多?在数学上它们是相同的(如果我错了请纠正我)。我的猜测是它与 ma.average
上的加权选项有关,但如果没有传递权重,不应该有回退吗?
最佳答案
找出为什么速度变慢的一个好方法是分析它。我将使用第三方库 line_profiler
和 IPython 命令 %lprun
(参见示例 this blog):
%load_ext line_profiler
import numpy as np
arr = np.full((3, 3), -9999, dtype=float)
%lprun -f np.ma.average np.ma.average(arr, axis=0)
Line # Hits Time Per Hit % Time Line Contents
==============================================================
519 def average(a, axis=None, weights=None, returned=False):
...
570 1 1810 1810.0 30.5 a = asarray(a)
571 1 15 15.0 0.3 m = getmask(a)
572
573 # inspired by 'average' in numpy/lib/function_base.py
574
575 1 5 5.0 0.1 if weights is None:
576 1 3500 3500.0 59.0 avg = a.mean(axis)
577 1 591 591.0 10.0 scl = avg.dtype.type(a.count(axis))
578 else:
...
608
609 1 7 7.0 0.1 if returned:
610 if scl.shape != avg.shape:
611 scl = np.broadcast_to(scl, avg.shape).copy()
612 return avg, scl
613 else:
614 1 5 5.0 0.1 return avg
我删除了一些不相关的行。
所以实际上有 30% 的时间花在 np.ma.asarray
上(arr.mean
不需要这样做!)。
然而,如果您使用更大的数组,相对时间会发生巨大变化:
arr = np.full((1000, 1000), -9999, dtype=float)
%lprun -f np.ma.average np.ma.average(arr, axis=0)
Line # Hits Time Per Hit % Time Line Contents
==============================================================
519 def average(a, axis=None, weights=None, returned=False):
...
570 1 609 609.0 7.6 a = asarray(a)
571 1 14 14.0 0.2 m = getmask(a)
572
573 # inspired by 'average' in numpy/lib/function_base.py
574
575 1 7 7.0 0.1 if weights is None:
576 1 6924 6924.0 86.9 avg = a.mean(axis)
577 1 404 404.0 5.1 scl = avg.dtype.type(a.count(axis))
578 else:
...
609 1 6 6.0 0.1 if returned:
610 if scl.shape != avg.shape:
611 scl = np.broadcast_to(scl, avg.shape).copy()
612 return avg, scl
613 else:
614 1 6 6.0 0.1 return avg
这次 np.ma.MaskedArray.mean
函数几乎占用了 90% 的时间。
注意:您还可以更深入地研究 np.ma.asarray
或 np.ma.MaskedArray.count
或 np.ma.MaskedArray。 mean
并检查他们的线路配置文件。但我只是想表明有很多被调用的函数会增加开销。
所以下一个问题是:np.ndarray.mean
和 np.ma.average
之间的相对时间是否也发生了变化?至少在我的电脑上,差异现在要小得多:
%timeit np.ma.average(arr, axis=0)
# 2.96 ms ± 91 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit arr.mean(axis=0)
# 1.84 ms ± 23.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
这次它甚至没有慢 2 倍。我假设对于更大的阵列,差异会变得更小。
这也是 NumPy 实际上很常见的东西:
即使对于普通的 numpy 函数,常数因子也非常高(例如,参见我对问题 "Performance in different vectorization method in numpy" 的回答)。对于 np.ma
,这些常数因子甚至更大,尤其是当您不使用 np.ma.MaskedArray
作为输入时。但是,尽管常数因子可能很高,但这些函数在处理大型数组时表现出色。
关于python - 为什么 numpy ma.average 比 arr.mean 慢 24 倍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45573265/
选项1:ViewModelA ModelA 选项2:ViewModelA ModelA * ModelA仅通过ViewModelB更改其属性,而仅更改,因此在Option2中,它无需实现任何机制即
考虑到下表,我对显示数据有一种罕见的态度: mysql> describe materiales; +-----------+--------------+------+-----+---------
我有一些时间序列数据点,我喜欢对它们执行简单的移动平均方法。如果我使用“forecast”包中的函数“ma”,我会得到以下结果: library(forecast) x<-c(1,5,2,8,6,3,
我正在尝试使用 numpy.ma.corrcoef 在存在缺失数据的情况下计算相关性。 根据文档:除了处理缺失数据外,此函数与 numpy.corrcoef 的功能相同。有关详细信息和示例,请参阅 n
我有两个(车速)信号,它们应该由类似的“潜在”驱动因素组成,但具有不同的自相关结构。驱动程序信号在统计上非常糟糕,因此我没有尝试对它们进行建模。 我可以通过使用 AR(1) 残差对信号进行预白化来获得
我无法获取 numpy.ma处理我的数据。我确定我以前用过它来屏蔽空白值,但不知道如何使用。这是显示我遇到的问题的代码片段。 import numpy as np import numpy.ma as
我这里有一个程序可以输出一个句子中的回文数。不区分大小写并忽略逗号和句点,例如当回文恰好是句子中的最后一个单词时。 #include #include using namespace std; c
numpy.ma.masked_where 用于屏蔽单个值。还有用于屏蔽间隔的 numpy.ma.masked_inside。 但是我不太明白它应该如何工作。 I found this snippet
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
我正在尝试子类化 numpy 的 MaskedArray添加属性,但似乎无法获得正确的结果。 我开始关注 the example用于子类化 numpy.ndarray ,效果很好。 然后我尝试子类化
我有一个页面,其中包含用于报告功能的图表和 map 菜单。然后,我选择了Highchart-Highmap库来达到上面的目的。图表功能运行良好,但是当我开发 map 功能时出现错误 TypeError
示例数据集(行是从一个更大的矩阵中随机提取的) import numpy as np test = [[np.nan, np.nan, 0.217, 0.562], [np.nan,
我注意到 numpy masked-array mean method可能不应该返回不同的类型: import numpy as np A = np.ma.masked_equal([1,1,0],
我正在使用 R 的预测包,并使用 ARIMA 函数创建了一个 MA(1) 模型。我绘制了时间序列本身($ma_model 的 x 变量)、模型本身($ma_model 的拟合变量)和残差(ma_mod
我正在尝试拍摄一个具有 h264 视频轨道和 dts-ma 音频轨道的视频,并生成一个 .m4v 文件: h264 视频轨道DTS-ma音轨AC-3 5.1音轨 我试图只复制视频和 dts-ma 音轨
我的印象是转储产生了我的 winDbg .dump /ma 不仅仅包含一个完整的转储 正在配置 WER解释 DumpType 0:自定义转储MINIDUMP_TYPE 的 CustomDumpFlag
我正在尝试编写一个子类 masked_array。到目前为止我得到的是: class gridded_array(ma.core.masked_array): def __init__(sel
我正在用 c++ 中的 openmp 做一个循环,以从 QVector 向 QMAP 中提供一些值,在某些计算机中它说“程序停止工作”,但在某些计算机中它可以工作。 我看到 Qmap 不是线程安全的,
所以我使用 numpy.ma.masked 方法在特定条件下绘制线条,但我想连接所有连续的线条。例如,使用此代码: import pylab as plt import numpy as np x =
我想从压缩的掩码数组和相应的掩码创建一个数组。用一个例子更容易解释这一点: >>> x=np.ma.array(np.arange(4).reshape((2,2)), mask = [[True,T
我是一名优秀的程序员,十分优秀!