在线的发现点与其他点最小距离,而不是在线python
我已经使用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)
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论