我想在y=60处插入一个值,我期望的输出应该在0.27
我代码:
x = [4.75, 2.00, 0.850, 0.425, 0.250, 0.180, 0.150, 0.075]
y = [100, 94.5, 86.3, 74.1, 54.9, 38.1, 9.3, 1.7]
x.sort()
xi = np.interp(60, y, x)
输出:
4.75
预期输出:
0.27
您必须根据您的xp
(在您的情况下是y)对输入数组进行排序
import numpy as np
x = [4.75, 2.00, 0.850, 0.425, 0.250, 0.180, 0.150, 0.075]
y = [100, 94.5, 86.3, 74.1, 54.9, 38.1, 9.3, 1.7]
sort_idx = np.argsort(y)
x_sorted = np.array(x)[sort_idx]
y_sorted = np.array(y)[sort_idx]
xi = np.interp(60, y_sorted, x_sorted)
print(xi)
0.296484375
您必须sort()
两个列表,不仅是x坐标,而且是y坐标:
x.sort()
y.sort()
xi = np.interp(60, y, x)
print(xi)
## 0.296484375
对x数组进行排序,但不对y数组进行排序。文档(这里)说:
单调递增采样点的一维线性插值。
但是你有一个单调递减的函数
x = np.array([4.75, 2.00, 0.850, 0.425, 0.250, 0.180, 0.150, 0.075])
x.sort()
y = np.array([100, 94.5, 86.3, 74.1, 54.9, 38.1, 9.3, 1.7])
y.sort()
xi = np.interp(60, y, x)
print(xi)
返回0.296484375