knn: K Nearest Neighbours

Description Usage Arguments Value Examples

Description

It is the K Nearest Neighbours method

Usage

1
knn(X_train, Y_train, X_test, K)

Arguments

X_train

A Matrix of trainning observations.

Y_train

A numeric vector of classes or values of the trainning observations.

X_test

A Matrix of testing observations.

K

An integer as a parameter for the knn method.

Value

predicted labels

Examples

1
2
3
4
5
6
7
8
9
X <- as.matrix(cbind(runif(n = 100), runif(n = 100)))
pos <- sample(100, 70)
X_train <- X[pos, ]
X_test <- X[-pos, ]
Y_train <- as.numeric( X_train[, 1] ** 2 - X_train[, 2] > 0)
Y_test <- as.numeric(X_test[, 1] ** 2 - X_test[, 2] > 0)
K <- 5
Y_predicted <- knn(X_train = X_train, Y_train = Y_train, X_test = X_test, K = K)
print(table(Y_test, Y_predicted))

PauloCirino/MLAT documentation built on May 13, 2019, 1:22 p.m.