python - 将 UnivariateSpline 与 SCIPY 结合使用

标签 python scipy interpolation

我正在尝试使用 UnivariateSpline 函数为某些数据(原始布拉格峰)生成拟合。这是我的代码:

import matplotlib.pyplot as plt
from scipy import interpolate
import numpy as np

spline = interpolate.UnivariateSpline(xi, yi, s = 0)
measured_depth_i = np.linspace(0.4, 10.4, 1000)
measured_dose_i = spline(measured_depth_i)

plt.plot(measured_depth, measured_dose, 'ro', label='Original Points') 
plt.plot(measured_depth_i, measured_depth_i, label='Interpolated Points' ) 

其中 xi =

[  0.4 ,   0.48,   0.63,   0.79,   0.93,   1.08,   1.22,   1.38,
         1.53,   1.68,   1.83,   1.99,   2.14,   2.29,   2.43,   2.59,
         2.73,   2.89,   3.04,   3.19,   3.34,   3.48,   3.64,   3.8 ,
         3.94,   4.09,   4.24,   4.39,   4.55,   4.7 ,   4.85,   5.  ,
         5.15,   5.3 ,   5.45,   5.59,   5.75,   5.9 ,   6.05,   6.21,
         6.35,   6.5 ,   6.66,   6.8 ,   6.95,   7.11,   7.24,   7.4 ,
         7.55,   7.71,   7.86,   8.  ,   8.16,   8.31,   8.46,   8.61,
         8.76,   8.91,   9.06,   9.22,   9.36,   9.52,   9.67,   9.81,
         9.97,  10.12,  10.27,  10.4 ]

和 yi =

[  2.16457961e+01,   2.16457961e+01,   2.16010733e+01,
         2.18470483e+01,   2.20483005e+01,   2.22942755e+01,
         2.24731664e+01,   2.26967800e+01,   2.29427549e+01,
         2.30545617e+01,   2.32781753e+01,   2.34570662e+01,
         2.36583184e+01,   2.37924866e+01,   2.39713775e+01,
         2.42620751e+01,   2.44856887e+01,   2.46869410e+01,
         2.49552773e+01,   2.51341682e+01,   2.53577818e+01,
         2.56932021e+01,   2.58497317e+01,   2.61404293e+01,
         2.63864043e+01,   2.65876565e+01,   2.68559928e+01,
         2.72361360e+01,   2.74821109e+01,   2.78622540e+01,
         2.81529517e+01,   2.84436494e+01,   2.88461538e+01,
         2.91815742e+01,   2.95617174e+01,   3.00089445e+01,
         3.04114490e+01,   3.09033989e+01,   3.13059034e+01,
         3.18872987e+01,   3.24463327e+01,   3.31395349e+01,
         3.37656530e+01,   3.44364937e+01,   3.51296959e+01,
         3.60017889e+01,   3.68515206e+01,   3.78801431e+01,
         3.91323792e+01,   4.04293381e+01,   4.19946333e+01,
         4.37164580e+01,   4.57513417e+01,   4.83228980e+01,
         5.16100179e+01,   5.58363148e+01,   6.13595707e+01,
         6.93202147e+01,   8.35420394e+01,   1.00000000e+02,
         8.47271914e+01,   3.60688730e+01,   6.21645796e+00,
         3.35420394e-01,   2.23613596e-02,   2.23613596e-02,
         2.23613596e-02,   2.23613596e-02]

我的插值曲线似乎仍然是一条直线,并且与我的数据完全不匹配,无论我的 s 值如何变化。谁能解释为什么我不能很好地适应我的数据?

最佳答案

诀窍是在绘图语句中正确获取变量名称:

spline = interpolate.UnivariateSpline(xi, yi, s = 0)
measured_depth_i = np.linspace(0.4, 10.4, 1000)
measured_dose_i = spline(measured_depth_i)

plt.plot(xi, yi, 'ro', label='Original Points')_
plt.plot(measured_depth_i, measured_dose_i, label='Interpolated Points' )_

结果图如下: enter image description here

关于python - 将 UnivariateSpline 与 SCIPY 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25151547/

相关文章:

python-2.7 - Pandas 插值返回 NaN

c++ - 机器精度

python - Python中如何快速得到线性规划的可行解?

javascript - Python - 如何在 JS 中导出 JSON

python - 如何更好地使用 Cython 更快地求解微分方程?

python - 如何 "stretch"我的数据帧并在现有值之间进行插值

Python正则表达式,如何替换段落中两个单词之间的文本?

Python Scikit 随机森林回归错误

python - 在 scikit dict vectorizer 中按特征名称提取特征

python - 在 seaborn 散点图中显示相关值