เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การเติมค่าที่ขาดหายไป

เมื่อข้อมูลมีค่าที่ขาดหายไป จะเติมค่าเหล่านั้นได้อย่างไร?

ในแบบฝึกหัดนี้ จะได้ฝึกใช้วิธี interpolation ต่าง ๆ เพื่อเติมค่าที่ขาดหายไป พร้อมแสดงผลลัพธ์ในแต่ละครั้ง แต่ก่อนอื่น ต้องสร้างฟังก์ชัน (interpolate_and_plot()) ที่จะใช้สำหรับ interpolate ค่าที่ขาดหายไปและพล็อตกราฟ

ข้อมูล time series ชุดหนึ่งถูกโหลดไว้ใน DataFrame ชื่อ prices แล้ว

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Machine Learning สำหรับข้อมูล Time Series ใน 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()
แก้ไขและรันโค้ด