我有一個 100 x 100 的 2d numpy 陣列。我想從“內部”80 x 80 值中隨機采樣值,以便我可以排除受邊緣效應影響的值。我想從第 10 行到第 90 行以及從第 10 列到第 90 列進行采樣。
然而,重要的是,我需要保留100 x 100 網格中的原始索引值,所以我不能只是修剪資料集并繼續前進。如果我這樣做,我并沒有真正解決邊緣效應問題,因為這是在多次迭代的回圈中發生的。
gridsize = 100
new_abundances = np.zeros([100,100],dtype=np.uint8)
min_select = int(np.around(gridsize * 0.10))
max_select = int(gridsize - (np.around(gridsize * 0.10)))
row_idx =np.arange(min_select,max_select)
col_idx = np.arange(min_select,max_select)
indices_random = ????? 不知何故,僅在 row_idx 和 col_idx 集合的行和列中從 new_abundances 中隨機采樣。
我最終需要的是從扁平化的 new_abundances 陣列中選擇的 250 個隨機索引的串列。我需要將 new_abundances 陣列保留為 2d 以識別“邊緣”,但是一旦完成,我需要將其展平以獲得隨機選擇的索引。
期望的輸出:
來自展平的 new_abundances 陣列的一維索引串列。
uj5u.com熱心網友回復:
像這樣的東西能解決你的問題嗎?
import numpy as np
np.random.seed(0)
mat = np.random.random(size=(100,100))
x_indices = np.random.randint(low=10, high=90, size=250)
y_indices = np.random.randint(low=10, high=90, size=250)
coordinates = list(zip(x_indices,y_indices))
flat_mat = mat.flatten()
flat_index = x_indices * 100 y_indices
然后,您可以使用coordinates串列中的任何值訪問元素,例如mat[coordinates[0]]回傳 處的矩陣值coordinates[0]。的價值coordinates[0]是(38, 45)在我的情況下。如果矩陣被展平,則可以計算相應元素的一維索引。在這種情況下,mat[coordinates[0]] == flat_mat[flat_index[0]]持有,其中flat_index[0]==3845=100*38 45
另請注意,通過這種方式可以對原始資料進行多次采樣。
使用你的符號:
import numpy as np
np.random.seed(0)
gridsize = 100
new_abundances = np.zeros([100,100],dtype=np.uint8)
min_select = int(np.around(gridsize * 0.10))
max_select = int(gridsize - (np.around(gridsize * 0.10)))
x_indices = np.random.randint(low=min_select, high=max_select, size=250)
y_indices = np.random.randint(low=min_select, high=max_select, size=250)
coords = list(zip(x_indices,y_indices))
flat_new_abundances = new_abundances.flatten()
flat_index = x_indices * gridsize y_indices
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/397941.html
