# Can someone help me me how to do this?

Hi I need to write a function knnOutliers(v, k, t) that takes a list argument v (which contain a list of points, where each point is a tuple containing two real-valued numbers), an integer argument k (corresponding to the number of neighbours of interest) and a positive real-valued argument t (specifying the threshold used to test whether a point is an outlier), and returns the list of indexes of the outlier points (if any) in the data sequence v. We can assume that k≥1, and that there are at least k+1 points in v. If our output list contains multiple output indexes, the indexes should appear in increasing order (see the third example below). If no outliers are detected, the function should return an empty list. We need to calculate the Euclidean distance between two points and sort the numbers using sort () :)). Thank you!!
Here is an example :
>>> print(knnOutliers([(2.0,2.1),(2.1,2.0),(2.0,2.0),(4.0,4.0),(4.1,4.2),(4.2,4.1),(3.1,2.9),(3.0,3.0)], 2, 1.0))
[6, 7]
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Just to be sure:

does k=1 mean you compare v[n] with v[n-1] and v[n+1]
and k=2 mean you compare v[n] with v[n-2], v[n-1], v[n+1], v[n+2]

to attack this problem decompose it into simple steps:

1 function to calculate distance between two points
1 function / generator to return you the sliding window with values (or indices) to compare with

which verion of Python are you using?
0
Commented:
Well I must have misunderstood something in your explanations:
Below the code as I understood it:
I wrote:
- one generator yielding an index, a point and it's neighbour points
- one function getting the max distance of a point and all its neighbour points
- the function to combine both above functions.

However the result is not what you seem to expect.
I get:
[1, 2, 3, 4, 5, 6, 7]

``````def idx_p_neighbours_it(v, k):
""" iterator yielding index, points and it's neighbours
"""
for i, p in enumerate(v):
neighbours = []
l = len(v)
for j in range(max(i-k,0) ,i):
neighbours.append(v[j])
for j in range(i+1,min(i+k+1, len(v))):
neighbours.append(v[j])
yield i, p, neighbours

def get_max_distance_square(p, neighbours):
""" returns max of euclidean distances
of a point and its neighbours
"""
max_dist_square = 0
x, y = p
for neigh in neighbours:
dx = x - neigh[0]
dy = y - neigh[1]
dist_square = dx*dx + dy*dy
print("P: %r N=%r" % (p,neigh)),
print("P: %4.2f^2 + %4.2f^2 == %4.1f" % (dx, dy, dist_square))
if dist_square > max_dist_square:
max_dist_square = dist_square
print("MAX = %r" % max_dist_square)
return max_dist_square

def knnOutliers(v, k, t):
tsquare = t*t
result = []
for i, p, neighbours  in idx_p_neighbours_it(v, k):
print("I:%r P:%r N:%r" % (i,p, neighbours))
max_dist_square = get_max_distance_square(p, neighbours)
if  max_dist_square > tsquare:
result.append(i)
return result

print(knnOutliers([(2.0,2.1),(2.1,2.0),(2.0,2.0),(4.0,4.0),(4.1,4.2),(4.2,4.1),(3.1,2.9),(3.0,3.0)], 2, 1.0))
``````
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

QA ManagerCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: gelonida (https:#a40953663)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Experts-Exchange Cleanup Volunteer
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.