从一组点来看,我得到了scipy.spatial带有Delaunay或的凸包ConvexHull(来自 qhull 库)。现在我想将这个凸包外的点投影到船体上(即船体上与外部点距离最小的点)。
这是我到目前为止的代码:
from scipy.spatial import Delaunay, ConvexHull
import numpy as np
hu = np.random.rand(10, 2) ## the set of points to get the hull from
pt = np.array([1.1, 0.5]) ## a point outside
pt2 = np.array([0.4, 0.4]) ## a point inside
hull = ConvexHull(hu) ## get only the convex hull
#hull2 = Delaunay(hu) ## or get the full Delaunay triangulation
import matplotlib.pyplot as plt
plt.plot(hu[:,0], hu[:,1], "ro") ## plot all points
#plt.triplot(hu[:,0], …Run Code Online (Sandbox Code Playgroud) 我编写了一个克里金算法,但我发现它很慢。特别是,您是否知道我如何在下面的 cons 函数中矢量化这段代码:
import time
import numpy as np
B = np.zeros((200, 6))
P = np.zeros((len(B), len(B)))
def cons():
time1=time.time()
for i in range(len(B)):
for j in range(len(B)):
P[i,j] = corr(B[i], B[j])
time2=time.time()
return time2-time1
def corr(x,x_i):
return np.exp(-np.sum(np.abs(np.array(x) - np.array(x_i))))
time_av = 0.
for i in range(30):
time_av+=cons()
print "Average=", time_av/100.
Run Code Online (Sandbox Code Playgroud)
编辑:奖金问题
corr(B[i], C[j])C 与 B 相同的维度,广播解决方案会发生什么如果我的 p 范数订单是一个数组,scipy 解决方案会发生什么:
p=np.array([1.,2.,1.,2.,1.,2.])
def corr(x, x_i):
return np.exp(-np.sum(np.abs(np.array(x) - np.array(x_i))**p))
Run Code Online (Sandbox Code Playgroud)
对于 2.,我尝试过,P = np.exp(-cdist(B, C,'minkowski', p))但 …