2017-08-06 1 views
0

Ich versuche, den Code auszuführen:scipy.interpolate.pchip erhielt Fehler

x = [0, 1, 2, 3, 4, 5] 
y = [1, 2, 1.5, 2.5, 3, 2.5] 
xs = np.linspace(x[0],x[-1],100) 
curve = interpolate.pchip(x,y) 
ys = curve(xs) 
dys = curve.derivative(xs) 
pl.plot(xs,ys,label=u'pchip') 
pl.plot(xs,dys,label=u'derivative') 
pl.plot(x,y,'o') 
pl.legend(loc='best') 
pl.grid() 
pl.margins(0.1,0.1) 
pl.show() 

bekam aber einen Fehler wie folgt aus:

ValueError        Traceback (most recent call last) 
<ipython-input-61-256eb8fb78c2> in <module>() 
     4 curve = interpolate.pchip(x,y) 
     5 ys = curve(xs) 
----> 6 dys = curve.derivative(xs)#Construct a new piecewise polynomial 
representing the derivative. 
     7 pl.plot(xs,ys,label=u'pchip') 
     8 pl.plot(xs,dys,label=u'1 class diff') 

C:\Program Files\Anaconda3\lib\site-packages\scipy\interpolate\interpolate.py in derivative(self, nu) 
    1377 
    1378   """ 
-> 1379   if nu < 0: 
    1380    return self.antiderivative(-nu) 
    1381 

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 

Das System ich benutze, ist Windows-+ Python3.5 + Anaconda + Jupyter Notizbuch.

Vielen Dank.

Antwort

1

Die Art, wie ich die docs interpretieren, anstatt das zu tun:

dys = curve.derivative(xs) 

Sie tun sollten:

deriv = curve.derivative() 
dys = deriv(xs) 

# alternative 
# dys = curve.derivative()(xs) 

whiches gibt:

enter image description here

+0

Es workds. Vielen Dank! –

+1

Oder "curve (xs, 1)", um die ersten abgeleiteten Elemente zu bewerten. –

Verwandte Themen