How to add an if statement in an objective function in scipy.optimize
"Zeina Abu-Aisheh" <[email protected]> Tue, 22 Nov 2022 15:40:54 -0000
| Newsgroups | gmane.comp.python.scientific.user |
|---|---|
| Message-ID | <[email protected]> |
I have data (x) that represents some angles that looks like: [20, 30, 48, 46, 50, 70, 100], and a categorical ground truth which has two classes (0 and 1) e.g. [0, 0, 0, 0, 1, 1, 1]. I want to find a threshold that decides when an angle belongs to the class 0 or 1.
I was doing the following:
from scipy.optimize import curve_fit
def objective(x, threshold):
if x > threshold:
return 1
else:
return 0
# curve fit
threshold, _ = curve_fit(objective, x, y)
however, this code didn't work and I got this error:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
it seems that the problem was in having the if statement in the objective function, how could I use optimise to get the best threshold in such a case?
Thanks
_______________________________________________
SciPy-User mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/scipy-user.python.org/
Member address: [email protected]