KNN: Supervised Machine Learning K Nearest Neighbours

KNNR Documentation

Supervised Machine Learning K Nearest Neighbours

Description

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.

Format

R6Class object.

Usage

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)

Methods

$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

Arguments

k

The Number of Nearest Neighbours to be considered while making a prediction

proba

It specified if the probability should be computed or not by default the labelled value will be returned, default=FALSE

algorithm

It specifies the algorithm which is used to fit the model, Options are 'kd_tree','cover_tree','brute'

type

It specifies the type of problem to be solved , for example a classification or regression problem , values to be specified are 'regr' or 'class'

Public fields

k

the number of neighbours to consider while predicting ::: 1 by default but must be specified

proba

must be set to True if probability needs to be computed ::: by default the value is set to FALSE

algorithm

used to train the model, the options are 'kd_tree','cover_tree','brute' ::: by default no algorithm is specified

type

of 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

Methods

Public methods


Method new()

Usage
KNN$new(k, proba, algorithm, type)
Arguments
k

k specidies the number of neighbours to be considering while making a prediction.

proba

proba specifies if probability ranging between 0-1 shall be computed,by default value is set to FALSE.

algorithm

algorithm specifies the algorithm or method used to fit the model, options include 'kd_tree','cover_tree', and 'brute'.

type

specifies the type of problem under study that needs to be solved for example a regression or a classification problem, options are 'regr' or 'class'.

Details

Create an instance of the new KNN object from the class.

Returns

A KNN object that can be used to call fit and predict methods.

Examples
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 fit()

Usage
KNN$fit(train_data, test_data, y)
Arguments
train_data

must be R data frame or matrix

test_data

must be a R data frame or matrix

y

Can be a character, name or value of target variable

Details

Trains the the K Nearest Neighbour KNN model

Returns

NULL

Examples
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 predict()

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

A Character, 'raw' for the exact labels else 'proba'

Details

Makes a Prediction based on the Nearest Neigbours for data

Returns

A list of predictions based on the neighbours

Examples
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 clone()

The objects of this class are cloneable with this method.

Usage
KNN$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples

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")

MalikShahidSultan/machinelearning documentation built on May 9, 2022, 8:32 p.m.