Help us understand the problem. What is going on with this article?

整数値のベクトルをone hot表現に変換

More than 1 year has passed since last update.

クラス分類問題などで、整数値のベクトルをone hot表現に変換する場合、
pythonではnumpyを使って以下のように変換できる。

python
import numpy as np

target_vector = [0,2,1,3,4]               # クラス分類を整数値のベクトルで表現したもの
n_labels = len(np.unique(target_vector))  # 分類クラスの数 = 5
np.eye(n_labels)[target_vector]           # one hot表現に変換
実行して得られるone-hot表現
array([[ 1.,  0.,  0.,  0.,  0.],   # 0
       [ 0.,  0.,  1.,  0.,  0.],   # 2
       [ 0.,  1.,  0.,  0.,  0.],   # 1
       [ 0.,  0.,  0.,  1.,  0.],   # 3
       [ 0.,  0.,  0.,  0.,  1.]])  # 4

参考

https://www.reddit.com/r/MachineLearning/comments/31fk7i/converting_target_indices_to_onehotvector/

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした