我特林通過取多數正方形塊陣列中的和寫入這些另一個數組減少2D陣列的尺寸的二維數組。方塊的大小是可變的,讓我們說一邊的n值。該數組的數據類型將是一個整數。我目前在Python中使用循環來將每個塊分配給一個臨時數組,然後從tmpArray中提取唯一值。然後我通過這些循環找到發生次數最多的一次。正如你可以想象的那樣,隨着輸入數組大小的增加,這個過程很快變得太慢。「平鋪」使用numpy的
我見過的例子取最小值,最大值,並從我的正方形塊的意思,但我不知道如何將它們轉換爲多數。 Grouping 2D numpy array in average 和 resize with averaging or rebin a numpy 2d array
我正在尋找使用numpy的整個數組上執行此過程加快這一進程的一些手段。 (切換爲輸入變得太大,無法在內存中平鋪陣列的部分,我可以處理這方面)
感謝
#snippet of my code
#pull a tmpArray representing one square chunk of my input array
kernel = sourceDs.GetRasterBand(1).ReadAsArray(int(sourceRow),
int(sourceCol),
int(numSourcePerTarget),
int(numSourcePerTarget))
#get a list of the unique values
uniques = np.unique(kernel)
curMajority = -3.40282346639e+038
for val in uniques:
numOccurances = (array(kernel)==val).sum()
if numOccurances > curMajority:
ans = val
curMajority = numOccurances
#write out our answer
outBand.WriteArray(curMajority, row, col)
#This is insanity!!!
繼勃固的外觀極好建議,我覺得我好上通向解決方案的途徑。 這是我到目前爲止。我做的一個改變是使用原始網格形狀中的一個(x y,n n)數組。我遇到的問題是,我似乎無法弄清楚如何將where,counts和uniq_a步驟從一維轉換爲兩維。
#test data
grid = np.array([[ 37, 1, 4, 4, 6, 6, 7, 7],
[ 1, 37, 4, 5, 6, 7, 7, 8],
[ 9, 9, 11, 11, 13, 13, 15, 15],
[9, 10, 11, 12, 13, 14, 15, 16],
[ 17, 17, 19, 19, 21, 11, 23, 23],
[ 17, 18, 19, 20, 11, 22, 23, 24],
[ 25, 25, 27, 27, 29, 29, 31, 32],
[25, 26, 27, 28, 29, 30, 31, 32]])
print grid
n = 4
X, Y = grid.shape
x = X // n
y = Y // n
grid = grid.reshape((x, n, y, n))
grid = grid.transpose([0, 2, 1, 3])
grid = grid.reshape((x*y, n*n))
grid = np.sort(grid)
diff = np.empty((grid.shape[0], grid.shape[1]+1), bool)
diff[:, 0] = True
diff[:, -1] = True
diff[:, 1:-1] = grid[:, 1:] != grid[:, :-1]
where = np.where(diff)
#This is where if falls apart for me as
#where returns two arrays:
# row indices [0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3]
# col indices [ 0 2 5 6 9 10 13 14 16 0 3 7 8 11 12 15 16 0 3 4 7 8 11 12 15
# 16 0 2 3 4 7 8 11 12 14 16]
#I'm not sure how to get a
counts = where[:, 1:] - where[:, -1]
argmax = counts[:].argmax()
uniq_a = grid[diff[1:]]
print uniq_a[argmax]
這有一定的幫助!我仍然希望一次在整個數據集中實現這個算法。像grid.reshape((5,grid.shape [0] // 55,-1)).max(axis = 3).max(1)將提供最大值。如果我想出來,我會發布解決方案。 – 2012-01-31 00:10:07
真是太棒了!看到我需要學習多少,真是令人h目。 我已經通過您的建議,但將其更改爲(x * y,n * n)數組。 – 2012-01-31 18:53:11