python 函数插值总结

本文介绍了Python中使用Scipy库进行一维数据插值的方法,重点讨论了interp1d函数的用法。通过示例展示了如何进行插值并保存插值结果到CSV和XLSX文件,同时探讨了数据处理和限制插值点范围的技巧。
摘要由CSDN通过智能技术生成

数据插值方式总结

Scipy 一维数据插值

曲线最好能有函数关系

详见:

https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html
‘’’
class scipy.interpolate.interp1d(x, y, kind=‘linear’, axis=-1, copy=True, bounds_error=None, fill_value=nan, assume_sorted=False)[source]
Interpolate a 1-D function.

x and y are arrays of values used to approximate some function f: y = f(x). This class returns a function whose call method uses interpolation to find the value of new points.
‘’’

import numpy as np
from scipy import interpolate  # 插值 Class
import pylab as pl
import matplotlib.pyplot as plt
import pandas as pd
from pandas import  DataFrame

带有 显示函数关系的插值

plt.figure(1, figsize=(40, 20))
x=np.linspace(0,10,11)
#x=[  0.   1.   2.   3.   4.   5.   6.   7.   8.   9.  10.]
y=np.sin(x)
xnew=np.linspace(0,10,101)
plt.plot(x,y,"ro")

for kind in ["nearest","zero","slinear","quadratic","cubic"]:#插值方式
    #"nearest","zero"为阶梯插值
    #slinear 线性插值
    #"quadratic","cubic" 为2阶、3阶B样条曲线插值
    f=interpolate.interp1d(x,y,kind=kind)
    # ‘slinear’, ‘quadratic’ and ‘cubic’ refer to a spline interpolation of first, second or third order)
    ynew=f(xnew)
    plt.plot(xnew,ynew,label=str(kind))
plt.legend(loc="lower right",fontsize =30)
plt.show()
df = pd.read_excel('118ad.xlsx')             # 读取文件
test = DataFrame(df)
test

Activity41 Activity42 Activity43 Activity44 Activity45 Activity46 Activity47 Activity48 Activity49 Activity50 … Dose41 Dose42 Dose43 Dose44 Dose45 Dose46 Dose47 Dose48 Dose49 Dose50
0 0.000000 0.000000 0.000000 0.000000 0.000415 0.000000 0.000000 0.000000 0.000083 0.002710 … 0.164550 1.644300e-01 1.641900e-01 0.170860 0.175240 0.166360 0.161920 0.165450 0.166400 1.644000e-01
1 0.000000 0.000041 0.000428 0.000876 0.000000 0.000421 0.000809 0.000000 0.000000 0.000775 … 0.170490 1.704700e-01 1.704500e-01 0.176920 0.181820 0.172080 0.167950 0.171830 0.172660 1.696500e-01
2 0.000000 0.000820 0.000000 0.000042 0.000000 0.000842 0.000000 0.000000 0.000414 0.000000 … 0.172970 1.729800e-01 1.724400e-01 0.179670 0.184030 0.174890 0.170510 0.174370 0.175390 1.718500e-01
3 0.000453 0.000000 0.000039 0.000417 0.000000 0.000421 0.000000 0.000449 0.000000 0.000000 …

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值