python - Numba:选择数组的 N 维子集
问题描述
让我们有一些data
选择index
:
import numpy as np
shape = 50, 50, 50, 50
data = np.linspace(0.0, 10.0 - 1.0e-3, int(np.prod(shape))).reshape(shape)
index = (data > data.mean()).astype(bool)
现在,在常规中numpy
,我可以简单地
In: data[index]
Out: array([4.9995008, 4.9995024, 4.999504 , ..., 9.9989968, 9.9989984,
9.999 ])
但numba
如果我不事先展平阵列,显然不能这样做,这有点贵:
@jit(nopython=True, parallel=False)
def select_4d(data, index):
return data[index]
select_4d(data, index)
导致错误(下)。有没有一种廉价的方法来解决这个问题而不使阵列变平?
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function getitem>) found for signature:
>>> getitem(array(float64, 4d, C), array(bool, 4d, C))
There are 22 candidate implementations:
- Of which 20 did not match due to:
Overload of function 'getitem': File: <numerous>: Line N/A.
With argument(s): '(array(float64, 4d, C), array(bool, 4d, C))':
No match.
- Of which 2 did not match due to:
Overload in function 'GetItemBuffer.generic': File: numba/core/typing/arraydecl.py: Line 162.
With argument(s): '(array(float64, 4d, C), array(bool, 4d, C))':
Rejected as the implementation raised a specific error:
TypeError: unsupported array index type array(bool, 4d, C) in [array(bool, 4d, C)]
raised from /home/saman/anaconda3/envs/myenv3/lib/python3.9/site-packages/numba/core/typing/arraydecl.py:68
解决方案
推荐阅读
- android - Json$Default 不能转换为 kotlinx.serialization.json.Json
- ios - 如何使用 Swift JSONDecoder 解码可以是数组或单个对象的 JSON 属性?
- mongodb - MongoDB 按年和班级分组然后计数 - 不工作
- vba - 如何检索和存储 JSESSIONID cookie?
- java - 在 ContextInitializer 方法中从 schema.sql 文件执行 sql 语句
- c# - 我可以在 Xamarin Forms 上检查设备中的可用存储吗?
- plugins - 无法应用插件 [id 'com.jfrog.artifactory']
- lua - 为什么和/或 nil 值在哪里?
- python - 从 PyBioMed.PyMolecule 导入 moe;ModuleNotFoundError:没有名为“rdkit”的模块
- excel - 如何在vba excel中引用选定的单元格块