[英]How to find corresponding X value given a specified Y value in a plot
Currently implementing a momentum gradient descent, but I need to find the y value at the specific points x = 2.0000000052746745 for the first plot and x = 3.000000003516446 for the second plot目前正在实施动量梯度下降,但我需要在第一个 plot 的特定点 x = 2.0000000052746745 和第二个 plot 的 x = 3.000000003516446 处找到 y 值
def dz_dx(x,y):
return (x-2)/(np.sqrt(25-(x-2)**2-(y-3)**2))
def dz_dy(x,y):
return (y-3)/(np.sqrt(25-(x-2)**2-(y-3)**2))
xStart = 5
yStart = 5
learning_rate = 0.01
maxLimit = 10000
xStartHistory = np.zeros(maxLimit)
yStartHistory = np.zeros(maxLimit)
gamma = 0.9
update1 = 0
update2 = 0
for i in range(maxLimit):
xStartHistory[i] = xStart
yStartHistory[i] = yStart
dx = dz_dx(xStart, yStart)
dy = dz_dy(xStart, yStart)
update1 = (gamma * update1) + (learning_rate * dx)
update2 = (gamma * update2) + (learning_rate * dy)
xStart = xStart - update1
yStart = yStart - update2
print("xHistory:",xStartHistory[maxLimit-1])
print("yHistory:",yStartHistory[maxLimit-1])
figs, axs = plt.subplots(2)
axs[0].plot(xStartHistory)
axs[1].plot(yStartHistory)
You need some handler to get data from axes:您需要一些处理程序来从轴获取数据:
line0, = axs[0].plot(xStartHistory)
line1, = axs[1].plot(yStartHistory)
datax0 = line0.get_xdata()
datay0 = line0.get_ydata()
# value of y at x=2.000:
y_at_x0 = datay0[list(datax0).index(2.000)]
datax1 = line1.get_xdata()
datay1 = line1.get_ydata()
# value of y at x=3.000:
y_at_x1 = datay1[list(datax1).index(3.000)]
print(y_at_x0)
print(y_at_x1)
Further, if you need some interpolation over data you can then use numpy interpolation through griddata
[ link ].此外,如果您需要对数据进行一些插值,则可以通过
griddata
[ 链接] 使用 numpy 插值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.