0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

3次元配列の各列をシャッフルする

Last updated at Posted at 2021-10-07

feature importance用のコード
numpy配列のシャッフルが一番可用性が高そうなので作った

  • numpyのシャッフルからのtensor変換
  • イテレーションごとに配列の再定義
    が必要
    列ごとの要素をとってきてシャッフルして再代入


# 3次元のnumpyの列をシャッフル
# イテレーションごとに3次元配列を再定義しないとすべての列がシャッフルされてしまうので注意
def shuffletensorcolumns_loop(array,i: int) -> None:
    tmp = array[:,:,i]
    for j in range(array.shape[0]):
      np.random.shuffle(tmp[j]) # 破壊的処理なので代入不要
    array[:,:,i] = tmp


df = np.arange(1,201).reshape(5,4,10)
for i in range(df.shape[2]):
    df2 = df.copy()
    shuffletensorcolumns_loop(df2,i)
    print(df2)

追記
メモリチェック

df = np.arange(1,201).reshape(5,4,10)

for i in range(df.shape[2]):
    df2 = df.copy()
    print(id(df))
    shuffletensorcolumns_loop(df2,i)
    print(id(df2))
    print(np.may_share_memory(df,df2))

>>>
140643364911504
140643364910064
False
140643364911504
140643365525984
False
140643364911504
140643364911824
False
...
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?