Reputation: 15
I get the following warning while using scipy.optimize: RuntimeWarning: invalid value encountered in power To my understanding, this comes from the exp function from numpy but I cannot figure out how to solve this for my issue
I have looked up multiple solutions that have been proposed but none seems to work for my issue or I'm simply not proficient enough to be able to apply them to my specific problem
I'm trying to fit following data to a modified 3 parameter Weibull function
Modules:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
My data is the following:
k = np.array([0.19624784, 0.24164188, 0.25641914, 0.30762518, 0.35346055,
0.38394795, 0.43306334, 0.45042259, 0.48428853, 0.50895787,
0.54263082, 0.5795924 , 0.588977 , 0.59078306, 0.61547335,
0.66019107, 0.68048225, 0.67923856, 0.68521766, 0.72072055,
0.75214316, 0.74576285, 0.7398933 , 0.73672459, 0.76617277,
0.79304079, 0.79531024, 0.78652484, 0.81252188, 0.82150161,
0.83746662, 0.86754186, 0.85742784, 0.88497858, 0.88234556,
0.90162162, 0.93864255, 0.92455724, 0.92188063, 0.899652 ,
0.94256297, 0.97279928, 0.95750676, 0.9395713 , 0.94953204,
1. , 0.99591568, 0.97808785, 0.96963366, 0.97140757,
0.97813063, 0.95783083, 0.94360437, 0.91859959, 0.91154328,
0.90502759, 0.86196873, 0.85309616, 0.85776327, 0.85093222,
0.80796675, 0.79844975, 0.79033716, 0.78440427, 0.74708102,
0.72567559, 0.72527317, 0.71904074, 0.68425383, 0.65547696,
0.64717119, 0.63439336, 0.6291027 , 0.59739265, 0.58711217,
0.58749354, 0.56884387, 0.56339959, 0.54077923, 0.54585967,
0.54278178, 0.51729934, 0.51325348, 0.50718134, 0.51529603,
0.49221335, 0.4770514 , 0.47483486, 0.47918123, 0.46902945,
0.45048035, 0.44986787, 0.44836071, 0.44506315, 0.42633282,
0.41056783, 0.40685124, 0.40554987, 0.39086833, 0.37326042])
t = np.array([0.30110593, 0.30342784, 0.30574744, 0.30806935, 0.31039127,
0.31271318, 0.3150351 , 0.31735701, 0.31967893, 0.32200084,
0.32432276, 0.32664467, 0.32896659, 0.33129082, 0.33361274,
0.33593465, 0.33825657, 0.34057848, 0.3429004 , 0.34522231,
0.34754423, 0.34986382, 0.35218573, 0.35450765, 0.35682956,
0.35915148, 0.36147339, 0.36379531, 0.36611722, 0.36843914,
0.37076338, 0.37308529, 0.37540721, 0.37772912, 0.38005104,
0.38237295, 0.38469487, 0.38701446, 0.3893387 , 0.39165829,
0.3939802 , 0.39630212, 0.39862403, 0.40094595, 0.40326786,
0.40558978, 0.40791169, 0.41023361, 0.41255784, 0.41487976,
0.41720167, 0.41952359, 0.4218455 , 0.42416742, 0.42648933,
0.42881125, 0.43113084, 0.43345276, 0.43577467, 0.43809659,
0.4404185 , 0.44274042, 0.44506233, 0.44738425, 0.44970616,
0.45202808, 0.45435231, 0.45667423, 0.45899614, 0.46131806,
0.46363997, 0.46596189, 0.4682838 , 0.47060572, 0.47292531,
0.47524723, 0.47756914, 0.47989106, 0.48221297, 0.48453489,
0.4868568 , 0.48917872, 0.49150063, 0.49382487, 0.49614678,
0.4984687 , 0.50079061, 0.50311253, 0.50543444, 0.50775636,
0.51007595, 0.51239786, 0.51471978, 0.51704169, 0.51936361,
0.52168552, 0.52400744, 0.52632935, 0.52865127, 0.53097318])
The function to fit:
def weib_4_p(t,a,b,c,d):
return d*(a/b)*((t-c)/b)**(a-1)*np.exp(-((t-c)/b)**a)
Fitting and visualizing:
popt, pcov = curve_fit(weib_4_p, t, k, p0 = [1.2, 0.1, 0.3, 10], bounds=((1.1,0,0.1,0),(100,1,100, 1000)))
plt.plot(t, k, 'bo')
plt.plot(t, weib_4_p(t, *popt), 'r-')
plt.show()
Can someone help me to get rid of this error?
Upvotes: 1
Views: 5012
Reputation: 33147
This is not exactly an error but mostly a warning. This pop ups when you try to raise a negative number to a fraction power.
E.g. Try this: 1.055 * (np.array([-2.0]) ** (1.0 / 2.4)) - 0.055
In your code, due to the bounds this happens but there is no problem and the optimization finishes.
popt, pcov = curve_fit(weib_4_p, t, k, p0 = [1.2, 0.1, 0.3, 10], bounds=((1.1,0,0.1,0),(100,1,100, 1000)))
print(popt)
# [1.94400274 0.15991757 0.28674807 0.17655357]
print(pcov)
# [[ 2.16729151e-03 7.53415378e-05 -1.21896311e-04 -2.82748341e-05]
# [ 7.53415378e-05 4.76623815e-06 -5.38376253e-06 4.01774320e-07]
# [-1.21896311e-04 -5.38376253e-06 7.76347347e-06 8.65238209e-07]
# [-2.82748341e-05 4.01774320e-07 8.65238209e-07 2.07468076e-06]]
Upvotes: 2