What is 1 of K encoding. One hot representation. (Python)



Python
library
pip

MeCab

Common
class

pickle

read/readline

numpy
asfarray

digitize

expit

linalg.solve

meshgrid

mgrid

ndmin

pad

poly1d

polyfit

prod

shape

matplotlib
figure

pcolormesh

scatter

pytorch
BCELoss, MSELoss

device

Embedding

TensorDataset, Dataloader

RNN, LSTM
scikit-learn
SVC

GaussianNB

scipy
interpolate
tkinter
postscript

image display

frame, grid

Crop Image

other
linear interpolation

Hysteresis switch

Square/Triangle wave

OpenAI gym
CartPole-v0

By purpose
1 of K Coding


Release date:2024/9/15         

In Japanese


■1 of K coding explained

1 of K coding is a method to create an array in which only certain elements are 1 and the rest are 0. It is also called one hot representation. It is often used in machine learning.

■Example implementation of 1 of K encoding

An example implementation using numpy is as follows:

import numpy as np
a = np.zeros(5) # Create an array with 5 elements, all with a value of 0
a[3] = 1    # # Set the third element to 1
print(a)

  ⇒ array([0., 0., 0., 1., 0.])



See the implementation example using keras here

from keras.utils import np_utils
a= np_utils.to_categorical(3, 5)  # Set the third element of a five-element array to 1
print(a)

  ⇒ array([0., 0., 0., 1., 0.], dtype=float32)










List of related articles



Python
library
pip

MeCab

Common
class

pickle

read/readline

numpy
asfarray

digitize

expit

linalg.solve

meshgrid

mgrid

ndmin

pad

poly1d

polyfit

prod

shape

matplotlib
figure

pcolormesh

scatter

pytorch
BCELoss, MSELoss

device

Embedding

TensorDataset, Dataloader

RNN, LSTM
scikit-learn
SVC

GaussianNB

scipy
interpolate
tkinter
postscript

image display

frame, grid

Crop Image

other
linear interpolation

Hysteresis switch

Square/Triangle wave

OpenAI gym
CartPole-v0

By purpose
1 of K Coding