在线的发现点与其他点最小距离,而不是在线python

发布于 2025-02-13 05:41:47 字数 1219 浏览 1 评论 0原文

我已经使用scipy.optimize.fmin_cobyla找到了图上点和线之间的最小距离。有没有办法获得fmin_cobyla返回该最小距离发生的线上的点(不是距离!)?当前,这是我的代码:

from matplotlib import pyplot as plt
from scipy.optimize import curve_fit, fmin_cobyla

P = (0.5, 2)

def logifunc(x,A,x0,k):
    return A / (1 + np.exp(-k*(x-x0)))


x, y = ecdf_xy(gene='Cd44', cluster_num=6, group='TREAT')
x[0] = 0

popt, pcov = curve_fit(logifunc, x, y, p0=[min(y), np.median(y), max(y)])

def f(x):
    return logifunc(x, *popt)

def objective(X):
    x,y = X
    return np.sqrt((x - P[0])**2 + (y - P[1])**2)

def c1(X):
    x,y = X
    return f(x) - y

X = fmin_cobyla(objective, x0=[0.5,0.5], cons=[c1])
print('The minimum distance is {0:1.2f}'.format(objective(X)))

fig, ax = plt.subplots()
plt.scatter(x, logifunc(x, *popt))
plt.plot(x, logifunc(x, *popt))
plt.plot(P[0], P[1], marker='o')

circle = plt.Circle(P, objective(X))
ax.add_patch(circle)

”在此处输入图像描述”

如果没有办法使用fmin_cobyla要这样做,我还如何找到与p最小距离的行上的点?那是我问题的主要目标。

当然,有关我的代码的任何其他评论或建议。

I have used scipy.optimize.fmin_cobyla to find the minimum distance between a point and a line on a graph. Is there a way to get fmin_cobyla to return the point on the line (not the distance!) at which that minimum distance occurs? Currently, this is my code:

from matplotlib import pyplot as plt
from scipy.optimize import curve_fit, fmin_cobyla

P = (0.5, 2)

def logifunc(x,A,x0,k):
    return A / (1 + np.exp(-k*(x-x0)))


x, y = ecdf_xy(gene='Cd44', cluster_num=6, group='TREAT')
x[0] = 0

popt, pcov = curve_fit(logifunc, x, y, p0=[min(y), np.median(y), max(y)])

def f(x):
    return logifunc(x, *popt)

def objective(X):
    x,y = X
    return np.sqrt((x - P[0])**2 + (y - P[1])**2)

def c1(X):
    x,y = X
    return f(x) - y

X = fmin_cobyla(objective, x0=[0.5,0.5], cons=[c1])
print('The minimum distance is {0:1.2f}'.format(objective(X)))

fig, ax = plt.subplots()
plt.scatter(x, logifunc(x, *popt))
plt.plot(x, logifunc(x, *popt))
plt.plot(P[0], P[1], marker='o')

circle = plt.Circle(P, objective(X))
ax.add_patch(circle)

enter image description here

If there isn't a way to use fmin_cobyla to do this, how else might I find the point on the line that lies the minimum distance from P? That is the main goal of my question.

Any other comments or suggestions about my code are of course welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文