KNN_Euc | R Documentation |
KNN_Euc function
KNN_Euc( X_test, X_pred, Y_test, K, method = c("Euc", "Man", "Min", "Cos"), pred_weights = FALSE, p = NULL )
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 |
r x 1 vector of predicted labels for the X_pred dataframe
## 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"
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.