2017-11-18 1 views
1

Ich bin seltsam (aus meiner Sicht) Ergebnis von numpy polyfit. Mein Code:Seltsames Ergebnis von numpy polyfit

import numpy as np 
data=np.array([2482.9, 2483.0, 2485.9, 2486.0, 2486.4, 2485.1, 2485.4, 2484.9, 2484.8, 2484.8, 2484.8, 2484.0, 2484.1, 2484.1, 2484.1]) 
wr = range(len(data)) 
poly = np.polyfit(wr , data, deg = 2) 
wp = np.poly1d(poly) 
el = 2484.1 
res = wp(el) 
print(res) 
#result -225256.888955 

Ist das ein Fehler?

+0

Was bekommen Sie, und was erwarten Sie? – eyllanesc

+0

Haben Sie die Daten geplottet? Ist ein Polynom von 2 Grad anwendbar? – harandk

+0

Ich habe meine Daten in LibreOffice gezeichnet. Polynom 2 Grad ist in Ordnung. Was ich habe, können Sie in dem Code oben sehen: '#result -225256.888955'' – Prokhozhii

Antwort

1

Wie @DSM bereits gesagt hat - es sieht nicht wie ein quadratisches Polynom aus.

import numpy.polynomial.polynomial as poly 
x = wr; y = data 

coefs = poly.polyfit(x, y, 4) 
ffit = poly.Polynomial(coefs) 

plt.plot(x, y) 
plt.plot(x, ffit(x)) 
plt.legend(['y(x)','ffit(x)']) 

Ergebnis: obwohl

Wir können versuchen, es mit einem höheren Grad passen

enter image description here

+1

Ja, nur verwirrt x mit y. Heute ist nicht mein Tag :) Zu müde, um diesen Fehler zu bemerken. – Prokhozhii

+0

@Prokhozhii, ja, etwas Ruhe ;-) – MaxU