简体   繁体   English

替换 NumPy 数组的某些给定索引的最有效方法是什么?

[英]What's the most efficient way to replace some given indices of a NumPy array?

I have three arrays, indices , values , and replace_values .我有三个 arrays、 indicesvaluesreplace_values I have to loop over indices , replacing each value in old_values[indices[i]] with new_values[i] .我必须遍历indices ,用new_values[i]替换old_values[indices[i]]中的每个值。 What's the fastest possible way to do this?最快的方法是什么? It feels like there should be some way to speed it up using NumPy functions or advanced slicing instead of the normal for loop.感觉应该有某种方法可以使用 NumPy 函数或高级切片而不是正常for循环来加速它。

This code works, but is relatively slow:此代码有效,但相对较慢:

import numpy as np

# Example indices and values
values = np.zeros([5, 5, 3]).astype(int)

indices = np.array([[0,0], [1,0], [1,3]])
replace_values = np.array([[140, 150, 160], [20, 30, 40], [100, 110, 120]])

print("The old values are:")
print(values)

for i in range(len(indices)):
    values[indices[i][0], indices[i][1]] = replace_values[i]

print("The new values are:")
print(values)

Use zip to separate x and y indices, then cast to tuple and assign:使用zip分隔xy索引,然后转换为tuple并分配:

>>> values[tuple(zip(*indices))] = replace_values
>>> values

array([[[140, 150, 160],
        [  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0]],

       [[ 20,  30,  40],
        [  0,   0,   0],
        [  0,   0,   0],
        [100, 110, 120],
        [  0,   0,   0]],

       [[  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0]],

       [[  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0]],

       [[  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0]]])

Where tuple(zip(*indices)) returns:其中tuple(zip(*indices))返回:

((0, 1, 1), (0, 0, 3))

As your indices is np.array itself, you can remove zip and use transpose, as pointed out by @MadPhysicist:正如@MadPhysicist 所指出的,由于您的索引本身就是np.array ,因此您可以删除zip并使用转置:

>>> values[tuple(*indices.T)]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 numpy 二维数组中查找元素索引的最有效方法 - The most efficient way of finding indices of element(s) in numpy 2D array 给定一个数组,创建 arrays 数组的有效方法是什么,其中每个子数组的索引与给定数组中的值相等 - Given an array, what's an efficient way to create an array of arrays where each of these subarrays has indices with equal values from the given array 将 MySQL 结果集转换为 NumPy 数组的最有效方法是什么? - What's the most efficient way to convert a MySQL result set to a NumPy array? 在 NumPy Python 中广播行到列时通过引用增加数组的最有效方法是什么? 可以矢量化吗? - What's the most efficient way to increment an array by a reference while broadcasting row to column in NumPy Python? Can it be vectorized? 检查 NumPy 数组中是否存在值的最有效方法是什么? - What is the most efficient way to check if a value exists in a NumPy array? 访问存储在 NumPy 数组中的树节点的最有效方法是什么 - What is most efficient way to access nodes of a tree stored in a NumPy array 在另一个数组中找到一个 numpy 数组的行列的最有效方法是什么? - What is the most efficient way of finding the ranks of one numpy array in another? 在Numpy数组中匹配模板的最有效方法是什么? - What is the most efficient way to match templates in a Numpy array? 基于两个numpy数组获取排序索引的最有效方法 - Most efficient way to get sorted indices based on two numpy arrays 索引 Numpy 矩阵的最有效方法是什么? - What is the most efficient way of indexing Numpy matrices?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM