Click here to Skip to main content
15,790,884 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to finds the Euclidean distance between the closest pair of points. The Euclidean distance between points (x1; y1) and  (x2; y2) is d((x1; y1); (x2; y2)) =�(𝑥𝑥2 − 𝑥𝑥1)2 + (𝑦𝑦2 − 𝑦𝑦1)2 2 Example, if L = [(3.0, 4.3), (9.2, 8.0), (1.0, 10.0), (5.2, 6.4), (8.5, 4.6), (6.7, 0.4), (2.5, 5.1), (1.9, 2.7)], then the closest pair are (3.0, 4.3) and (2.5, 5.1), as shown in the below figure.

the code give me this error
TypeError: tri() missing 1 required positional argument: 'N'

how can I fix it

What I have tried:

import numpy as np
def closetpair(L):
    def distance(L):
        for i in range(len(L)):
            for j in range(len(L)):
        distance2.resize((len(L), len(L)))
        return distance2
    for i in range(len(A)):
        for j in range(len(A)):
            if A[i,j]==np.min(c):
                print("the closet pair are:  ", L[i],L[j])
Updated 24-May-21 5:30am
Richard Deeming 24-May-21 7:59am    
Your error message refers to the tri function, but I can't see anywhere in your code that calls that function.
Richard Deeming 24-May-21 8:17am    
Nobody can help you to fix an error in code that we cannot see.

The error is in the part of the code where you call the np.tri function. You have not shown that part of the code.
OriginalGriff 24-May-21 8:20am    
How do you expect us to "fix" something you don't show us?
Read what Richard said: you do not call tri in the code you show us, but the error message refers to it!
OriginalGriff 24-May-21 9:07am    
Aaaaaand ... you don't call tri in there, either ...

1 solution

When I ran that code, giving it your sample data for L, I got NameError: name 'distance2' is not defined rather than an error from np.tri().

That's what I expected. The variable 'distance2' is not defined in your closetpair() function. It's a local variable inside the distance() function. No code outside of a function can see the that function's local variables.

Your main problem is that you are not calling the distance() function. Instead, you're trying to use a local variable from the function that you haven't called. Change the line that calls np.triu to:

When I run with that change, and with your sample data set for L, I get the answer you said you were expecting: (3.0, 4.3) (2.5, 5.1).

You've made the code difficult to read by defining a function inside of a function. That code in the distance function is only called once inside of closetpair(), so there's no need to define and call a function. Another fix would be to get rid of the def distance(L): statement and just include that code in closetpair(). (I think you meant for that to be "closestpair", but I've kept your spelling and only made changes needed to make the code work.)

If you're going to keep that distance() function, move it outside of closetpair(). Had you done that, you probably would have recognized the error message and made the needed change yourself.

As a last point, you're doing a lot of unneeded work. You are creating an NxN matrix with the distances between every pair of points from L, when you only need to compare unique pairs of different points. If you just rewrite that loop inside of your distance function, you don't need the array, you don't need any square roots at all, and you don't need numpy. The usual way to loop over just distinct pairs in an array looks something like:
for i in range(len(L) - 1):
    for j in range(i+1, len(L)):
        [do something with L[i] and L[j] here]

The result will be easier to code and get running, will run faster with less code and less data at runtime.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900