填补缺失值
当数据中存在缺失点时,如何将它们补上?
在本练习中,您将练习使用不同的插值方法来填补缺失值,并在每次填补后对结果进行可视化。不过,在此之前,您需要先编写一个函数(interpolate_and_plot()),用于对缺失数据点进行插值并绘图。
一个时间序列已加载到名为 prices 的 DataFrame 中。
本练习是课程的一部分
Python 中的时间序列机器学习
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create a function we'll use to interpolate and plot
def interpolate_and_plot(prices, interpolation):
# Create a boolean mask for missing values
missing_values = prices.____()
# Interpolate the missing values
prices_interp = prices.____(interpolation)
# Plot the results, highlighting the interpolated values in black
fig, ax = plt.subplots(figsize=(10, 5))
prices_interp.plot(color='k', alpha=.6, ax=ax, legend=False)
# Now plot the interpolated values on top in red
prices_interp[missing_values].plot(ax=ax, color='r', lw=3, legend=False)
plt.show()