2
1

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 5 years have passed since last update.

KerasでOne-Hotエンコーディングする方法

Last updated at Posted at 2019-05-19

備忘目的で書いています。
参考にしたのは=> https://github.com/fchollet/deep-learning-with-python-notebooks

  1. サンプルデータをインポート
    ここではMNISTをKerasからインポートします。
from keras.datasets import mnist

(train_images, train_labels) , (test_images, test_labels) = mnist.load_data()

# >print(train_labels[:3])
# [5 0 4]
  1. One-hotエンコーディング
    kerasに実装されたto_categoricalを使います。
    [0 ~ 9]の10個の数字が存在するのでone-hot結果の次元数が10になっています。

from keras.utils import to_categorical
train_labels_onehot = to_categorical(train_labels)

#> print(train_labels_onehot[:3])
# [[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
#  [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
#  [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]]
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?