給定一個N
行通過M
列陣列,我需要列,並在同一時間更新的(唯一的)列索引指向一個單獨的列表,將它洗洗牌元素的新位置。洗牌多維數組的列和索引的更新列表相應
例如,採取以下(3, 5)
陣列
a = [[ 0.15337424 0.21176979 0.19846229 0.5245618 0.24452392]
[ 0.17460481 0.45727362 0.26914808 0.81620202 0.8898504 ]
[ 0.50104826 0.22457154 0.24044079 0.09524352 0.95904348]]
和列的索引列表:
idxs = [0 3 4]
如果我從新按列排列,所以它看起來是這樣的:
a = [[ 0.24452392 0.19846229 0.5245618 0.21176979 0.15337424]
[ 0.8898504 0.26914808 0.81620202 0.45727362 0.17460481]
[ 0.95904348 0.24044079 0.09524352 0.22457154 0.50104826]]
索引數組應該修改爲如下所示:
idxs = [4 2 0]
我可以通過之前和之後洗牌換位它通過洗牌列陣列(見下面的代碼),但我不知道我怎麼會更新索引列表。整個過程需要儘可能快,因爲新陣列將執行數百萬次。
import numpy as np
def getData():
# Array of (N, M) dimensions
N, M = 10, 500
a = np.random.random((N, M))
# List of unique column indexes in a.
# This list could be empty, or it could have a length of 'M'
# (ie: contain all the indexes in the range of 'a').
P = int(M * np.random.uniform())
idxs = np.arange(0, M)
np.random.shuffle(idxs)
idxs = idxs[:P]
return a, idxs
a, idxs = getData()
# Shuffle a by columns
b = a.T
np.random.shuffle(b)
a = b.T
# Update the 'idxs' list?
感謝您Divakar idxs將是有益的!我試圖提高函數的性能(正如您可能從我以前的問題中猜到的那樣),並且您在https://stackoverflow.com/a/46079837/1391441中給出的答案仍然產生最快的結果。 – Gabriel