python: интерполировать со scipy, используя interp1d, не удалось для вида = 4 / четвертый - PullRequest
0 голосов
/ 11 февраля 2020

Я пытаюсь интерполировать между дискретными точками данных, используя функцию interp1d в scipy.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import numpy as np
import scipy
from scipy import interpolate
import matplotlib
import matplotlib.pyplot as plt

# ----- continuous function + sample data

x = np.linspace(1,40,15)
y = np.log(x)+0.5*np.sin(0.4*x)

xhi = np.linspace(1,40,10000)
yhi = np.log(xhi)+0.5*np.sin(0.4*xhi)

# ----- spline functions

f1 = scipy.interpolate.interp1d(x, y, kind=1)
yi1 = f1(xhi)

f2 = scipy.interpolate.interp1d(x, y, kind=2)
yi2 = f2(xhi)

f3 = scipy.interpolate.interp1d(x, y, kind=3)
yi3 = f3(xhi)

#f4 = scipy.interpolate.interp1d(x, y, kind=4) ## fails!!!
#yi4 = f4(xhi)

# ----- plot

plt.close('all')
fig1 = plt.figure(figsize=(1600/100,900/100))
plt.grid(which='major', axis='both', linestyle='--', linewidth=0.2, c='gray', alpha=0.5)
ax1 = fig1.gca()
ax1.plot( xhi, yhi, linestyle='--', linewidth=2.0, color='k', alpha=0.5, label=r'$\log(x)*0.5*\sin(0.4*x)$')
ax1.plot(xhi, yi1, linestyle='--', linewidth=1.0, color='blue', alpha=1.0, label='interp1d - O1')
ax1.plot(xhi, yi2, linestyle='--', linewidth=1.0, color='red', alpha=1.0, label='interp1d - O2')
ax1.plot(xhi, yi3, linestyle='--', linewidth=1.0, color='green', alpha=1.0, label='interp1d - O3')
#ax1.plot(xhi, yi4, linestyle='--', linewidth=1.0, color='purple', alpha=1.0, label='interp1d - O4') ## fails!!!
ax1.plot( x, y, marker='o', color='w', markeredgecolor='k', markersize=8.0, alpha=1.0, markeredgewidth=1.0, fillstyle='full', linestyle='none', label='interp pts')
ax1.legend(loc=4, fontsize='medium', ncol=1)
plt.show()

enter image description here

Когда я пытаюсь чтобы установить порядок сплайн-интерполяции на kind=4, я получаю ошибку:

Traceback (most recent call last):
  File "./test.py", line 32, in <module>
    f4 = scipy.interpolate.interp1d(x, y, kind=4)
  File "/home/steve/anaconda3/lib/python3.7/site-packages/scipy/interpolate/interpolate.py", line 533, in __init__
    check_finite=False)
  File "/home/steve/anaconda3/lib/python3.7/site-packages/scipy/interpolate/_bsplines.py", line 790, in make_interp_spline
    t = _not_a_knot(x, k)
  File "/home/steve/anaconda3/lib/python3.7/site-packages/scipy/interpolate/_bsplines.py", line 583, in _not_a_knot
    raise ValueError("Odd degree for now only. Got %s." % k)
ValueError: Odd degree for now only. Got 4.

Возможно ли выполнить сплайн-интерполяцию четвертого порядка (kind=4) в scipy в 1D?

1 Ответ

1 голос
/ 11 февраля 2020

Я отправил вопрос слишком быстро ... кажется, что это возможно с splrep и splev:

f4 = scipy.interpolate.splrep(x, y, k=4)
yi4 = scipy.interpolate.splev(xhi, f4)

Мне все еще немного любопытно относительно разницы между splrep / splev и interp1d в пересчете на состав, использованный для 1D сплайна.

...