Reputation: 1265
I am trying to fit a curve into my data. my function is polynomial order 3 as bellow:
def objective3(x, a, b, c,d):
return a * x + b * x**2 + c * x**3 +d
y=center_1080[itr,:]
x=[1000 ,2000, 3000, 4000, 5000, 6000, 8000, 10000, 12000]
popt, pcov,info,msg, ier= curve_fit(objective3, x, y,full_output=True)
a, b, c ,d= popt
y
has the same shape as x
. after that I used the following code to find the new values based on this curve for y:
x_line4 = arange(min(x), max(x), 1)
# calculate the output for the range
y_line4 = objective3(x_line4, a, b, c,d)
suppose x_line4
value is as follows (shape (5,):
11995
11996
11997
11998
11999
when I use objective3(x_line4,a,b,c,d)
the output is:
66.4718
66.4732
66.4746
66.4759
66.4773
but when I use each element of x_line4 separately as input the output is different. for example objective3(11999,a,b,c,d)=81.11075844620781
but objective3(x_line4[4],a,b,c,d)=66.4773
!!!! what is the problem? a=0.003184157353698613,b=-2.2820353448818053e-07,c=8.475420387015893e-12,d=61.11802131658904
Upvotes: 0
Views: 57
Reputation: 1016
What you are experiencing is an int-overflow:
The type of x_line4[4]
is int32
which is limited to 2,147,483,648
. If you plug in 11999
instead, you still have an int that is in principle limited to the same range, but Python seems to perform automated adjustment of the int
from a int32
to an int64
or something alike under the hood (not sure here).
For the individual numbers, the easiest thing to do is to add x = float(x)
in the first row of your function objective3
. If you expect your function to handle both single numbers as well as numpy arrays, you will have to check whether it is an numpy array using something like this:
import numpy as np
def objective3(x, a, b, c,d):
if isinstance(x, np.ndarray):
x.astype(float)
else:
x = float(x)
return a * x + b * x**2 + c * x**3 +d
Alternatively, you could also ensure that all numpy arrays are float arrays instead.
Edit: Just for clarification: The part where the error occurs is the x**3
since that value is above the maximum value an int32
can hold.
Upvotes: 1