cdist | usable configuration management | Configuration Management library
kandi X-RAY | cdist Summary
kandi X-RAY | cdist Summary
cdist is a usable configuration management system. It adheres to the KISS principle and is being used in small up to enterprise grade environments. For more information have a look at homepage or at docs/src for manual in reStructuredText format.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of cdist
cdist Key Features
cdist Examples and Code Snippets
Community Discussions
Trending Discussions on cdist
QUESTION
I would like to find minimum distance of each voxel to a boundary element in a binary image in which the z voxel size is different from the xy voxel size. This is to say that a single voxel represents a 225x110x110 (zyx) nm volume.
Normally, I would do something with scipy.ndimage.morphology.distance_transform_edt (https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.morphology.distance_transform_edt.html) but this gives the assume that isotropic sizes of the voxel:
...ANSWER
Answered 2021-Jun-15 at 02:32Normally, I would do something with scipy.ndimage.morphology.distance_transform_edt but this gives the assume that isotropic sizes of the voxel:
It does no such thing! You are looking for the sampling=
parameter. From the latest version of the docs:
Spacing of elements along each dimension. If a sequence, must be of length equal to the input rank; if a single number, this is used for all axes. If not specified, a grid spacing of unity is implied.
The wording "sampling" or "spacing" is probably a bit mysterious if you think of pixels as little squares/cubes, and that is probably why you missed it. In most situations, it is better to think of pixels as point samples on a grid, with fixed spacing between samples. I recommend Alvy Ray's a pixel is not a little square for a better understanding of this terminology.
QUESTION
I have two numpy arrays: ones is some coordinates (x
, y
, z
) with an id and another one is only some coordinates (x
, y
, z
):
ANSWER
Answered 2021-Jun-10 at 08:55using np.where(np.min(X)) doesn't give you the correct answer as min returns the minimum value (rather than the index of the minimum value) and where will return all nonzeros. I think what you are looking for is argmin:
QUESTION
I would like to build a function npbatch(U,X) which compares data points in an input matrix (U) with data points in a training matrix (X) and gets me the index of X with the shortest euclidean distance to the data point in U. I would like to avoid any loops to increase the performance and I would like to use the function scipy.spatial.distance.cdist to compute the distance.
Example Input:
...ANSWER
Answered 2021-Apr-25 at 12:33With scipy.spatial.distance.cdist
you already chose a well-suited function for the task. To get the indices, we just have to apply numpy.argmin
along the axis 0 (or axis 1 for cdist(U, X)
):
QUESTION
I want to quickly (say, <1 ms) perform a nearest neighbor search by quadrant.
Input of each search:
- a fixed set of points in 2D space (black points in image). This remains constant across searches, so it can be stored in an efficient data structure. Number of points between 1 and 1000.
- a point whose location is different for each search (red point in image). Abstractly, this divides the 2D space into 4 quadrants (separated by red lines in image) much like the origin in a Cartesian coordinates.
Output of each search:
- black point from each red quadrant that is closest (circled blue in image) to the red point.
The output should usually be 4 points, one from each quadrant. But there are some possible edge cases:
- a quadrant to have no black points; collect no points for this quadrant
- a quadrant has multiple ties; collect all such points for this quadrant
- same point lies on boundaries (red lines) of quadrants: do not collect that point more than once
Things that I know won't work:
- nearest point along just one axis, say x, can be very far away (circled green in image)
- second closest point in a quadrant (circled purple in image) may be closer than points in the other quadrants. A simple search for the red point's 4 nearest neighbors will collect this point I don't want.
EDIT: version of accepted answer's code, and timings
...ANSWER
Answered 2021-Apr-22 at 09:38Updated Answer
I have modified the original answer to run under numba
. It now does a 1,000 point dataset in 3 microseconds - so considerably faster compared to the original 1 millisecond or so.
QUESTION
I have a list of coordinates a
. I want to specify a radius r
and then list all points on a 2D grid within the specified radius of any point in a
, together with the minimum distance of each of those grid-points to any point in a
. Since a
is substantial (~1000-2000 points) I want to be as efficient as possible.
My implementation so far uses this code to list all coordinates within the given radius of a point; then iterates that over all coordinates in a
; then flattens the output, takes a set (there will be MANY duplicates) - this is the set of coordinates within the specified radius of any point on the line - then calculates the minimum distances of that set to a
using scipy.spatial.distance.cdist
:
ANSWER
Answered 2021-Apr-15 at 17:56Let's inspect your implementation carefully. Notice that you are already poised and partially compute a distance metric in collect
.
- What if you made
neighborhood
adict
instead of alist
, keyed by grid point with a value of minimum distance. You could entirely eliminate the call toset
andcdist
. - If
a
can contain float values, you should your range fromint(coord[0] - rad)
toint(coord[0] + rad) + 1
.int(0.5 - 10)
is-9
, whileint(0.5) - 10
is-10
. - You can compare to the squared radius, so you don't need to take the square root except once for the final result.
Points 2 and 3 are relatively minor improvements.
Here is an example:
QUESTION
I have two numpy arrays both having coordinates of some points. One array has the coordinates of a cutting planar surface (cutting_surf
) and another one has coordinates of some points (points
):
ANSWER
Answered 2021-Mar-24 at 13:10If I understand correctly, you have already done the filtering by distance, so I will focus on the aspect of separating the "clean" points.
Please note: In your example, the six cutting_surf
points are on two parallel lines, hence they span a plane - if your surface is not a simple plane, this simple approach will not work.
Geometrically speaking:
- We determine a vector
orth
orthogonal to thecutting_surf
plane. - For each point
p
of thepoints
, we determine the dot product betweenorth
andp
relative to some point on the plane. The sign of the dot product is the "side" of the plane: All points with a positive sign are on one side, all points with a negative sign on the other. (And those with a zero dot product are on the plane.)
For the first part, we describe the cutting_surf
plane in terms of two normed vectors v1
and v2
, and the orthogonal vector o
is just the cross product of these. This is basically an adaptation of another Stackoverflow answer:
QUESTION
Given a dataframe df
as follows:
ANSWER
Answered 2021-Mar-04 at 09:22Idea is create mask for not 0
values and less like 5km
, then use DataFrame.dot
for matrix multiplication nas last use Series.str.split
for new columns joined to original:
QUESTION
I have a class component that calculates the Euclidean distance between the last elements in arrays within 2 dictionaries. One dictionary contents the tracking trajectory of blobs (r
), and the other have the updated values of the blobs(b
). The methods of the class find the appeared or disappeared trajectories based on Euclidean distance. Finally, they reorder the r
dictionary on their best match with the b
dictionary.
I tested the functionality in this colab notebook and it works as expected, but when I implement it on my code the program gets slow.
- Is there I way I can improve the speed of this class?
- Is there a better approach to solve this problem? What is it?
Thank you.
...ANSWER
Answered 2021-Feb-26 at 13:46Does this code actually work? These lines look totally wrong:
QUESTION
i have a dataframe which consists of 472 rows and 32 columns and it looks like this:
...ANSWER
Answered 2021-Jan-28 at 15:59The distance calculation function you seem to be looking for is the following:
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise_distances.html
You can set the metric to be any of the ones used for scipy.spatial.distance.pdist.
Example of how it would work:
QUESTION
Can someone help me please on how to generate a weighted adjacency matrix from a numpy array based on euclidean distance between all rows, i.e 0 and 1, 0 and 2,.. 1 and 2,...?
Given the following example with an input matrix(5, 4):
...ANSWER
Answered 2021-Jan-19 at 09:59Assuming a
is your Euclidean distance matrix, you can use np.argpartition
to choose n
min/max values per row. Keep in mind the diagonal is always 0 and euclidean distances are non-negative, so to keep two closest point in each row, you need to keep three min per row (including 0s on diagonal). This does not hold if you want to do max however.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install cdist
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page