关于在没有for循环的情况下,在Python中计算点阵列到线段之间的欧式距离和python没有for循环吗的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于php–如何在没有foreach循
关于在没有for循环的情况下,在Python中计算点阵列到线段之间的欧式距离和python没有for循环吗的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于php – 如何在没有foreach循环的情况下有效地将子数组作为主数组?、python numpy行向量矩阵之间的欧式距离计算、python – 我可以在没有循环的情况下清理一个numpy数组吗?、Python 计算Numpy向量之间的欧氏距离等相关知识的信息别忘了在本站进行查找喔。
本文目录一览:- 在没有for循环的情况下,在Python中计算点阵列到线段之间的欧式距离(python没有for循环吗)
- php – 如何在没有foreach循环的情况下有效地将子数组作为主数组?
- python numpy行向量矩阵之间的欧式距离计算
- python – 我可以在没有循环的情况下清理一个numpy数组吗?
- Python 计算Numpy向量之间的欧氏距离
在没有for循环的情况下,在Python中计算点阵列到线段之间的欧式距离(python没有for循环吗)
我正在寻找一个函数来计算具有两个坐标(x,y)和线段的点的numpy点数组之间的欧几里得距离。我的目标是使线段和10k点的结果在0.01秒内。
我已经找到了单点功能。但是运行for循环效率很低。
我还发现此函数可计算到无限线的距离:
def line_dists(points, start, end): if np.all(start == end): return np.linalg.norm(points - start, axis=1) vec = end - start cross = np.cross(vec, start - points) return np.divide(abs(cross), np.linalg.norm(vec))
它非常有效,我想对边界线采用类似的方法。
感谢您的帮助。
答案1
小编典典设置–测试点P
,端点A
和B
:
取的点积
P - A
与normalize(A - B)
以获得 签名 平行距离分量s
从A
。和B
和同样t
。取这两个数字的最大值和零,以得到钳制的平行距离分量。仅当点在线段的“边界”( Voronoi区域 ?)之外时,该值才会为非零。
使用叉积,与以前一样计算垂直距离分量。
使用毕达哥拉斯(Pythagoras)计算所需的最近距离(从
P
到的灰线A
)。
上面的代码是无分支的,因此很容易通过以下方式向量化numpy
:
def lineseg_dists(p, a, b): # TODO for you: consider implementing @Eskapp''s suggestions if np.all(a == b): return np.linalg.norm(p - a, axis=1) # normalized tangent vector d = np.divide(b - a, np.linalg.norm(b - a)) # signed parallel distance components s = np.dot(a - p, d) t = np.dot(p - b, d) # clamped parallel distance h = np.maximum.reduce([s, t, np.zeros(len(p))]) # perpendicular distance component, as before # note that for the 3D case these will be vectors c = np.cross(p - a, d) # use hypot for Pythagoras to improve accuracy return np.hypot(h, c)
php – 如何在没有foreach循环的情况下有效地将子数组作为主数组?
这是我的数组:
Array
(
[0] => Array
(
[same_key] => 1000
)
[1] => Array
(
[same_key] => 1001
)
[2] => Array
(
[same_key] => 1002
)
[3] => Array
(
[same_key] => 1003
)
)
我想在不使用foreach循环的情况下获得以下内容.这可能吗?
Array
(
[0] => 1000
[1] => 1001
[2] => 1002
[3] => 1003
)
有小费吗?
解决方法:
以下将做到这一点
$myArray = Array
(
0 => Array
(
'adfadf'=> 1000
),
1 => Array
(
'adfadf' => 1001
),
2 => Array
(
'adfadf' => 1002
),
3 => Array
(
'adfadf' => 1003
)
);
$myArray = array_map('current', $myArray));
python numpy行向量矩阵之间的欧式距离计算
我是Numpy的新手,我想问你如何计算向量中存储的点之间的欧式距离。
假设我们有一个numpy.array,每行是一个向量,并且是一个numpy.array。我想知道是否有可能计算所有点与该单点之间的欧几里得距离并将它们存储在一个numpy.array中。
这是一个接口:
points #2d list of row-vectorssinglePoint #one row-vectorlistOfDistances= procedure( points,singlePoint)
我们可以有这样的东西吗?或者是否可以使用一个命令将单个点作为其他点的列表,最后得到距离矩阵?
谢谢
答案1
小编典典虽然可以使用向量化,但使用numpy数组时,@ Karl的方法将相当慢。
更简单的方法就是这样做np.hypot(*(points -single_point).T)
。(转置假定点是Nx2数组,而不是2xN。如果是2xN,则不需要.T
。
但是,这有点难以理解,因此您可以像这样(用一些罐头示例数据…)更明确地将其写出:
import numpy as npsingle_point = [3, 4]points = np.arange(20).reshape((10,2))dist = (points - single_point)**2dist = np.sum(dist, axis=1)dist = np.sqrt(dist)
python – 我可以在没有循环的情况下清理一个numpy数组吗?
deltas = data [1:] – data [: – 1]
有了这个:
for i in range(len(deltas)): if deltas[i] < 0: deltas[i] = 0 if deltas[i] > 100: deltas[i] = 0
对于这个特殊的例子……有更好的方法来做清洁部分吗?
问题第二部分:如果清理规则比这个例子更复杂或更复杂,该怎么办?例如,我们可能只想将所有负数更改为零.或者,我们可能正在进行更复杂的映射.
解决方法
import numpy as np deltas=np.diff(data) deltas[deltas<0]=0 deltas[deltas>100]=0
也可能,而且更快一点
deltas[(deltas<0) | (deltas>100)]=0
Python 计算Numpy向量之间的欧氏距离
vector1 = np.array([1,2,3])
vector2 = np.array([4,5,6])
dist = numpy.sqrt(numpy.sum(numpy.square(vector1 - vector2 )))
或
dist = numpy.linalg.norm(vector1 - vector2 )
我们今天的关于在没有for循环的情况下,在Python中计算点阵列到线段之间的欧式距离和python没有for循环吗的分享已经告一段落,感谢您的关注,如果您想了解更多关于php – 如何在没有foreach循环的情况下有效地将子数组作为主数组?、python numpy行向量矩阵之间的欧式距离计算、python – 我可以在没有循环的情况下清理一个numpy数组吗?、Python 计算Numpy向量之间的欧氏距离的相关信息,请在本站查询。
本文标签: