C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# Scipy Negative Distance? What?

By : mattycfp
Date : January 02 2021, 06:48 AM
it should still fix some issue This is because of floating-point inaccuracy, so some distances between your vectors, instead of being 0, are for example -0.000000000000000002. Use scipy.clip() function to correct the problem. If your distance matrix is dmatr, use numpy.clip(dmatr,0,1,dmatr) and you should be ok.
code :

Share :

## Negative results occurs when calculate cross-track distance(distance between line segment and point on sphere)

By : Jordav
Date : March 29 2020, 07:55 AM
Hope that helps It can happen if the coordinates of (x,y) are in fact values under (x",y"). That doesn't mean that the value is negative, but that the substraction is always made in the same order:
code :
(x-x"), (y-y").

## finding the distance between a set of points using scipy.spatial.distance.cdist(X, Y) in python

By : Devanopy
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Two solutions:
calculate the complete matrix directly, and the access the q-th column for the values between A and B[q].
code :
d = scipy.spatial.distance.cdist(A,B)

for q in range(len(B)):
y = d[:,q]
print y
for q in range(len(B)):
y = scipy.spatial.distance.cdist(A,[B[q]])
print y

## python how to get proper distance value out of scipy condensed distance matrix

By : Carlo Monti
Date : March 29 2020, 07:55 AM
should help you out This vector is in condensed form. It enumerates all pairs of indices in a natural order (in your example 0,1 0,2 0,3 0,4 1,2 1,3 1,4 2,3 2,4 ) and yields the distance between the elements at these array entries.
There is also the squareform function, which transforms the condensed form into a square matrix form (and vice versa). The square matrix form is exactly what you expect, i.e. at entry ij (row i, column j), it stores the distance between the i-th and j-th entry. For example, if you add print squareform(d) at the end of you code, the output will be:
code :
array([[ 0.,  3.,  1.,  4.],
[ 3.,  0.,  4.,  1.],
[ 1.,  4.,  0.,  5.],
[ 4.,  1.,  5.,  0.]])

## Distance transform with Manhattan distance - Python / NumPy / SciPy

By : Thomas
Date : March 29 2020, 07:55 AM
help you fix your problem Here's one with Scipy cdist -
code :
from scipy.spatial.distance import cdist

def bwdist_manhattan(a, seedval=1):

out = np.zeros(a.shape, dtype=int)
out[tuple(nz.T)] = cdist(z, nz, 'cityblock').min(0).astype(int)
return out
In [60]: a # input binary image with 1s at "seed" positions
Out[60]:
array([[1, 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, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0]])

In [61]: bwdist_manhattan(a)
Out[61]:
array([[0, 1, 2, 3, 4, 4, 3, 4],
[1, 2, 3, 4, 4, 3, 2, 3],
[2, 3, 4, 4, 3, 2, 1, 2],
[3, 4, 4, 3, 2, 1, 0, 1],
[4, 5, 5, 4, 3, 2, 1, 2]])

## Scipy Sparse - distance matrix (Scikit or Scipy)

By : Waleed Ibrahim
Date : March 29 2020, 07:55 AM
it should still fix some issue First, you can't use KDTree and pdist with sparse matrix, you have to convert it to dense (your choice whether it's your option):