วิธีการประมาณค่า scipy interp1d fill_value = tuple ไม่ทำงาน

ฉันต้องการคาดการณ์ฟังก์ชันที่เหมาะสม scipy.interpolate.interp1d ควรจะสามารถทำได้ (ดูตัวอย่างเอกสาร) แต่ฉันได้รับ "ValueError: ค่าใน x_new อยู่ต่ำกว่าช่วงการแก้ไข"

การใช้: python 2.7.12, numpy 1.13.3, scipy 0.19.1

fill_value : array-like หรือ (array-like, array_like) หรือ "extrapolate" เป็นทางเลือก - หากเป็น ndarray (หรือ float) ค่านี้จะถูกนำมาใช้เพื่อกรอกข้อมูลสำหรับจุดที่ร้องขอนอกช่วงข้อมูล หากไม่ได้ระบุไว้ ค่าเริ่มต้นจะเป็น NaN อาร์เรย์ที่มีลักษณะคล้ายจะต้องออกอากาศอย่างถูกต้องตามขนาดของแกนที่ไม่สอดแทรก - หากเป็นสิ่งทูเปิลที่มีสององค์ประกอบ องค์ประกอบแรกจะถูกใช้เป็นค่าเติมสำหรับ x_new < x[0] และองค์ประกอบที่สองจะถูกใช้สำหรับ x_new > x[-1] สิ่งใดก็ตามที่ไม่ใช่ทูเพิลแบบ 2 องค์ประกอบ (เช่น list หรือ ndarray โดยไม่คำนึงถึงรูปร่าง) จะถือเป็นอาร์กิวเมนต์ที่มีลักษณะคล้ายอาเรย์เดี่ยวซึ่งมีไว้สำหรับใช้สำหรับขอบเขตทั้งสองเป็น below, above = fill_value, fill_value

import numpy as np
from scipy.interpolate import interp1d
# make a time series
nobs = 10
t = np.sort(np.random.random(nobs))
x = np.random.random(nobs)
# compute linear interp (with ability to extrapolate too)
f1 = interp1d(t, x, kind='linear', fill_value='extrapolate') # this works
f2 = interp1d(t, x, kind='linear', fill_value=(0.5, 0.6)) # this doesn't

person John Mahoney    schedule 21.10.2017    source แหล่งที่มา


คำตอบ (1)


ตามเอกสารประกอบ, interp1d มีค่าเริ่มต้นเป็นการเพิ่ม ValueError ในการคาดการณ์ ยกเว้นเมื่อ fill_value='extrapolate' หรือเมื่อคุณระบุ bounds_error=False

In [1]: f1 = interp1d(t, x, kind='linear', fill_value=(0.5, 0.6), bounds_error=False)

In [2]: f1(0)
Out[2]: array(0.5)
person Craig    schedule 21.10.2017
comment
ขอบคุณเครก ฉันคิดว่าการระบุค่า tuple ให้กับ fill_value จะทำให้มีการใช้ (อาจยกเว้นว่าคุณจะตั้งค่า bounds_error=False อย่างชัดเจนด้วยเหตุผลบางประการ) เห็นได้ชัดว่านี่ไม่ใช่พฤติกรรมของ interp1d - person John Mahoney; 22.10.2017