LoginSignup
0
0

More than 3 years have passed since last update.

numpy2次元配列を3次元Onehot表現

Last updated at Posted at 2020-07-31

やりたかったこと

  • numpy2次元配列を3次元Onehot表現
  • (Qiitaに初投稿😀)
  • より良い書き方を知りたいです!詳しい方教えて下さい。

想定する場面

  • 画像セグメンテーションであれば
    • 2次元配列は「正解ラベルの元画像」に相当
    • 3次元配列は「加工済の正解ラベル」に相当

コード

import numpy as np

# クラス数
class_num = 4

# 各要素がクラスに該当する2次元の配列
arr_2d = np.array([[0, 1]
                  ,[2, 1]])

# 各層がクラスに対応する3次元の配列
np.identity(class_num)[arr_2d].transpose(2,0,1)

まだやってないこと

  • numpyだけでなくpytorchのTensor配列でも同様のことができそう
  • 複数枚の処理
0
0
1

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