python - 如何使用列表作为 numpy ndarrays 的索引参数?
问题描述
所以我有一个可能超级简单的问题。我有这些我分配的 numpy ndarrays,并希望通过作为列表返回的索引为它们分配值。如果我向您展示一些示例代码,可能会更容易。我有问题的代码在底部,在我的测试中(在实际进行扩展之前)我不断收到语法错误:'(
编辑:编辑以使其更容易进行故障排除并将一些示例代码放在底部
import numpy as np
def do_stuff(index, mask):
# this is where the calculations are made
magic = sum(mask)
return index, magic
def foo(full_index, comparison_dims, *xargs):
# I have this function executed in Parallel since I'm using a machine with 36 nodes per core, and can access upto 16 cores for each script #blessed
# figure out how many dimensions there are, and how big they are
parent_dims = []
parent_diffs = []
for j in xargs:
parent_dims += [len(j)]
parent_diffs += [j[1] - j[0]] # this is used to find a mask
index = [] # this is where the individual dimension indices will be stored
dim_n = 0
# loop through the dimensions
while dim_n < len(parent_dims):
dim_index = full_index % parent_dims[dim_n]
index += [dim_index]
if dim_n == 0:
mask = (comparison_dims[dim_n] > xargs[dim_n][dim_index] - parent_diffs[dim_n]/2) * \
(comparison_dims[dim_n] <= xargs[dim_n][dim_index] +parent_diffs[dim_n] / 2)
else:
mask *= (comparison_dims[dim_n] > xargs[dim_n][dim_index] - parent_diffs[dim_n]/2) * \
(comparison_dims[dim_n] <=xargs[dim_n][dim_index] + parent_diffs[dim_n] / 2)
full_index //= parent_dims[dim_n]
dim_n += 1
return do_stuff(index, mask)
def bar(comparison_dims, *xargs):
if len(xargs) == comparison_dims.shape[0]:
pass
elif len(comparison_dims.shape) == 2:
pass
else:
raise ValueError("silly person, you failed")
from joblib import Parallel, delayed
dims = []
for j in xargs:
dims += [len(j)]
myArray = np.empty(tuple(dims))
results = Parallel(n_jobs=1)(
delayed(foo)(
index, comparison_dims, *xargs)
for index in range(np.prod(dims))
)
# LOOK HERE, HELP HERE!
for index_list, result in results:
# I thought this would work, but oh golly was I was wrong, index_list here is a list of ints, and result is a value
# for example index, result = [0,3,7], 45.4
# so in execution, that would yield: myArray[0,3,7] = 45.4
# instead it yields SyntaxError because I don't know what I'm doing XD
myArray[*index_list] = result
return myArray
任何想法我可以如何使这项工作。我需要做什么?
我不是棚子里最锋利的工具,但我认为在你的帮助下,我们或许能够解决这个问题!
解决此问题的一个简单示例是:
compareDims = np.array([np.random.rand(1000), np.random.rand(1000)])
dim0 = np.arange(0,1,1./20)
dim1 = np.arange(0,1,1./30)
myArray = bar(compareDims, dim0, dim1)
解决方案
使用任意多维索引列表来索引 numpy 数组。你实际上需要使用一个元组:
for index_list, result in results:
myArray[tuple(index_list)] = result
推荐阅读
- flutter - 错误状态:Stream 已经被 Flutter 提供者监听
- azure - 跨订阅移动 Azure ExpressRoute 线路
- assembly - 在自定义机器上运行 GCC 编译代码。在程序集中找不到 sp 初始化
- powershell - 安全访问 Key Vault 机密
- python - 当我在类中按下 Python Tkinter 中的按钮时如何删除条目
- html - 如何在 html 和 css 中使背景颜色(光泽)发光
- angular - 如何关闭对话框Angular 9 ngx admin
- vb.net - 如何在 vb.net 中导出 XML 代码文档?
- keycloak - 无法从应用程序加载 keycloak 身份验证页面,使用 ajax 请求调用受保护资源
- java - pom.xml 每次创建新的 spring 项目时都会出现相同的错误?