title: K nearest neighbor pseudocode?
body: <p>so I am trying to code up the k nearest neighbor algorithm. The input to my function would be a set of data and a sample to classify. I am just trying to understand the workings of the algorithm. Can you guys tell me if this "pseudocode" of what I am trying to do is correct?</p>
  
  <pre><code>kNN (dataset, sample){
  
     1. Go through each item in my dataset, and calculate the "distance" from that data item to my specific sample.
     2. Out of those samples I pick the "k" ones that are most close to my sample, maybe in a premade array of "k" items?
  
  }
  </code></pre>
  
  <p>The part I get confused with is when I say "go through each item in my dataset". Should I be going through each CLASS in my dataset and finding the k-nearest neighbors? Then from there finding which one is closest to my sample, which then tells me the class?</p>
  
  <p>Part 2 question(ish), is using this algorithm but without a sample. How would I calculate the "accuracy" of the data set?</p>
  
  <p>I really am looking for broad word answers rather than specifics, but anything that helps me understand is appreciated. I am implementing this in R.</p>
  
  <p>Thanks</p>