python - TypeError: ufunc 'isfinite' not supported for the input types. numpy - Stack Overflow
TypeError: ufunc 'isfinite' not supported with numpy 1.24.0
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
NAN Filtering while using Python Library
I want to do a parameter fit with the minimize function from lmfit
result = minimize(residuals_all_exp, p, args=(t_grid_online, y0_dict, c_dict, datasets_dict), method='leastsq', nan_policy= "omit")
p
is a parameter structure like this
p.add("viability", value=0.9, min=0.80, max=1.20 , vary = True) t_grid_online
is
[29.06150676 29.06290012 29.06429324 29.06568692 29.06707991 29.06847032 29.06986228 29.07125381 29.07264628 29.07403913 29.07543133 29.07682025]
y0_dict
is
{'current_fermentation': [1, 0.9141716, 0.0001, 0.5]} c_dict
is
{'current_fermentation': {'feed_on': 0, 'feed_rate': 0.0069, 'csf': 200, 'M_base': 5, 'gas_flow': 30, 'T': 32, 'wf_on': 1, 'wf_CO2': 1, 'pressure': 1058.0}} and
datasets_dict
is
{'current_fermentation': [ base_rate t 7.181545 0.081033, Empty DataFrame Columns: [cX, cS, cE] Index: [], t CO2 7.181545 5.0 I saw other posts which suggested to change the dtype to float, but I don´t know which one.
Full Traceback:
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in 1 start = time.time() 2 ----> 3 result = minimize(residuals_all_exp, p, args=(t_grid_online, y0_dict, c_dict, datasets_dict), method='leastsq', nan_policy= "omit") 4 5 end = time.time()
~\anaconda3\lib\site-packages\lmfit\minimizer.py in minimize(fcn, params, method, args, kws, iter_cb, scale_covar, nan_policy, reduce_fcn, calc_covar, max_nfev, **fit_kws) 2592
nan_policy=nan_policy, reduce_fcn=reduce_fcn, 2593
calc_covar=calc_covar, max_nfev=max_nfev, **fit_kws) -> 2594 return fitter.minimize(method=method)
~\anaconda3\lib\site-packages\lmfit\minimizer.py in minimize(self, method, params, **kws) 2361
val.lower().startswith(user_method)): 2362
kwargs['method'] = val -> 2363 return function(**kwargs) 2364 2365
~\anaconda3\lib\site-packages\lmfit\minimizer.py in leastsq(self, params, max_nfev, **kws) 1698 result.call_kws = lskws
1699 try: -> 1700 lsout = scipy_leastsq(self.__residual, variables, **lskws) 1701 except AbortFitException: 1702 pass
~\anaconda3\lib\site-packages\scipy\optimize\minpack.py in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag) 408 if not isinstance(args, tuple): 409 args = (args,) --> 410 shape, dtype = _check_func('leastsq', 'func', func, x0, args, n) 411 m = shape[0] 412
~\anaconda3\lib\site-packages\scipy\optimize\minpack.py in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape) 22 def _check_func(checker, argname, thefunc, x0, args, numinputs, 23 output_shape=None): ---> 24 res = atleast_1d(thefunc(*((x0[:numinputs],) + args))) 25 if (output_shape is not None) and (shape(res) != output_shape): 26 if (output_shape[0] != 1):
~\anaconda3\lib\site-packages\lmfit\minimizer.py in __residual(self, fvars, apply_bounds_transformation) 602 raise AbortFitException("fit aborted by user.") 603 else: --> 604 return _nan_policy(np.asarray(out).ravel(), 605 nan_policy=self.nan_policy) 606
~\anaconda3\lib\site-packages\lmfit\minimizer.py in _nan_policy(arr, nan_policy, handle_inf) 2422 if nan_policy == 'omit': 2423
mask locates any values to remove
-> 2424 mask = ~handler_func(arr) 2425 if not np.all(mask): # there are some NaNs/infs/missing values 2426
return arr[mask]
~\anaconda3\lib\site-packages\lmfit\minimizer.py in (x)
2416 2417 if handle_inf: -> 2418 handler_func = lambda x: ~np.isfinite(x) 2419 else: 2420 handler_func = isnull
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'