Bù các giá trị bị thiếu
Khi có điểm dữ liệu bị thiếu, bạn có thể điền chúng như thế nào?
Trong bài tập này, bạn sẽ thực hành dùng các phương pháp nội suy khác nhau để điền một số giá trị bị thiếu và trực quan hóa kết quả sau mỗi lần. Nhưng trước hết, bạn sẽ tạo hàm (interpolate_and_plot()) dùng để nội suy các điểm dữ liệu bị thiếu và vẽ chúng.
Một chuỗi thời gian đã được nạp vào DataFrame có tên prices.
Bài tập này là một phần của khóa học
Machine Learning cho Dữ liệu Chuỗi thời gian bằng Python
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
# 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()