LabelEncoder: Label Encoder

Description Format Usage Methods Arguments Public fields Methods Examples

Description

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.

Format

R6Class object.

Usage

For usage details see Methods, Arguments and Examples sections.

1
2
3
4
lbl = LabelEncoder$new()
lbl$fit(x)
lbl$fit_transform(x)
lbl$transform(x)

Methods

$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

Arguments

data

a vector or list containing the character / factor values

Public fields

input_data

internal use

encodings

internal use

decodings

internal use

fit_model

internal use

Methods

Public methods


Method fit()

Usage
LabelEncoder$fit(data_col)
Arguments
data_col

a vector containing non-null values

Details

Fits the labelencoder model on given data

Returns

NULL, calculates the encoding and save in memory

Examples
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 fit_transform()

Usage
LabelEncoder$fit_transform(data_col)
Arguments
data_col

a vector containing non-null values

Details

Fits and returns the encoding

Returns

encoding values for the given input data

Examples
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 transform()

Usage
LabelEncoder$transform(data_col)
Arguments
data_col

a vector containing non-null values

Details

Returns the encodings from the fitted model

Returns

encoding values for the given input data

Examples
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 inverse_transform()

Usage
LabelEncoder$inverse_transform(coded_col)
Arguments
coded_col

a vector containing label encoded values

Details

Gives back the original values from a encoded values

Returns

original values from the label encoded data

Examples
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 clone()

The objects of this class are cloneable with this method.

Usage
LabelEncoder$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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)

superml documentation built on April 28, 2020, 9:05 a.m.