我需要澄清使用什么工具以及如何在 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 值。
我的问题是:如何像 scipy 的 interpolation.interp1d 函数那样使用 x 值来处理 NaN 值?
谢谢
最佳答案
我会删除与 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()
希望这有帮助!
关于python - 在 Python 中插入缺失数据并记住 x 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49360576/