title: How does one use KNN to predict something in R?
body: <p>How do we use KNN to make predictions? </p>
  
  <p>When we see examples of KNN algorithm (e.g.: <a href="https://www.kaggle.com/xvivancos/knn-in-the-iris-data-set#introduction" rel="nofollow noreferrer">k-Nearest Neighbors algorithm (k-NN) in the Iris data set</a> and <a href="https://www.analyticsvidhya.com/blog/2018/03/introduction-k-neighbours-algorithm-clustering/" rel="nofollow noreferrer">Introduction to k-Nearest Neighbors: A powerful Machine Learning Algorithm (with implementation in Python &amp; R)</a>) the algorithm is not being used to predict anything. My question is: let's suppose a have the <a href="https://www.kaggle.com/arshid/iris-flower-dataset" rel="nofollow noreferrer">iris dataset</a> (which already comes in R). Then, I run a KNN model so I will know the flowers species based on the sepal and petal information. Later, someone brings me a flower which specie is unknown. How can I predict the flower specie?</p>
  
  <p>This is the code I have so far:</p>
  
  <pre><code># Load libraries
  library(knitr)
  
  ### CHECK DATA
  ###############
  
  kable(head(iris))
  
  kable(summary(iris))
  
  ### NORMALIZE DATA
  ##################
  
  dataNorm &lt;- iris
  dataNorm[, -5] &lt;- scale(iris[, -5])
  
  ### DIVIDE DATA INTO TEST AND TRAIN
  ####################################
  set.seed(1234)
  
  # 70% train and 30% test
  ind &lt;- sample(2, nrow(dataNorm), replace=TRUE, prob=c(0.7,0.3))
  trainData &lt;- dataNorm[ind==1,]
  testData &lt;- dataNorm[ind==2,]
  
  ### EXECUTE KNN
  #################
  
  # Load the class package
  library("class")
  
  # Execution of k-NN with k=3
  KnnTestPrediction_k3 &lt;- knn(trainData[,-5], testData[,-5],
                          trainData$Species, k=3, prob=TRUE)
  </code></pre>
  
  <p>First, let's suppose k=3 gives me the best model.
  Now, I have a new flower with sepal.length=5, sepal.widht=4, petal.length=1.3, petal.width=0.35. How can I predict its specie based on my KNN model?</p>