| KNN | R Documentation |
This is function is based on a popular K-Nearest Neighbour Algorith, and fits a k nearest neighbour model using fast search algorithms. KNN falls in the category of supervised machine learning algorithm which can be used for classification and regression tasks.
R6Class object.
For details of the Usage refer to the Method,Examples & Arguments sections of the Documentation.
best_model = KNN$new(k=1, proba=FALSE, algorithm=NULL, type="class") best_model$fit(X_train, X_test, "target") best_model$predict(type)
$new()Initialisization: This will initialize a new instance of the trainer
$fit()Fitting Model : By calling this method it trains the knn model and stores the predictions
$predict()Predictions : It returns the predictions previously stored when calling the fit Method
The Number of Nearest Neighbours to be considered while making a prediction
It specified if the probability should be computed or not by default the labelled value will be returned, default=FALSE
It specifies the algorithm which is used to fit the model, Options are 'kd_tree','cover_tree','brute'
It specifies the type of problem to be solved , for example a classification or regression problem , values to be specified are 'regr' or 'class'
kthe number of neighbours to consider while predicting ::: 1 by default but must be specified
probamust be set to True if probability needs to be computed ::: by default the value is set to FALSE
algorithmused to train the model, the options are 'kd_tree','cover_tree','brute' ::: by default no algorithm is specified
typeof problem to to be solved , the regression or classification type must be specified, options include 'regr' and 'class' ::: by default the value is set to class
new()KNN$new(k, proba, algorithm, type)
kk specidies the number of neighbours to be considering while making a prediction.
probaproba specifies if probability ranging between 0-1 shall be computed,by default value is set to FALSE.
algorithmalgorithm specifies the algorithm or method used to fit the model, options include 'kd_tree','cover_tree', and 'brute'.
typespecifies the type of problem under study that needs to be solved for example a regression or a classification problem, options are 'regr' or 'class'.
Create an instance of the new KNN object from the class.
A KNN object that can be used to call fit and predict methods.
data("iris")
iris$Species <- as.integer(as.factor(iris$Species))
x_train <- iris[1:110,]
x_test <- iris[111:150,]
best_model <- KNN$new(k=3, proba=TRUE, type="class")
best_model$fit(x_train, x_test, 'Species')
preds <- best_model$predict(type="raw")
fit()KNN$fit(train_data, test_data, y)
train_datamust be R data frame or matrix
test_datamust be a R data frame or matrix
yCan be a character, name or value of target variable
Trains the the K Nearest Neighbour KNN model
NULL
data("iris")
iris$Species <- as.integer(as.factor(iris$Species))
x_train <- iris[1:110,]
x_test <- iris[111:150,]
best_model <- KNN$new(k=3, proba=TRUE, type="class")
best_model$fit(x_train, x_test, 'Species')
predict()KNN$predict(type = "raw")
typeA Character, 'raw' for the exact labels else 'proba'
Makes a Prediction based on the Nearest Neigbours for data
A list of predictions based on the neighbours
data("iris")
iris$Species <- as.integer(as.factor(iris$Species))
x_train <- iris[1:110,]
x_test <- iris[111:150,]
best_model <- KNN$new(k=3, proba=TRUE, type="class")
best_model$fit(x_train, x_test, 'Species')
preds <- best_model$predict(type="raw")
clone()The objects of this class are cloneable with this method.
KNN$clone(deep = FALSE)
deepWhether to make a deep clone.
data("iris")
iris$Species <- as.integer(as.factor(iris$Species))
x_train <- iris[1:110,]
x_test <- iris[111:150,]
best_model <- KNN$new(k=3, proba=TRUE, type="class")
best_model$fit(x_train, x_test, 'Species')
preds <- best_model$predict(type="raw")
## ------------------------------------------------
## Method `KNN$new`
## ------------------------------------------------
data("iris")
iris$Species <- as.integer(as.factor(iris$Species))
x_train <- iris[1:110,]
x_test <- iris[111:150,]
best_model <- KNN$new(k=3, proba=TRUE, type="class")
best_model$fit(x_train, x_test, 'Species')
preds <- best_model$predict(type="raw")
## ------------------------------------------------
## Method `KNN$fit`
## ------------------------------------------------
data("iris")
iris$Species <- as.integer(as.factor(iris$Species))
x_train <- iris[1:110,]
x_test <- iris[111:150,]
best_model <- KNN$new(k=3, proba=TRUE, type="class")
best_model$fit(x_train, x_test, 'Species')
## ------------------------------------------------
## Method `KNN$predict`
## ------------------------------------------------
data("iris")
iris$Species <- as.integer(as.factor(iris$Species))
x_train <- iris[1:110,]
x_test <- iris[111:150,]
best_model <- KNN$new(k=3, proba=TRUE, type="class")
best_model$fit(x_train, x_test, 'Species')
preds <- best_model$predict(type="raw")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.