我在3D中有两点:
(xa, ya, za)
(xb, yb, zb)
Run Code Online (Sandbox Code Playgroud)
我想计算距离:
dist = sqrt((xa-xb)^2 + (ya-yb)^2 + (za-zb)^2)
Run Code Online (Sandbox Code Playgroud)
使用NumPy或Python的最佳方法是什么?我有:
a = numpy.array((xa ,ya, za))
b = numpy.array((xb, yb, zb))
Run Code Online (Sandbox Code Playgroud) 问题: 对于点位置,在Python中计算反距离加权(IDW)插值的最佳方法是什么?
一些背景: 目前我正在使用RPy2与R及其gstat模块进行交互.不幸的是,gstat模块与arcgisscripting冲突,我通过在单独的进程中运行基于RPy2的分析来解决这个问题.即使在最近/未来版本中解决了这个问题,并且效率可以提高,我仍然希望删除我对安装R的依赖.
gstat网站确实提供了一个独立的可执行文件,使用我的python脚本更容易打包,但我仍然希望Python解决方案不需要多次写入磁盘并启动外部进程.插值函数的调用次数,单独的点和值集,在我正在执行的处理中可以接近20,000.
我特别需要为点进行插值,因此在性能方面使用ArcGIS中的IDW函数生成比使用R更严重的栅格声音.....除非有办法有效地屏蔽掉我需要的点.即使进行了这种修改,我也不希望性能如此出色.我会将此选项作为另一种选择.更新:这里的问题是你绑定到您正在使用的单元格大小.如果减小单元大小以获得更好的准确性,则处理需要很长时间.如果你想要特定点的值,你还需要通过点数提取来跟进所有丑陋的方法.
我看过scipy文档,但看起来没有一种直接计算IDW的方法.
我正在考虑滚动我自己的实现,可能使用一些scipy功能来定位最近的点并计算距离.
我错过了一些明显的东西吗 是否有一个我没见过的python模块完全符合我的要求?在scipy的帮助下创建自己的实现是明智的选择吗?
我有一个n维点的集合,我想找到哪两个是最接近的.我可以为2个维度做的最好的是:
from numpy import *
myArr = array( [[1, 2],
[3, 4],
[5, 6],
[7, 8]] )
n = myArr.shape[0]
cross = [[sum( ( myArr[i] - myArr[j] ) ** 2 ), i, j]
for i in xrange( n )
for j in xrange( n )
if i != j
]
print min( cross )
Run Code Online (Sandbox Code Playgroud)
这使
[8, 0, 1]
Run Code Online (Sandbox Code Playgroud)
但这对于大型阵列来说太慢了.我可以应用什么样的优化?
有关:
我试图生成一个有效的代码,用于生成一些随机位置向量,然后我用它来计算一对相关函数.我想知道是否有直接的方法来设置我的框中任意两点之间允许的最小距离约束.
我的代码目前如下:
def pointRun(number, dr):
"""
Compute the 3D pair correlation function
for a random distribution of 'number' particles
placed into a 1.0x1.0x1.0 box.
"""
## Create array of distances over which to calculate.
r = np.arange(0., 1.0+dr, dr)
## Generate list of arrays to define the positions of all points,
## and calculate number density.
a = np.random.rand(number, 3)
numberDensity = len(a)/1.0**3
## Find reference points within desired region to avoid edge effects.
b = [s for s in …
Run Code Online (Sandbox Code Playgroud) 给定n维空间中的两组点,一个映射点如何从一组映射到另一组,以使每个点仅使用一次,并且使成对的点之间的欧式距离最小化?
例如,
import matplotlib.pyplot as plt
import numpy as np
# create six points in 2d space; the first three belong to set "A" and the
# second three belong to set "B"
x = [1, 2, 3, 1.8, 1.9, 3.4]
y = [2, 3, 1, 2.6, 3.4, 0.4]
colors = ['red'] * 3 + ['blue'] * 3
plt.scatter(x, y, c=colors)
plt.show()
Run Code Online (Sandbox Code Playgroud)
因此,在上面的示例中,目标是将每个红点映射到一个蓝点,以使每个蓝点仅使用一次,并且使两点之间的距离之和最小。
我碰上了这个问题,这有助于解决问题的第一部分-计算所有对点之间的距离跨越使用集scipy.spatial.distance.cdist()
功能。
从那里,我可能可以测试每一行中单个元素的每个排列,并找到最小值。
我想到的应用程序涉及3维空间中相当少量的数据点,因此蛮力方法可能很好,但是我想我先检查一下是否有人知道一种更有效或更优雅的解决方案。
在Matlab中存在pdist2
命令.给定矩阵mx2
和矩阵nx2
,每行矩阵代表一个2d
点.现在我想建立一个mxn
矩阵,使得(i,j)
元素表示从距离i
理论值的点mx2
矩阵j
TH的点nx2
矩阵.我只是调用命令pdist2(M,N)
.
我在python中寻找替代方案.我当然可以写2 for循环,但由于我使用2个numpy数组,使用for循环并不总是最好的选择.在python宇宙中是否有针对此的优化命令?基本上我要求python替代MATLAB pdist2
.
我有两个数组A
和B
。现在让它们都是一维的。
对于 中的每个元素,我需要与 中的元素最匹配的元素A
的索引。B
A
我可以使用列表表达式来解决这个问题
import numpy as np
A = np.array([ 1, 3, 1, 5 ])
B = np.array([ 1.1, 2.1, 3.1, 4.1, 5.1, 6.1 ])
indices = np.array([ np.argmin(np.abs(B-a)) for a in A ])
print(indices) # prints [0 2 0 4]
print(B[indices]) # prints [1.1 3.1 1.1 5.1]
Run Code Online (Sandbox Code Playgroud)
但这种方法对于巨大的数组来说确实很慢。
我想知道是否有更快的方法利用优化的 numpy 函数。
我有 2 组 2D 点(A 和 B),每组大约有 540 个点。我需要找到集合 B 中与 A 中所有点的距离超过定义距离 alpha 的点。
我有一个解决方案,但速度不够快
# find the closest point of each of the new point to the target set
def find_closest_point( self, A, B):
outliers = []
for i in range(len(B)):
# find all the euclidean distances
temp = distance.cdist([B[i]],A)
minimum = numpy.min(temp)
# if point is too far away from the rest is consider outlier
if minimum > self.alpha :
outliers.append([i, B[i]])
else:
continue
return outliers
Run Code Online (Sandbox Code Playgroud)
我正在使用带有 …
我有2个numpy数组(比如X和Y),每行代表一个点向量.
我想在X中的每个点到Y中的每个点之间找到平方的欧氏距离(将其称为'dist').
我希望输出为矩阵D,其中D(i,j)是dist(X(i) ),Y(j)).
我有以下python代码基于:http://nonconditional.com/2014/04/on-the-trick-for-computing-the-squared-euclidian-distances-between-two-sets-of-vectors/
def get_sq_distances(X, Y):
a = np.sum(np.square(X),axis=1,keepdims=1)
b = np.ones((1,Y.shape[0]))
c = a.dot(b)
a = np.ones((X.shape[0],1))
b = np.sum(np.square(Y),axis=1,keepdims=1).T
c += a.dot(b)
c -= 2*X.dot(Y.T)
return c
Run Code Online (Sandbox Code Playgroud)
我试图避免循环(我应该吗?)并使用矩阵mult来进行快速计算.但我在大型阵列上遇到"内存错误"的问题.也许有更好的方法来做到这一点?
python ×9
numpy ×8
scipy ×3
algorithm ×2
performance ×2
arrays ×1
correlation ×1
distribution ×1
indexing ×1
matrix ×1
random ×1
spatial ×1