Show confidence limits and prediction limits in scatter plot

前端 未结 3 621
温柔的废话
温柔的废话 2020-12-01 03:27

I have two arrays of data as hight and weight:

import numpy as np, matplotlib.pyplot as plt

heights = np.array([50,52,53,54,58,60,62,64,66,67,68,70,72,74,76         


        
3条回答
  •  臣服心动
    2020-12-01 03:44

    For a project of mine, I needed to create intervals for time-series modeling, and to make the procedure more efficient I created tsmoothie: A python library for time-series smoothing and outlier detection in a vectorized way.

    It provides different smoothing algorithms together with the possibility to computes intervals.

    In the case of linear regression:

    import numpy as np
    import matplotlib.pyplot as plt
    from tsmoothie.smoother import *
    from tsmoothie.utils_func import sim_randomwalk
    
    # generate 10 randomwalks of length 50
    np.random.seed(33)
    data = sim_randomwalk(n_series=10, timesteps=50, 
                          process_noise=10, measure_noise=30)
    
    # operate smoothing
    smoother = PolynomialSmoother(degree=1)
    smoother.smooth(data)
    
    # generate intervals
    low_pi, up_pi = smoother.get_intervals('prediction_interval', confidence=0.05)
    low_ci, up_ci = smoother.get_intervals('confidence_interval', confidence=0.05)
    
    # plot the first smoothed timeseries with intervals
    plt.figure(figsize=(11,6))
    plt.plot(smoother.smooth_data[0], linewidth=3, color='blue')
    plt.plot(smoother.data[0], '.k')
    plt.fill_between(range(len(smoother.data[0])), low_pi[0], up_pi[0], alpha=0.3, color='blue')
    plt.fill_between(range(len(smoother.data[0])), low_ci[0], up_ci[0], alpha=0.3, color='blue')
    

    In the case of regression with order bigger than 1:

    # operate smoothing
    smoother = PolynomialSmoother(degree=5)
    smoother.smooth(data)
    
    # generate intervals
    low_pi, up_pi = smoother.get_intervals('prediction_interval', confidence=0.05)
    low_ci, up_ci = smoother.get_intervals('confidence_interval', confidence=0.05)
    
    # plot the first smoothed timeseries with intervals
    plt.figure(figsize=(11,6))
    plt.plot(smoother.smooth_data[0], linewidth=3, color='blue')
    plt.plot(smoother.data[0], '.k')
    plt.fill_between(range(len(smoother.data[0])), low_pi[0], up_pi[0], alpha=0.3, color='blue')
    plt.fill_between(range(len(smoother.data[0])), low_ci[0], up_ci[0], alpha=0.3, color='blue')
    

    I point out also that tsmoothie can carry out the smoothing of multiple time-series in a vectorized way. Hope this can help someone

提交回复
热议问题