lgb.prepare_rules.Rd 1.79 KB
Newer Older
James Lamb's avatar
James Lamb committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/lgb.prepare_rules.R
\name{lgb.prepare_rules}
\alias{lgb.prepare_rules}
\title{Data preparator for LightGBM datasets with rules (numeric)}
\usage{
lgb.prepare_rules(data, rules = NULL)
}
\arguments{
\item{data}{A data.frame or data.table to prepare.}

\item{rules}{A set of rules from the data preparator, if already used.}
}
\value{
15
16
17
A list with the cleaned dataset (\code{data}) and the rules (\code{rules}).
        The data must be converted to a matrix format (\code{as.matrix}) for input
        in \code{lgb.Dataset}.
James Lamb's avatar
James Lamb committed
18
19
}
\description{
20
Attempts to prepare a clean dataset to prepare to put in a \code{lgb.Dataset}.
21
22
             Factors and characters are converted to numeric. In addition, keeps rules created
             so you can convert other datasets using this converter.
James Lamb's avatar
James Lamb committed
23
24
25
26
27
28
29
30
31
32
}
\examples{
data(iris)

str(iris)

new_iris <- lgb.prepare_rules(data = iris) # Autoconverter
str(new_iris$data)

data(iris) # Erase iris dataset
33
iris$Species[1L] <- "NEW FACTOR" # Introduce junk factor (NA)
James Lamb's avatar
James Lamb committed
34
35
36
37
38
39

# Use conversion using known rules
# Unknown factors become 0, excellent for sparse datasets
newer_iris <- lgb.prepare_rules(data = iris, rules = new_iris$rules)

# Unknown factor is now zero, perfect for sparse datasets
40
newer_iris$data[1L, ] # Species became 0 as it is an unknown factor
James Lamb's avatar
James Lamb committed
41

42
newer_iris$data[1L, 5L] <- 1.0 # Put back real initial value
James Lamb's avatar
James Lamb committed
43
44
45
46
47
48
49
50

# Is the newly created dataset equal? YES!
all.equal(new_iris$data, newer_iris$data)

# Can we test our own rules?
data(iris) # Erase iris dataset

# We remapped values differently
51
52
53
54
55
56
57
personal_rules <- list(
    Species = c(
        "setosa" = 3L
        , "versicolor" = 2L
        , "virginica" = 1L
    )
)
James Lamb's avatar
James Lamb committed
58
59
60
61
newest_iris <- lgb.prepare_rules(data = iris, rules = personal_rules)
str(newest_iris$data) # SUCCESS!

}