Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Suppose you are trying to remove outliers from a data set consisting of points in Rd. One of the simplest approaches is to remove points
Suppose you are trying to remove outliers from a data set consisting of points in Rd. One of the simplest approaches is to remove points that are in "sparse" regions - that is, points that don't have many other points close by. To do this, we might calculate the distance from a point to it's k th closest neighbor. If this distance is above some threshold, we consider the point an outlier. More generally, the task of finding the distance from a query point to its k th closest "neighbor" is a common one in data science and machine learning. Here, we'll consider the 1-dimensional version of the 1 problem of finding k th neighbor distance. In a file named knn_distance.py, write a function named knn_distance (arr, q, k) that returns a pair of two things: - the distance between q and the k th closest point to q in arr; - the k th closest point to q in arr The query point q does not need to be in arr. For simplicity, arr will be a Python list of numbers, and q will be a number. k should start counting at one, so that knn_distance (arr, q,1 ) returns the distance between q and the point in arr closest to q. Your approach should have an expected time of (n), where n is the size of the input list. Your function may modify arr. In cases of a tie, the point you return is arbitrary (though the distance is not). Your code can assume that k will be len ( (arr). Example: > knn_distance ([3,10,52,15],19,1) (4,15) > knn_distance ([3,10,52,15],19,2, (9,10) > knn_distance ([3,10,52,15],19,3) (16,3) As this is a programming problem, submit your code to the Gradescope autograder
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started