簡體   English   中英

從樣條插值曲線中提取數據 Python

[英]Extracting data from spline interpolated curve in Python

我想從 x 數組中精確數字的樣條插值曲線中提取數據。 由於其分散的范圍,我從 x 數組計算了對數。

我使用 UnivariateSpline(樣條插值)在我的數據上擬合曲線。 現在,如何從該插值曲線中獲得任何給定 X 值的 Y 值?

這是我的代碼:

   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()      

在此處輸入圖像描述

我搜索了很多文檔,但找不到任何解決方案。

這個問題可以通過下面的代碼輕松解決:

print(spl(x))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM