Max Distance between 2 points in a data set and identifying the points
Question: I have a matrix consisting of x,y,z coordinates of several points. I would
like to find the extremum points i.e. the two points that are farthest apart.

I could figure out a way in matlab, but i need it in Python

Here is the code in matlab

    
    
    A = randint(500,3,[-5 5]);
    D=pdist(A);
    D=squareform(D);
    [N,I]=max(D(:));
    [I_row, I_col] = ind2sub(size(D),I);
    

pdist gives the distance between pairs of points(i,j). squareform gives the
matrix output In last two steps I attempt to find the indices of the matrix
I_row, I_col..

Points I_row and I_col have the max distance..

Could anybody suggest me an efficient way in python as all my other codes are
in Python.


Answer: If you have `scipy`, you have exact equivalent for most of matlab core
functions :

    
    
    from numpy import random, nanmax, argmax, unravel_index
    from scipy.spatial.distance import pdist, squareform
    
    A = random.randint(-5,5, (500,3))
    D = pdist(A)
    D = squareform(D);
    N, [I_row, I_col] = nanmax(D), unravel_index( argmax(D), D.shape )
    

You can also get it in pure python using `itertools` :

    
    
    from itertools import combinations
    from random import randint
    
    A = [[randint(-5,5) for coord in range(3)] for point in range(500)]
    
    def square_distance(x,y): return sum([(xi-yi)**2 for xi, yi in zip(x,y)])    
    
    max_square_distance = 0
    for pair in combinations(A,2):
        if square_distance(*pair) > max_square_distance:
            max_square_distance = square_distance(*pair)
            max_pair = pair
    

