S.Gou
S.Gou

Reputation: 37

How to compare each row of vectors of numpy array to itself and every element

I have a numpy array which contains vectorised data. I need to compare each of these vectors (a row in the array) euclidean distances to itself and every other row.

The vectors are of the form

[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]

I know I need two loops, here is what I have so far

def euclidean_distance_loop(termdoc):
    i = 0
    j = 0
    matrix = np.array([])
    while( j < (len(termdoc-1))):
        matrix = np.append(matrix,[euclidean_distance(termdoc[i],termdoc[j])])
        j = j + 1
        
    return np.array([matrix])

euclidean_distance_loop(termdoc)

I know this is an index problem and I need another index or an incremented index in another loop but not sure how to construct it

Upvotes: 0

Views: 962

Answers (1)

Jun
Jun

Reputation: 432

You don’t need loops.

def self_distance(x):
    return np.linalg.norm(x[:,np.newaxis] - x, axis=-1)

See also:

Upvotes: 2

Related Questions