问题描述
记录今天看见代码里面用到scipy.interpolate.make_interp_spline()函数,主要学习https://blog.csdn.net/weixin_42782150/article/details/107176500 这篇博客的内容,记录一下自己的理解!
make_interp_spline是一种插值法,是一种曲线平滑处理的方法,下面看详细的代码实现:
import numpy as np
from matplotlib import pyplot as plt
from scipy.interpolate import make_interp_spline
# x->ndarrary(7), y->ndarrary(7)
x = np.array([6,7,8,9,10,11,12])
y = np.array([1.53E+03, 5.92E+02, 2.04E+02, 7.24E+01, 2.72E+01, 1.10E+01, 4.70E+00])
"""
np.linspace 等差数列,从min(x)到max(x)生成300个数,便于后续插值,这里需要注意,设置为多少,
那么后面x_smooth和y_smooth的维度也会对应变化,例如这里设置300,那么x_smooth->ndarrary(300),
y_smooth->ndarrary(300)
"""
x_smooth = np.linspace(x.min(), x.max(), 300)
y_smooth = make_interp_spline(x, y)(x_smooth)
plt.plot(x_smooth, y_smooth, label="later")
plt.plot(x, y, label='origin')
plt.legend()
plt.show()