对scipy.interpolate.make_interp_spline的理解

本文介绍了如何利用scipy库中的make_interp_spline函数进行数据插值和平滑处理。通过示例代码展示了如何创建等差数列并应用插值方法,最后用matplotlib绘制原始数据和插值后的平滑曲线。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

记录今天看见代码里面用到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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值