2012-01-30 42 views
3

我特林通過取多數正方形塊陣列中的和寫入這些另一個數組減少2D陣列的尺寸的二維數組。方塊的大小是可變的,讓我們說一邊的n值。該數組的數據類型將是一個整數。我目前在Python中使用循環來將每個塊分配給一個臨時數組,然後從tmpArray中提取唯一值。然後我通過這些循環找到發生次數最多的一次。正如你可以想象的那樣,隨着輸入數組大小的增加,這個過程很快變得太慢。「平鋪」使用numpy的

我見過的例子取最小值,最大值,並從我的正方形塊的意思,但我不知道如何將它們轉換爲多數。 Grouping 2D numpy array in averageresize 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] 

回答

2

這是一個函數,它會根據numpy.unique的實現更加快速地找到多數。

def get_majority(a): 
    a = a.ravel() 
    a = np.sort(a) 
    diff = np.empty(len(a)+1, 'bool') 
    diff[0] = True 
    diff[-1] = True 
    diff[1:-1] = a[1:] != a[:-1] 
    where = np.where(diff)[0] 
    counts = where[1:] - where[:-1] 
    argmax = counts.argmax() 
    uniq_a = a[diff[1:]] 
    return uniq_a[argmax] 

讓我知道這是否有幫助。

更新

您可以執行以下操作,讓您的陣列是(n*n, x, y),應該設置你的第一軸運行,得到這個矢量化的方式來完成。

X, Y = a.shape 
x = X // n 
y = Y // n 
a = a.reshape((x, n, y, n)) 
a = a.transpose([1, 3, 0, 2]) 
a = a.reshape((n*n, x, y)) 

只需記住一些事情。儘管重塑和轉置儘可能返回視圖,但我認爲重塑轉置重塑將被迫複製。還應該推廣上述方法以在軸上操作,但可能需要一點創意。

+0

這有一定的幫助!我仍然希望一次在整個數據集中實現這個算法。像grid.reshape((5,grid.shape [0] // 55,-1)).max(axis = 3).max(1)將提供最大值。如果我想出來,我會發布解決方案。 – 2012-01-31 00:10:07

+0

真是太棒了!看到我需要學習多少,真是令人h目。 我已經通過您的建議,但將其更改爲(x * y,n * n)數組。 – 2012-01-31 18:53:11

1

這可能是一個有點紅臉了,但我最終訴諸scipy.stats.stats模式功能,發現大多數的值。我不確定在處理時間方面與其他解決方案相比如何。

import scipy.stats.stats as stats 
#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 = 2 
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)) 
answer = np.array(stats.mode(grid, 1)[0]).reshape(x, y) 
+0

我認爲stats.mode是一個很好的選擇。對不起,因爲模式返回數組,所以你可以把np.array放在最後一行。 – 2012-01-31 23:36:25