scipy.interpolate 输入值问题
scipy.interpolate problems with inputing values
目前正在尝试使用 scipy 的插值实现来创建均匀的三次 B 样条(钳位)。当使用 interpolate.splev() 时,我传入的目标 (x) 值发生了变化,函数 returns 我得到了与目标值接近但不相同的点的 x 值(以及正确的 y 值对于错误的 x 值)。有人对我如何解决这个问题有任何建议吗?下面提供的代码可重现问题。非常感谢您:)
import numpy as np
import random as rd
from scipy import interpolate
import matplotlib.pyplot as plt
# The number of knots to form spline.
knots = 11
# Creating the x and y values for the knots.
x = np.linspace(0, 1, knots, endpoint=True)
y = np.array([rd.random() for i in range(knots)])
# Knot Vector.
t = np.linspace(0, 1, len(x) - 2, endpoint=True)
t = np.append([0, 0, 0], t)
t = np.append(t, [1, 1, 1])
# Cubic Spline definition.
spline = [t, [x, y], 3]
# =====================================================================
# X value I want to predict the y value off.
target = rd.random()
# Using spline evaluate to get the value of the "target".
prediction = interpolate.splev(target, spline)
print("X Value given to .splev:", target)
print("What .splev sees: x =", prediction[0], ", y =", prediction[1])
# =====================================================================
# Plotting the control points.
plt.plot(x, y, 'k--', label='Control Polygon', marker='o', markerfacecolor='red')
# Output used for display purposes only.
out = interpolate.splev(np.linspace(0, 1, 1000, endpoint=True), spline)
# Plotting the b-spline line.
plt.plot(out[0], out[1], 'b', linewidth=2.0, label='B-spline curve')
plt.grid(True)
plt.show()
更新代码:
import matplotlib.pyplot as plt
from scipy.interpolate import splev, splrep
import numpy as np
import random as rd
knots = 11
# X knots equidistant apart and Y random.
x = np.linspace(0, 1, knots, endpoint=True)
y = np.array([rd.random() for i in range(knots)])
# Creating the spline using slrep.
spline = splrep(x, y, s=0)
x2 = np.linspace(0, 1, 200)
y2 = splev(x2, spline)
# Plotting the a random point with target (x) and the predicted (y).
target = rd.random()
prediction = splev(target, spline)
plt.plot(target, prediction, 'o')
# Plotting the spline.
plt.plot(x, y, 'o', x2, y2)
plt.grid(True)
plt.show()
splev 接受结和系数的元组。要拟合数据,请使用 tck= splrep(x, y, s=0); splev(xnew, tck)
或 make_interp_spline
.
要创建具有给定系数的样条对象,请使用 BSpline(t, c, k)
目前正在尝试使用 scipy 的插值实现来创建均匀的三次 B 样条(钳位)。当使用 interpolate.splev() 时,我传入的目标 (x) 值发生了变化,函数 returns 我得到了与目标值接近但不相同的点的 x 值(以及正确的 y 值对于错误的 x 值)。有人对我如何解决这个问题有任何建议吗?下面提供的代码可重现问题。非常感谢您:)
import numpy as np
import random as rd
from scipy import interpolate
import matplotlib.pyplot as plt
# The number of knots to form spline.
knots = 11
# Creating the x and y values for the knots.
x = np.linspace(0, 1, knots, endpoint=True)
y = np.array([rd.random() for i in range(knots)])
# Knot Vector.
t = np.linspace(0, 1, len(x) - 2, endpoint=True)
t = np.append([0, 0, 0], t)
t = np.append(t, [1, 1, 1])
# Cubic Spline definition.
spline = [t, [x, y], 3]
# =====================================================================
# X value I want to predict the y value off.
target = rd.random()
# Using spline evaluate to get the value of the "target".
prediction = interpolate.splev(target, spline)
print("X Value given to .splev:", target)
print("What .splev sees: x =", prediction[0], ", y =", prediction[1])
# =====================================================================
# Plotting the control points.
plt.plot(x, y, 'k--', label='Control Polygon', marker='o', markerfacecolor='red')
# Output used for display purposes only.
out = interpolate.splev(np.linspace(0, 1, 1000, endpoint=True), spline)
# Plotting the b-spline line.
plt.plot(out[0], out[1], 'b', linewidth=2.0, label='B-spline curve')
plt.grid(True)
plt.show()
更新代码:
import matplotlib.pyplot as plt
from scipy.interpolate import splev, splrep
import numpy as np
import random as rd
knots = 11
# X knots equidistant apart and Y random.
x = np.linspace(0, 1, knots, endpoint=True)
y = np.array([rd.random() for i in range(knots)])
# Creating the spline using slrep.
spline = splrep(x, y, s=0)
x2 = np.linspace(0, 1, 200)
y2 = splev(x2, spline)
# Plotting the a random point with target (x) and the predicted (y).
target = rd.random()
prediction = splev(target, spline)
plt.plot(target, prediction, 'o')
# Plotting the spline.
plt.plot(x, y, 'o', x2, y2)
plt.grid(True)
plt.show()
splev 接受结和系数的元组。要拟合数据,请使用 tck= splrep(x, y, s=0); splev(xnew, tck)
或 make_interp_spline
.
要创建具有给定系数的样条对象,请使用 BSpline(t, c, k)