OneHotEncoder.fit: OneHotEncoder.fit fits an OneHotEncoder object

Description Usage Arguments Value Examples

View source: R/basic.R

Description

OneHotEncoder.fit fits an OneHotEncoder object

Usage

1

Arguments

X

A matrix or data.frame, which can include NA

Value

Returns an object of S4 class OneHotEncoder

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# matrix input
X1 <- matrix(c(0, 1, 0, 1, 0, 1, 2, 0, 3, 0, 1, 2),c(4,3),byrow=FALSE)
oenc <- OneHotEncoder.fit(X1)
z <- transform(oenc,X1,sparse=TRUE)
# return a sparse matrix
print(z)

# data.frame
X2 <- cbind(data.frame(X1),X4=c('a','b','d',NA),X5=factor(c(1,2,3,1)))
oenc <- OneHotEncoder.fit(X2)
z <- transform(oenc,X2,sparse=FALSE)
# return a dense matrix
print(z)

Example output

Attaching package: 'CatEncoders'

The following object is masked from 'package:base':

    transform

4 x 9 sparse Matrix of class "dgCMatrix"
                      
[1,] 1 . 1 . . . . . 1
[2,] . 1 . 1 . 1 . . .
[3,] 1 . . . 1 . 1 . .
[4,] . 1 1 . . . . 1 .
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,]    1    0    1    0    0    0    0    0    1     0     1     0     0     1
[2,]    0    1    0    1    0    1    0    0    0     0     0     1     0     0
[3,]    1    0    0    0    1    0    1    0    0     0     0     0     1     0
[4,]    0    1    1    0    0    0    0    1    0     1     0     0     0     1
     [,15] [,16]
[1,]     0     0
[2,]     1     0
[3,]     0     1
[4,]     0     0

CatEncoders documentation built on May 1, 2019, 6:29 p.m.