插值法处理缺失值

2020-04-11  本文已影响0人  echolvan

常用插值法

线性插值

import numpy as np
from scipy.interpolate import interpld

x = np.array([1,2,3,4,5,8,9,10])  # 创建自变量x
y1 = np.array([2,8,18,32,50,128,162,200])  # 创建因变量y1
y2 = np.array([3,5,7,9,11,17,19,21])  # 创建因变量y2
LinearInsValue1 = interpld(x, y1, kind='linear')  # 线性插值拟合x, y1
LinearInsValue2 = interpld(x, y2, kind='linear')  # 线性插值拟合x, y2
print('当x为6,7时,使用线性插值y1为:', LinearInsValue([6,7]))
print('当x为6,7时,使用线性插值y2为', LinearInsValue([6,7]))

多项式插值

# 拉格朗日插值
from scipy.interpolate import lagrange

LaInsValue1 = lagrange(x, y1)
LaInsValue2 = lagrange(x, y2)
print('拉格朗日插值', LaInsValue1([6,7]))
print('拉格朗日插值', LaInsValue2([6,7]))

样条插值

from scipy.interpolate import spline
# 样条插值拟合x, y1
SplineInsValue1 = spline(x, y1, xnew=np.array([6,7]))
# 样条插值拟合x, y2
SplineInsValue2 = spline(x, y2, xnew=np.array([6,7]))
print(SplineInsValue1)
上一篇下一篇

猜你喜欢

热点阅读