简体   繁体   中英

Extracting data from spline interpolated curve in Python

I want to extract the data from the spline interpolated curve at the exact numbers in the x array. I calculated the log from the x array due to its scattered range.

I used UnivariateSpline (Spline interpolation) to fit a curve on my data. Now, how do I get a Y value for any given X value from that interpolated curve?

This is my code:

   import numpy as np
   import matplotlib.pyplot as plt
   from scipy.interpolate import UnivariateSpline
   import scipy.interpolate
   from scipy import stats

   
   x=np.log([22.0, 27.0, 35.0, 40.0, 61.0,
 71.0, 84.0, 118.0, 135.0, 175.0,
  223.0, 285.0, 350.0, 444.0, 565.0,
   702.0, 890.0, 1125.0, 1414.0, 1785.0,
    2249.0, 2832.0, 5658.0, 7138.0, 8988.0,
     11323.0, 14273.0, 17983.0, 22652.0])

   y=[63.91, 67.88, 55.57,
    82.9, 74.08, 22.27,
     24.57, 18.51, 19.87,
   16.22, 12.78, 10.42,
    9.3, 8.11, 6.55, 5.72,
     5.57, 7.0, 5.05, 5.45,
      13.76, 2.69, 0.8, 0.75,
       0.63, 0.59, 0.61, 0.68, 0.63]   

   ## creating weighing matrix ## Factor Investing = 2
   z_score=np.abs(stats.zscore(y))
   weight_spl=np.ones(len(x), dtype=int)
   i = 0
   for s in weight_spl:
       if z_score[i] > 2:
              weight_spl[i] = 0
       i=i+1
   
   ### Applying Spline    
   k=3 # poly-order
   spl = UnivariateSpline(x, y, w=weight_spl, s=100, k=k)
   xs  = np.linspace(x.min(), x.max(), len(x))
   plt.plot(x, y, 'ro', ms=5)
   plt.plot(xs, spl(xs), 'cyan', lw=2,alpha=0.3)
   plt.show()      

在此处输入图像描述

I searched many documents, but I couldn't find any solution.

This problem can be easily fixed by the following code:

print(spl(x))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM