Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8442171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:46:43+00:00 2026-06-10T08:46:43+00:00

Let me start by saying I have no experience with R, KNN or data

  • 0

Let me start by saying I have no experience with R, KNN or data science in general. I recently found Kaggle and have been playing around with the Digit Recognition competition/tutorial.

In this tutorial they provide some sample code to get you started with a basic submission:

# makes the KNN submission

library(FNN)

train <- read.csv("c:/Development/data/digits/train.csv", header=TRUE)
test <- read.csv("c:/Development/data/digits/test.csv", header=TRUE)

labels <- train[,1]
train <- train[,-1]

results <- (0:9)[knn(train, test, labels, k = 10, algorithm="cover_tree")]

write(results, file="knn_benchmark.csv", ncolumns=1) 

My questions are:

  1. How can I view the nearest neighbors that have been selected for a
    particular test row?
  2. How can I modify which of those ten is selected
    for my results?

These questions may be too broad. If so, I would welcome any links that could point me down the right road.

It is very possible that I have said something that doesn’t make sense here. If this is the case, please correct me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:46:45+00:00Added an answer on June 10, 2026 at 8:46 am

    1) You can get the nearest neighbors of a given row like so:

    k <- knn(train, test, labels, k = 10, algorithm="cover_tree")
    indices <- attr(k, "nn.index")
    

    Then if you want the indices of the 10 nearest neighbors to row 20 in the training set:

    print(indices[20, ])
    

    (You’ll get the 10 nearest neighbors because you selected k=10). For example, if you run with only the first 1000 rows of the training and testing set (to make it computationally easier):

    train <- read.csv("train.csv", header=TRUE)[1:1000, ]
    test <- read.csv("test.csv", header=TRUE)[1:1000, ]
    
    labels <- train[,1]
    train <- train[,-1]
    
    k <- knn(train, test, labels, k = 10, algorithm="cover_tree")
    indices = attr(k, "nn.index")
    
    print(indices[20, ])
    # output:
    #  [1] 829 539 784 487 293 882 367 268 201 277
    

    Those are the indices within the training set of 1000 that are closest to the 20th row of the test set.

    2) It depends what you mean by “modify”. For starters, you can get the indices of each of the 10 closest labels to each row like this:

    closest.labels = apply(indices, 2, function(col) labels[col])
    

    You can then see the labels of the 10 closest points to the 20th training point like this:

    closest.labels[20, ]
    # [1] 0 0 0 0 0 0 0 0 0 0
    

    This indicates that all 10 of the closest points to row 20 are all in the group labeled 0. knn simply chooses the label by majority vote (with ties broken at random), but you could choose some kind of weighting scheme if you prefer.

    ETA: If you’re interested in weighting the closer elements more heavily in your voting scheme, note that you can also get the distances to each of the k neighbors like this:

    dists = attr(k, "nn.dist")
    dists[20, ]
    # output:
    # [1] 1238.777 1243.581 1323.538 1398.060 1503.371 1529.660 1538.128 1609.730
    # [9] 1630.910 1667.014
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay let me start off by saying I have been looking around a lot
Let me start by saying that I have been reading the drag'n drop tutorial
Let me start by saying, I am new to Java programming. I have coded
Let me start out by saying I have tried this and looked at this
First let me start by saying that I don't have a complete understanding of
Let me start by saying I have looked at many similar questions asked, but
Let me start by saying I've looked and found descriptions of the use of
Let me start by saying I've been doing professional development work in C++ for
First off, let me start by saying, I know this exact question has been
Let me start by saying I am new to Android development but have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.