繁体   English   中英

scipy.interpolate输入值的问题

[英]scipy.interpolate problems with inputing values

当前尝试使用scipy的插值实现创建均匀的三次B样条曲线(钳位)。 当使用interpolate.splev()时,我传入的目标(x)值发生了变化,该函数向我返回了一个接近但与目标值不相同的点的x值(以及错误的x值的正确y值) 。 有人对我如何解决此问题有任何建议? 下面提供的代码可以重新创建问题。 提前非常感谢您:)

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) tck= splrep(x, y, s=0); splev(xnew, tck)make_interp_spline

要创建具有给定系数的splne对象,请使用BSpline(t, c, k)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM