我有26个数据数组,我正在使用非常大的数组,因此我想加快此过程。这段代码实际上可以工作,并且可以达到我想要的目的,但是速度却非常慢。

我想要的代码是:

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

我的以下代码似乎有些混乱,但是可以解决。这是我的慢代码:

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]

10-04 23:06
查看更多