15,745,014 members
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):
distance1=[]

def distance(L):
for i in range(len(L)):
for j in range(len(L)):
distance=np.sqrt((L[j][1]-L[i][1])**2+(L[j][0]-L[i][0])**2)
distance1.append(distance)
distance2=np.array(distance1)
distance2.resize((len(L), len(L)))
distance2
return distance2
A=np.triu(distance2)
c=[]
c.append(A[(A!=0.0)])
print(c)
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])```
Posted
Updated 24-May-21 4: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 ...

## Solution 1

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:
Python
```A=np.triu(distance(L))
```

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:
Python
```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.