KNNTrainer | R Documentation |
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.
R6Class
object.
For usage details see Methods, Arguments and Examples sections.
bst = KNNTrainer$new(k=1, prob=FALSE, algorithm=NULL, type="class") bst$fit(X_train, X_test, "target") bst$predict(type)
$new()
Initialise the instance of the trainer
$fit()
trains the knn model and stores the test prediction
$predict()
returns predictions
number of neighbours to predict
if probability should be computed, default=FALSE
algorithm used to train the model, possible values are 'kd_tree','cover_tree','brute'
type of problem to solve i.e. regression or classification, possible values are 'reg' or 'class'
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
new()
KNNTrainer$new(k, prob, algorithm, type)
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'
Create a new 'KNNTrainer' object.
A 'KNNTrainer' object.
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")
fit()
KNNTrainer$fit(train, test, y)
train
data.frame or matrix
test
data.frame or matrix
y
character, name of target variable
Trains the KNNTrainer model
NULL
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')
predict()
KNNTrainer$predict(type = "raw")
type
character, 'raw' for labels else 'prob'
Predits the nearest neigbours for test data
a list of predicted neighbours
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")
clone()
The objects of this class are cloneable with this method.
KNNTrainer$clone(deep = FALSE)
deep
Whether to make a deep clone.
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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.