Search code examples
pythonmatrixdistance

Python: Getting a Distance Matrix


I have a problem and would like your help. I have a class (Customers) in which all customers are stored with their respective x and y coordinates. I would now like to build a distance matrix or a two dimensional list that shows the distances between customers.

I already have a working function for the distances:

# Getting Distances
def get_distance(a,b):
    d = [a.getX()- b.getX() , a.getY() - b.getY()]
    return sqrt(d[0] * d[0] + d[1] * d[1])


# Distances between Customers:
Distances = []
for i in range(0,nr_customers):
    for j in range(0, nr_customers):
        get_distance(customer[i],customer[j])
        Distances.append(get_distance(customer[i],customer[j]))

So far, of course, only one list of all distances is output. Can someone help me? Please without Numpy or Pandas, we do not allow their use.


Solution

  • What you are missing in your code is the distance of customer i with all other customers(including i). You are not storing them in the inner loop as a list. You need to define it in your outer loop.

    # Getting Distances
    def get_distance(a,b):
        return sqrt((a.getX()- b.getX())**2 + (a.getY() - b.getY())**2)
    
    
    # Distances between Customers:
    Distances = []
    for i in range(0,nr_customers):
        distance_i = []
        for j in range(0, nr_customers):
            distance_i.append(get_distance(customer[i],customer[j]))
        Distances.append(distance_i)
    

    Now you have the distances between all customers as a 2D list.

    Although you could get the same result using scipy distance_matrix() :

    # importing distance_matrix
    from scipy.spatial import distance_matrix
    
    # making a list of customers coordinates
    Customer_Coordinates = [(customer[i].getX(),customer[i].getY()) for i in range(nr_customers)]
    
    # calculating the distance matrix
    d = distance_matrix(Customer_Coordinates,Customer_Coordinates)
    
    # printing the result
    print(d)