实验6-1 拉格朗日插值法
题目描述:用拉格朗日插值法对missing_data.xls中表格的空值进行填补。
# p1, lab6
# Fill all of the null values with Lagrange's interpolation
# Data file name is "missing_data.xls"
import pandas as pd
from scipy.interpolate import lagrange
dir = 'F:/Data Mining/codes/ch6/lab6_1' # dir is a built-in name, will be shadowed if is distinctly defined
data = pd.read_excel(dir + '/data/missing_data.xls', header=None) # header=None indicates that the table does not have header
def lagrange_interpolate(s, n, k=5):
y = s[list(range(n-k, n)) + list(range(n+1, n+1+k))] # may create indexes out of bound, which are defined as null values
y = y[y.notnull()] # y.notnull() returns a Series object in boolean type
return lagrange(y.index, list(y))(n)
# method lagrange(x, w) in module scipy.interpolate
# param x is an array like object