KNN_Euc: KNN_Euc function

View source: R/KNN_Euc.R

KNN_EucR Documentation

KNN_Euc function

Description

KNN_Euc function

Usage

KNN_Euc(
  X_test,
  X_pred,
  Y_test,
  K,
  method = c("Euc", "Man", "Min", "Cos"),
  pred_weights = FALSE,
  p = NULL
)

Arguments

X_test

n x m numeric dataframe or matrix

X_pred

r x m numeric dataframe or matrix

Y_test

n x 1 column vector of test observation labels

K

integer value; the number of neighbors

method

Determines how distance will be calculated for nearest-neighbors. Choose from the following: "Euc": will calculate using the Euclidean Distance "Man": will calculate using the Manhattan Distance "Min": will calculate using the Minkowski Distance "Cos": will calculate using the Cosine-Similarity Distance

pred_weights

default is FALSE; if TRUE will calculate weights for predictions of K-nearest Neighbors by calculating the inverse of the distances of the K-nearest neighbors if Y_test is of numeric type.

p

takes values of positive integers or infinity (in the case of Sup Norm) to be used in the Minkowski Distance function if "Min" is chosen as distance method

Value

r x 1 vector of predicted labels for the X_pred dataframe

Examples

## Using the built-in iris dataframe

set.seed(1234)
n_fit = 10
train_ind = sample(1:nrow(iris),n_fit)
X_test = iris[-train_ind, -ncol(iris)]
X_pred = iris[train_ind, -ncol(iris)]

Y_test = iris[-train_ind, ncol(iris)]

KNN_Euc(X_test = X_test, X_pred = X_pred, Y_test = Y_test, K = 5)

## returns the following:
## [1] "setosa"   "versicolor"   "virginica"   "virginica"  "virginica"   "virginica"
## [7] "virginica"   "virginica"   "versicolor"   "virginica"


llrebecca21/ClusterPack documentation built on April 15, 2022, 4:37 a.m.