我有不同數量的具有相同大小和相同 dtype 的陣列。他們的單元格(pixels在我的代碼中呼叫)要么有一個浮點數,要么有一個 NaN。我想合并這些陣列,但有 3 個特定標準。如果對于一個像素(稱為重疊像素):
- 至少 2 個陣列有一個值(非 NaN):僅將輸入陣列的值 1 歸于合并陣列中的重疊像素。
- 只有 1 個輸入陣列有一個值,歸于合并陣列中的重疊像素,該輸入陣列的像素值。
- 如果沒有一個輸入陣列具有特定像素的值,我們
np.nan在重疊像素中寫入 a。
為了做到這一點,我有一個回圈遍歷每個像素,并評估有多少輸入陣列具有值。為了滿足第一個標準,我撰寫了一組 if/elif/else 條件。為了滿足第二個標準,else我的部分條件只是一個np.nansum(因為除了 1 個陣列之外的所有陣列在這個特定像素處都有 NaN)。
我寫了一個根本沒有效率的函式,而且非常有限。我怎樣才能改進我的代碼以處理可變數量的要合并的陣列?(超過 3 個陣列)。
我的代碼:
import numpy as np
def merger(*args):
# This function evaluates pixel per pixel the values of 2 to 3 arrays the same size.
# Each pixel either has a value or a NaN. We want to merge the arrays without summing their values at overlapping pixels.
# If at least two arrays have a value for an intersecting pixel, we pick one of the array's value to attribute to the merging pixel in the new array.
# If we have 2 arrays to merge
if len(args) == 2:
C = np.empty([args[0].shape[0], args[0].shape[1], args[0].shape[2]],dtype=float)
for b in range(args[0].shape[0]):
for i in range(args[0].shape[1]):
for j in range(args[0].shape[2]):
# If the two similar pixels have a value, take the value of the first array
if np.isnan(args[0][b,i,j]) == False and np.isnan(args[1][b,i,j]) == False:
C[b,i,j] = args[0][b,i,j]
# If all of the pixels are NaN, we input a NaN
elif np.isnan(args[0][b,i,j]) == True and np.isnan(args[1][b,i,j]) == True:
C[b,i,j] = np.nan
# Else, take the nansum of the two pixels (because one is a NaN, the other will be the real value)
else:
C[b,i,j] = np.nansum([args[0][b,i,j],args[1][b,i,j]])
# If we have 3 arrays to merge (A1, A2 and A3)
if len(args) == 3:
C = np.empty([args[0].shape[0], args[0].shape[1], args[0].shape[2]],dtype=float)
for b in range(args[0].shape[0]):
for i in range(args[0].shape[1]):
for j in range(args[0].shape[2]):
# If A1 and A2 have a value but not A3, pick the value of A1. If A1 and A3 have a value but not A2, pick the value of A1
if np.isnan(args[0][b,i,j]) == False and np.isnan(args[1][b,i,j]) == False and np.isnan(args[2][b,i,j]) == True or np.isnan(args[0][b,i,j]) == False and np.isnan(args[2][b,i,j]) == False and np.isnan(args[1][b,i,j]) == True:
C[b,i,j] = args[0][b,i,j]
# If A2 and A3 have a value but not A1, pick the value of A2
elif np.isnan(args[1][b,i,j]) == False and np.isnan(args[2][b,i,j]) == False and np.isnan(args[0][b,i,j]) == True:
C[b,i,j] = args[1][b,i,j]
# If all the arrays have a value, pick the value of A3
elif np.isnan(args[1][b,i,j]) == False and np.isnan(args[2][b,i,j]) == False and np.isnan(args[2][b,i,j]) == False:
C[b,i,j] = args[2][b,i,j]
# If all of the pixels are NaN, we input a NaN
elif np.isnan(args[1][b,i,j]) == True and np.isnan(args[2][b,i,j]) == True and np.isnan(args[2][b,i,j]) == True:
C[b,i,j] = np.nan
# If only one array has a value, nansum will attribute this value to the pixel
else:
C[b,i,j] = np.nansum([args[0][b,i,j],args[1][b,i,j], args[2][b,i,j]])
return C
# Example
A1 = np.array([[[1, 1, 1],[np.nan, np.nan, np.nan], [1, np.nan, 1]],[[1, 1, 1],[np.nan, np.nan, np.nan], [1, np.nan, 1]]])
A2 = np.array([[[1, np.nan, 1],[1, np.nan, np.nan], [1, np.nan, 1]],[[1, 1, 1],[np.nan, np.nan, 1], [np.nan, 1, 1]]])
A3 = np.array([[[1, 1, 1],[np.nan, np.nan, 1], [np.nan, 1, 1]],[[1, np.nan, 1],[1, np.nan, np.nan], [1, np.nan, 1]]])
merger(A1, A2, A3)
array([[[ 1., 1., 1.],
[ 1., nan, 1.],
[ 1., 1., 1.]],
[[ 1., 1., 1.],
[ 1., nan, 1.],
[ 1., 1., 1.]]])
uj5u.com熱心網友回復:
除非我遺漏了什么,否則這與用 A2 然后 A3 迭代替換 A1 nan 值有什么不同?由于您沒有對任何內容求和,并且您從其他陣列中任意選擇一個非空值。
A1 = np.array([[[1, 1, 1],[np.nan, np.nan, np.nan], [1, np.nan, 1]],[[1, 1, 1],[np.nan, np.nan, np.nan], [1, np.nan, 1]]])
A2 = np.array([[[1, np.nan, 1],[1, np.nan, np.nan], [1, np.nan, 1]],[[1, 1, 1],[np.nan, np.nan, 1], [np.nan, 1, 1]]])
A3 = np.array([[[1, 1, 1],[np.nan, np.nan, 1], [np.nan, 1, 1]],[[1, np.nan, 1],[1, np.nan, np.nan], [1, np.nan, 1]]])
A1[np.isnan(A1)] = A2[np.isnan(A1)]
A1[np.isnan(A1)] = A3[np.isnan(A1)]
print(A1)
輸出
[[[ 1. 1. 1.]
[ 1. nan 1.]
[ 1. 1. 1.]]
[[ 1. 1. 1.]
[ 1. nan 1.]
[ 1. 1. 1.]]]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/454372.html
上一篇:整數的陣列形式
