寻找最小空间距离
Finding minimual spatial distance
任务是找到这样一个坐标为 (x,0) 的点,使它与原始集合中最远的点(距离为欧氏距离)的距离最小。
我的想法是找到找到欧氏距离的函数的最小值,如下所示:
import matplotlib.pyplot as plt
from scipy.spatial.distance import cdist
from scipy.optimize import minimize
def function_3(points_x, points_y):
dots = np.array([points_x,points_y])
ans = minimize(cdist(dots,points1),x0=0)
return(ans)
但好像我做错了什么...有人可以给个建议吗?
解决方案
这里有一个完整的工作示例,用于拟合 (x, 0)
:
形式的点
from scipy.spatial.distance import cdist
from scipy.optimize import minimize
# set up a test set of 100 points to fit against
n = 100
xyTestset = np.random.rand(n,2)*10
def fun(x, xycomp):
# x is a vector, assumed to be of size 1
# cdist expects a 2D array, so we reshape xy into a 1x2 array
xy = np.array((x[0], 0)).reshape(1, -1)
return cdist(xy, xycomp).max()
fit = minimize(fun, x0=0, args=xyTestset)
print(fit.x)
输出:
[5.06807808]
这意味着,粗略地说,最小化正在按照预期找到一组随机测试点的质心。如果您想对 (x, y)
形式的点进行 2D 拟合,您可以这样做:
from scipy.spatial.distance import cdist
from scipy.optimize import minimize
# set up a test set of 100 points to fit against
n = 100
xyTestset = np.random.rand(n,2)*10
def fun(x, xycomp):
# x is a vector, assumed to be of size 2
return cdist(x.reshape(1, -1), xycomp).max()
fit = minimize(fun, x0=(0, 0), args=xyTestset)
print(fit.x)
输出:
[5.21292828 5.01491085]
这又大致是 xyTestset
中 100 个随机点的质心,如您所料。
完整解释
您 运行 遇到的问题是 scipy.optimize.minimize
对其第一个参数 fun
的形式有非常具体的期望。 fun
应该是一个以 x
作为第一个参数的函数,其中 x
是要最小化的值的一维向量。 fun
也可以接受额外的参数。这些必须通过 args
参数传递给最小化,并且它们的值是恒定的(即它们不会在最小化过程中改变)。
此外,您应该知道您的拟合案例 (x, 0)
可以简化。它实际上是一个一维问题,因此您需要做的就是计算点之间的 x 距离。您可以完全忽略 y 距离并仍然得到相同的结果。
此外,您不需要最小化来解决您陈述的问题。到最远点(相当于"minimizing the distance to all points")的距离最小的点就是质心。质心的坐标是点集中每个坐标的平均值,因此如果您的点存储在 Nx2 数组中 xydata
您可以通过以下方式计算质心:
xydata.mean(axis=1)
任务是找到这样一个坐标为 (x,0) 的点,使它与原始集合中最远的点(距离为欧氏距离)的距离最小。 我的想法是找到找到欧氏距离的函数的最小值,如下所示:
import matplotlib.pyplot as plt
from scipy.spatial.distance import cdist
from scipy.optimize import minimize
def function_3(points_x, points_y):
dots = np.array([points_x,points_y])
ans = minimize(cdist(dots,points1),x0=0)
return(ans)
但好像我做错了什么...有人可以给个建议吗?
解决方案
这里有一个完整的工作示例,用于拟合 (x, 0)
:
from scipy.spatial.distance import cdist
from scipy.optimize import minimize
# set up a test set of 100 points to fit against
n = 100
xyTestset = np.random.rand(n,2)*10
def fun(x, xycomp):
# x is a vector, assumed to be of size 1
# cdist expects a 2D array, so we reshape xy into a 1x2 array
xy = np.array((x[0], 0)).reshape(1, -1)
return cdist(xy, xycomp).max()
fit = minimize(fun, x0=0, args=xyTestset)
print(fit.x)
输出:
[5.06807808]
这意味着,粗略地说,最小化正在按照预期找到一组随机测试点的质心。如果您想对 (x, y)
形式的点进行 2D 拟合,您可以这样做:
from scipy.spatial.distance import cdist
from scipy.optimize import minimize
# set up a test set of 100 points to fit against
n = 100
xyTestset = np.random.rand(n,2)*10
def fun(x, xycomp):
# x is a vector, assumed to be of size 2
return cdist(x.reshape(1, -1), xycomp).max()
fit = minimize(fun, x0=(0, 0), args=xyTestset)
print(fit.x)
输出:
[5.21292828 5.01491085]
这又大致是 xyTestset
中 100 个随机点的质心,如您所料。
完整解释
您 运行 遇到的问题是 scipy.optimize.minimize
对其第一个参数 fun
的形式有非常具体的期望。 fun
应该是一个以 x
作为第一个参数的函数,其中 x
是要最小化的值的一维向量。 fun
也可以接受额外的参数。这些必须通过 args
参数传递给最小化,并且它们的值是恒定的(即它们不会在最小化过程中改变)。
此外,您应该知道您的拟合案例 (x, 0)
可以简化。它实际上是一个一维问题,因此您需要做的就是计算点之间的 x 距离。您可以完全忽略 y 距离并仍然得到相同的结果。
此外,您不需要最小化来解决您陈述的问题。到最远点(相当于"minimizing the distance to all points")的距离最小的点就是质心。质心的坐标是点集中每个坐标的平均值,因此如果您的点存储在 Nx2 数组中 xydata
您可以通过以下方式计算质心:
xydata.mean(axis=1)