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

Interpolating missing data in Python keeping in mind x values

我需要说明使用什么工具以及如何在 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()

上面的代码产生下图

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

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

谢谢

我会删除与 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()

希望对您有所帮助!