LabelEncoder | R Documentation |
Encodes and decodes categorical variables into integer values and vice versa. This is a commonly performed task in data preparation during model training, because all machine learning models require the data to be encoded into numerical format. It takes a vector of character or factor values and encodes them into numeric.
R6Class
object.
For usage details see Methods, Arguments and Examples sections.
lbl = LabelEncoder$new() lbl$fit(x) lbl$fit_transform(x) lbl$transform(x)
$new()
Initialise the instance of the encoder
$fit()
creates a memory of encodings but doesn't return anything
$transform()
based on encodings learned in fit
method is applies the transformation
$fit_transform()
encodes the data and keep a memory of encodings simultaneously
$inverse_transform()
encodes the data and keep a memory of encodings simultaneously
a vector or list containing the character / factor values
input_data
internal use
encodings
internal use
decodings
internal use
fit_model
internal use
fit()
LabelEncoder$fit(data_col)
data_col
a vector containing non-null values
Fits the labelencoder model on given data
NULL, calculates the encoding and save in memory
data_ex <- data.frame(Score = c(10,20,30,4), Name=c('Ao','Bo','Bo','Co')) lbl <- LabelEncoder$new() lbl$fit(data_ex$Name) data_ex$Name <- lbl$fit_transform(data_ex$Name) decode_names <- lbl$inverse_transform(data_ex$Name)
fit_transform()
LabelEncoder$fit_transform(data_col)
data_col
a vector containing non-null values
Fits and returns the encoding
encoding values for the given input data
data_ex <- data.frame(Score = c(10,20,30,4), Name=c('Ao','Bo','Bo','Co')) lbl <- LabelEncoder$new() lbl$fit(data_ex$Name) data_ex$Name <- lbl$fit_transform(data_ex$Name)
transform()
LabelEncoder$transform(data_col)
data_col
a vector containing non-null values
Returns the encodings from the fitted model
encoding values for the given input data
data_ex <- data.frame(Score = c(10,20,30,4), Name=c('Ao','Bo','Bo','Co')) lbl <- LabelEncoder$new() lbl$fit(data_ex$Name) data_ex$Name <- lbl$transform(data_ex$Name)
inverse_transform()
LabelEncoder$inverse_transform(coded_col)
coded_col
a vector containing label encoded values
Gives back the original values from a encoded values
original values from the label encoded data
data_ex <- data.frame(Score = c(10,20,30,4), Name=c('Ao','Bo','Bo','Co')) lbl <- LabelEncoder$new() lbl$fit(data_ex$Name) data_ex$Name <- lbl$fit_transform(data_ex$Name) decode_names <- lbl$inverse_transform(data_ex$Name)
clone()
The objects of this class are cloneable with this method.
LabelEncoder$clone(deep = FALSE)
deep
Whether to make a deep clone.
data_ex <- data.frame(Score = c(10,20,30,4), Name=c('Ao','Bo','Bo','Co'))
lbl <- LabelEncoder$new()
data_ex$Name <- lbl$fit_transform(data_ex$Name)
decode_names <- lbl$inverse_transform(data_ex$Name)
## ------------------------------------------------
## Method `LabelEncoder$fit`
## ------------------------------------------------
data_ex <- data.frame(Score = c(10,20,30,4), Name=c('Ao','Bo','Bo','Co'))
lbl <- LabelEncoder$new()
lbl$fit(data_ex$Name)
data_ex$Name <- lbl$fit_transform(data_ex$Name)
decode_names <- lbl$inverse_transform(data_ex$Name)
## ------------------------------------------------
## Method `LabelEncoder$fit_transform`
## ------------------------------------------------
data_ex <- data.frame(Score = c(10,20,30,4), Name=c('Ao','Bo','Bo','Co'))
lbl <- LabelEncoder$new()
lbl$fit(data_ex$Name)
data_ex$Name <- lbl$fit_transform(data_ex$Name)
## ------------------------------------------------
## Method `LabelEncoder$transform`
## ------------------------------------------------
data_ex <- data.frame(Score = c(10,20,30,4), Name=c('Ao','Bo','Bo','Co'))
lbl <- LabelEncoder$new()
lbl$fit(data_ex$Name)
data_ex$Name <- lbl$transform(data_ex$Name)
## ------------------------------------------------
## Method `LabelEncoder$inverse_transform`
## ------------------------------------------------
data_ex <- data.frame(Score = c(10,20,30,4), Name=c('Ao','Bo','Bo','Co'))
lbl <- LabelEncoder$new()
lbl$fit(data_ex$Name)
data_ex$Name <- lbl$fit_transform(data_ex$Name)
decode_names <- lbl$inverse_transform(data_ex$Name)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.