Skip to main content
 首页 » 编程设计

python之Numpy ndenumerate 非常慢。有没有更快的方法在 numpy 的数组上使用条件

2025年05月04日7三少

我有 26 个数据数组,我正在处理非常大的数组,所以我想加快这个过程。这段代码确实有效并实现了我想要它实现的目标,但速度非常慢。

我想要代码做的是:

查看两个数组并选择满足或不满足特定条件的单元格。如果满足该条件,那么我想通过编辑和从另一个数组中获取值来更改这些单元格中的值。数组中的所有值都需要更新以反射(reflect)这些变化。

我下面的代码可能看起来有点困惑,但这里是。这是我的慢代码:

active_layer # An array 
 
active_layer_volumes = [] # List of 7 arrays 
active_layer_proportions = [] # List of 7 arrays 
 
inactive_layer # Array 
 
inactive_layer_volumes = [] # List of 7 arrays 
inactive_layer_proportions = [] # List of 7 arrays 
 
# Calculate the lower and upper limits for the volume of the active layer 
al_upper_volume_limit = 5 
al_lower_volume_limit = 1 
 
# Count the grainsizes as the model works through them 
grain_size_counter = 1     
 
# Set up some empty arrays to hold the new values  
new_active_layer_total = np.zeros_like(active_layer) 
new_inactive_layer_total = np.zeros_like(inactive_layer) 
 
# Iterate through the 24 arrays in order 
for active_layer_proportion, active_layer_volume, inactive_layer_proportion, inactive_layer_volume in izip(active_layer_volumes, active_layer_proportions,inactive_layer_volumes, inactive_layer_proportions): 
 
    # Iterate through all of the cells in the active layer checking to see if certain conditions are met  
    for [i, j], depth in np.ndenumerate(active_layer): # Iterate through the cells  
 
        if active_layer[i, j] >= al_upper_volume_limit: # check to see if the volume in that cell is greater than 5m3 
           inactive_layer_volume[i, j] = (20 * active_layer_proportion[i, j]) + inactive_layer_volume[i, j] # add 20cm proportion of that grainsize to the active layer 
           active_layer_volume[i, j] = (active_layer[i, j] - 20) * active_layer_proportion[i, j]  
 
        elif active_layer[i, j] < al_lower_volume_limit and inactive_layer[i, j] > 0: # check to see if the volume in that cell is greater than 5m3         
            active_layer_volume[i, j] = (20 * inactive_layer_proportion[i, j]) + active_layer_volume[i, j]                       
            inactive_layer_volume[i, j] = inactive_layer_volume[i, j] - (20 * inactive_layer_proportion[i, j]) 
 
    # Increment a counter as the loop goes through the arrays                       
    grain_size_counter + 1 
 
    # Add the new calculated volumes to a running total array 
    new_active_layer_total += active_layer_volume 
    new_inactive_layer_total += inactive_layer_volume 

请您参考如下方法:

您可以将 ndenumerate 上的内部循环替换为以下向量化表达式:

# Array B contains True/False for the condition and is subsequently  
# used as Boolean index. 
B = (active_layer >= al_upper_volume_limit) 
inactive_layer_volume[B] += 20 * active_layer_proportion[B] 
active_layer_volume[B] = (active_layer[B] - 20) * active_layer_proportion[B] 
 
# The "not B" does the "else" part of the elif statement it replaces 
B = ~B & (active_layer < al_lower_volume_limit) & (inactive_layer > 0) 
active_layer_volume[B] += 20 * inactive_layer_proportion[B]                       
inactive_layer_volume[B] -= 20 * inactive_layer_proportion[B]