Imputar valores perdidos
Cuando te faltan puntos de datos, ¿cómo puedes rellenarlos?
En este ejercicio, practicarás utilizando distintos métodos de interpolación para rellenar algunos valores que faltan,
visualizando el resultado cada vez. Pero antes, crearás la función (interpolate_and_plot()
) que utilizarás para interpolar los puntos de datos que faltan y trazarlos.
Se ha cargado una única serie temporal en un DataFrame llamado prices
.
Este ejercicio forma parte del curso
Machine learning para datos de series temporales en Python
Ejercicio interactivo práctico
Pruebe este ejercicio completando este código de muestra.
# 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()