在Python中插入缺失的数据,记住x值

时间:2018-03-19 10:23:09

标签: python pandas interpolation missing-data

我需要澄清使用什么工具以及如何在Python中插入缺失。请参阅以下代码:

import matplotlib.pyplot as plt
from scipy import interpolate

# Create data with missing y values
x = [i for i in range(0, 10)]
y = [i**2 + i**3 for i in range(0, 10)]
y[4] = np.nan
y[7] = np.nan

# Interpolation attempt 1: Use scipy's interpolate.interp1d
f = interpolate.interp1d(x, y)
ynew = f(x)

# Interpolate attempt 2: Use pandas.Series.interpolate
yp = pd.Series(y)
yp = yp.interpolate(limit_direction='both', kind='cubic')

plt.plot(x, y, 'o', x, ynew, '-', x, yp, 'x')

plt.show()

上面的代码产生了下图

Graph Plot

注意interp1d行(如文档所述)不处理NaN值。

我的问题是:如何在使用x值时处理NaN值,就像scipy的interpolation.interp1d函数一样?

由于

1 个答案:

答案 0 :(得分:1)

我会删除与NaN值相关联的值,并为剩余的值对开发模型,然后预测所有x。像这样:

# Create data with missing y values
x = [i for i in range(0, 10)]
y = [i**2 + i**3 for i in range(0, 10)]
y[4] = np.nan
y[7] = np.nan

# convert to numpy arrays
x = np.array(x)
y = np.array(y)

# drop NaNs
idx_finite = np.isfinite(y)
f_finite = interpolate.interp1d(x[idx_finite], y[idx_finite])
ynew_finite = f_finite(x)

# Interpolation attempt 1: Use scipy's interpolate.interp1d
f = interpolate.interp1d(x, y)
ynew = f(x)

# Interpolate attempt 2: Use pandas.Series.interpolate
yp = pd.Series(y)
yp = yp.interpolate(limit_direction='both', kind='cubic')

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y, 'o',label="true")
ax.plot(x, ynew, '-',label="interp1d")
ax.plot(x, ynew_finite, '--',label="interp1d finite")
ax.plot(x, yp, 'x',label="pandas")
plt.legend()
plt.show()

enter image description here

希望这有帮助!