## GitHub Source Code of FastKNN
k.nearest.neighbors2 <- function(i, distance_matrix, k = 5)
{
  ordered_neighbors <- order(distance_matrix[i, ]) 
  return(ordered_neighbors[1:(k)])
}
knn_test_function2 <- function(dataset, test, distance,labels, k = 3)
{
  if(ncol(dataset)!=ncol(test))
    stop("The test set and the training set have a different number of features")
  predictions <- rep(0, nrow(test))
  for (i in 1:nrow(test)){
    indices <- k.nearest.neighbors2(i, distance, k = k)
    vec_mas_cer <- table(labels[indices])
    predictions[i] <- sample(names(which(vec_mas_cer == max(vec_mas_cer))),size = 1)}
  return(predictions)}
knn_training_function2 <- function(dataset, distance,label, k = 1)
{
  if(isSymmetric(distance)==FALSE)
    stop("The distance Matrix is not symmmetric")
  predictions <- rep(0, nrow(dataset))
  # For every point in the dataset
  for (i in 1:nrow(dataset)){
    indices <- k.nearest.neighbors2(i, distance, k = k)
    vec_mas_cer <- table(label[indices])
    predictions[i] <- sample(names(which(vec_mas_cer == max(vec_mas_cer))), size = 1)}
  return(predictions)}
Distance_for_KNN_test2<-function(test_set, train_set)
{
  if(ncol(test_set)!=ncol(train_set))
    stop("The test set and the training set have a different number of features")
  distanciapost<-matrix(NA,nrow(test_set),nrow(train_set))
  for (i in 1:nrow(test_set)){
    distanciapost[i,]<-as.matrix(pdist(train_set,test_set[i,]))}
  return(distanciapost)
}
##
