danfo.LabelEncoder
Encode target labels with value between 0 and n_classes-1.
class danfo.LabelEncoder [source]
danfo.js provides the LabelEncoder class for encoding Series and Arrays to integer between 0 and n_classes -1. This is mostly used as a preprocessing step before most machine learning tasks.
The API is similar to sklearn's LabelEncoder, and provides a fit and transform method.

Examples

Label Encode values in a Series

Node
Browser
1
let data = ["dog","cat","man","dog","cat","man","man","cat"]
2
let series = new dfd.Series(data)
3
4
let encode = new dfd.LabelEncoder()
5
6
encode.fit(series)
7
console.log(encode);
8
9
let sf_enc = encode.transform(series.values)
10
sf_enc.print()
11
12
let new_sf = encode.transform(["dog","man"])
13
new_sf.print()
Copied!
1
Copied!
Output
1
LabelEncoder { label: [ 'dog', 'cat', 'man' ] }
2
╔═══╤══════════════════════╗
3
║ │ 0 ║
4
╟───┼──────────────────────╢
5
║ 0 │ 0 ║
6
╟───┼──────────────────────╢
7
║ 1 │ 1 ║
8
╟───┼──────────────────────╢
9
║ 2 │ 2 ║
10
╟───┼──────────────────────╢
11
║ 3 │ 0 ║
12
╟───┼──────────────────────╢
13
║ 4 │ 1 ║
14
╟───┼──────────────────────╢
15
║ 5 │ 2 ║
16
╟───┼──────────────────────╢
17
║ 6 │ 2 ║
18
╟───┼──────────────────────╢
19
║ 7 │ 1 ║
20
╚═══╧══════════════════════╝
21
22
╔═══╤══════════════════════╗
23
║ │ 0 ║
24
╟───┼──────────────────────╢
25
║ 0 │ 0 ║
26
╟───┼──────────────────────╢
27
║ 1 │ 2 ║
28
╚═══╧══════════════════════╝
Copied!
Labels not found in the original data used for fitting are represented with -1
Node
Browser
1
const dfd = require("danfojs-node")
2
3
let data = { fruits: ['pear', 'mango', "pawpaw", "mango", "bean"] ,
4
Count: [20, 30, 89, 12, 30],
5
Country: ["NG", "NG", "GH", "RU", "RU"]}
6
7
8
let df = new dfd.DataFrame(data)
9
let encode = new dfd.LabelEncoder()
10
11
encode.fit(df['fruits'])
12
console.log(encode);
13
14
let sf_enc = encode.transform(df['fruits'].values)
15
sf_enc.print()
16
17
let new_sf = encode.transform(["mango","man"])
18
new_sf.print()
Copied!
1
Copied!
Output
1
LabelEncoder { label: [ 'pear', 'mango', 'pawpaw', 'bean' ] }
2
╔═══╤══════════════════════╗
3
║ │ 0 ║
4
╟───┼──────────────────────╢
5
║ 0 │ 0 ║
6
╟───┼──────────────────────╢
7
║ 1 │ 1 ║
8
╟───┼──────────────────────╢
9
║ 2 │ 2 ║
10
╟───┼──────────────────────╢
11
║ 3 │ 1 ║
12
╟───┼──────────────────────╢
13
║ 4 │ 3 ║
14
╚═══╧══════════════════════╝
15
16
╔═══╤══════════════════════╗
17
║ │ 0 ║
18
╟───┼──────────────────────╢
19
║ 0 │ 1 ║
20
╟───┼──────────────────────╢
21
║ 1 │ -1 ║
22
╚═══╧══════════════════════╝
Copied!
Last modified 1yr ago