KNNTrainer: K Nearest Neighbours Trainer

Description Format Usage Methods Arguments Public fields Methods Examples

Description

Trains a k nearest neighbour model using fast search algorithms. KNN is a supervised learning algorithm which is used for both regression and classification problems.

Format

R6Class object.

Usage

For usage details see Methods, Arguments and Examples sections.

1
2
3
bst = KNNTrainer$new(k=1, prob=FALSE, algorithm=NULL, type="class")
bst$fit(X_train, X_test, "target")
bst$predict(type)

Methods

$new()

Initialise the instance of the trainer

$fit()

trains the knn model and stores the test prediction

$predict()

returns predictions

Arguments

k

number of neighbours to predict

prob

if probability should be computed, default=FALSE

algorithm

algorithm used to train the model, possible values are 'kd_tree','cover_tree','brute'

type

type of problem to solve i.e. regression or classification, possible values are 'reg' or 'class'

Public fields

k

number of neighbours to predict

prob

if probability should be computed, default=FALSE

algorithm

algorithm used to train the model, possible values are 'kd_tree','cover_tree','brute'

type

type of problem to solve i.e. regression or classification, possible values are 'reg' or 'class'

model

for internal use

Methods

Public methods


Method new()

Usage
KNNTrainer$new(k, prob, algorithm, type)
Arguments
k

k number of neighbours to predict

prob

if probability should be computed, default=FALSE

algorithm

algorithm used to train the model, possible values are 'kd_tree','cover_tree','brute'

type

type of problem to solve i.e. regression or classification, possible values are 'reg' or 'class'

Details

Create a new 'KNNTrainer' object.

Returns

A 'KNNTrainer' object.

Examples
data("iris")

iris$Species <- as.integer(as.factor(iris$Species))

xtrain <- iris[1:100,]
xtest <- iris[101:150,]

bst <- KNNTrainer$new(k=3, prob=TRUE, type="class")
bst$fit(xtrain, xtest, 'Species')
pred <- bst$predict(type="raw")

Method fit()

Usage
KNNTrainer$fit(train, test, y)
Arguments
train

data.frame or matrix

test

data.frame or matrix

y

character, name of target variable

Details

Trains the KNNTrainer model

Returns

NULL

Examples
data("iris")

iris$Species <- as.integer(as.factor(iris$Species))

xtrain <- iris[1:100,]
xtest <- iris[101:150,]

bst <- KNNTrainer$new(k=3, prob=TRUE, type="class")
bst$fit(xtrain, xtest, 'Species')

Method predict()

Usage
KNNTrainer$predict(type = "raw")
Arguments
type

character, 'raw' for labels else 'prob'

Details

Predits the nearest neigbours for test data

Returns

a list of predicted neighbours

Examples
data("iris")

iris$Species <- as.integer(as.factor(iris$Species))

xtrain <- iris[1:100,]
xtest <- iris[101:150,]

bst <- KNNTrainer$new(k=3, prob=TRUE, type="class")
bst$fit(xtrain, xtest, 'Species')
pred <- bst$predict(type="raw")

Method clone()

The objects of this class are cloneable with this method.

Usage
KNNTrainer$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
43
44
45
46
47
48
49
50
51
52
53
54
data("iris")

iris$Species <- as.integer(as.factor(iris$Species))

xtrain <- iris[1:100,]
xtest <- iris[101:150,]

bst <- KNNTrainer$new(k=3, prob=TRUE, type="class")
bst$fit(xtrain, xtest, 'Species')
pred <- bst$predict(type="raw")

## ------------------------------------------------
## Method `KNNTrainer$new`
## ------------------------------------------------

data("iris")

iris$Species <- as.integer(as.factor(iris$Species))

xtrain <- iris[1:100,]
xtest <- iris[101:150,]

bst <- KNNTrainer$new(k=3, prob=TRUE, type="class")
bst$fit(xtrain, xtest, 'Species')
pred <- bst$predict(type="raw")

## ------------------------------------------------
## Method `KNNTrainer$fit`
## ------------------------------------------------

data("iris")

iris$Species <- as.integer(as.factor(iris$Species))

xtrain <- iris[1:100,]
xtest <- iris[101:150,]

bst <- KNNTrainer$new(k=3, prob=TRUE, type="class")
bst$fit(xtrain, xtest, 'Species')

## ------------------------------------------------
## Method `KNNTrainer$predict`
## ------------------------------------------------

data("iris")

iris$Species <- as.integer(as.factor(iris$Species))

xtrain <- iris[1:100,]
xtest <- iris[101:150,]

bst <- KNNTrainer$new(k=3, prob=TRUE, type="class")
bst$fit(xtrain, xtest, 'Species')
pred <- bst$predict(type="raw")

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