在Python中插入丢失的数据时记住x值
我需要澄清使用什么工具以及如何在Python中插入缺失的内容。请参阅以下代码:在Python中插入丢失的数据时记住x值,python,pandas,interpolation,missing-data,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 # Interpolat
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值时,如何像使用scipy的interpolation.interp1d函数一样处理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()
希望这有帮助 谢谢你@Eric!是的,你的回答确实有用。干杯