Re: How to add an if statement in an objective function in scipy.optimize

Robert Kern <[email protected]> Tue, 22 Nov 2022 19:47:37 -0500
Newsgroups gmane.comp.python.scientific.user
Message-ID <CAF6FJivs8wJR0NU96Mv5BsuWQrCW8-6UNU8GJaKnZbWvAgbt2A@mail.gmail.com>
--===============1964601830678225658==
Content-Type: multipart/alternative; boundary="00000000000045625805ee18a469"

--00000000000045625805ee18a469
Content-Type: text/plain; charset="UTF-8"

On Tue, Nov 22, 2022 at 7:31 PM Zeina Abu-Aisheh <[email protected]>
wrote:

> Thanks a lot for your answer, your solution makes sense in case we only
> need to find the threshold, but our problem is more complex than that, as
> we want to optimise more than just one parameter, we are looking for
> something like this
>
> def objective(x, a, b):
>         k =  a * x + b
>         if x > threshold:
>            return 1
>         else:
>             return 0
> and so ideally we would like to optimise a, b and threshold.
> Is this possible in Scipy?
>

The objective function `f(xdata, *params)` is passed the full `xdata` array
and is expected to return an array `y` the same shape as `ydata` so that
`curve_fit()` can calculate `(y - ydata)**2`. It is not called once for
every `xdata[i]` scalar. That's why the `if x > threshold:` doesn't work.
Because `x` is an array, `x > threshold` returns a boolean array that is
True where `x[i] > threshold` and False otherwise. numpy chose to make
arrays themselves not have a truth value in order to catch errors like this.

Because `True==1` and `False==0`, you could just `return x > threshold`.
All in all, you probably don't want to implement this kind of
classification in this way. You will probably want to use logistic
regression (or similar form of generalized linear model), which is more
computationally tractable and statistically meaningful.

-- 
Robert Kern

--00000000000045625805ee18a469
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr">On Tue, Nov 22, 2022 at 7:31 PM Zeina Abu=
-Aisheh &lt;<a href=3D"mailto:[email protected]">zeina.abuaisheh@gm=
ail.com</a>&gt; wrote:<br></div><div class=3D"gmail_quote"><blockquote clas=
s=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid r=
gb(204,204,204);padding-left:1ex">Thanks a lot for your answer, your soluti=
on makes sense in case we only need to find the threshold, but our problem =
is more complex than that, as we want to optimise more than just one parame=
ter, we are looking for something like this<br>
<br>
def objective(x, a, b):<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 k =3D=C2=A0 a * x + b<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if x &gt; threshold:<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return 1<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 else:<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return 0<br>
and so ideally we would like to optimise a, b and threshold.<br>
Is this possible in Scipy?<br></blockquote><div><br></div><div>The objectiv=
e function `f(xdata, *params)` is passed the full `xdata` array and is expe=
cted to return an array `y` the same shape as `ydata` so that `curve_fit()`=
 can calculate=C2=A0`(y - ydata)**2`. It is not called once for every `xdat=
a[i]` scalar. That&#39;s why the `if x &gt; threshold:` doesn&#39;t work. B=
ecause `x` is an array, `x &gt; threshold` returns a boolean array that is =
True where `x[i] &gt; threshold` and False otherwise. numpy chose to make a=
rrays themselves not have a truth value in order to=C2=A0catch errors like =
this.</div><div><br></div><div>Because `True=3D=3D1` and `False=3D=3D0`, yo=
u could just `return x &gt; threshold`. All in all, you probably don&#39;t =
want to implement this kind of classification in this way. You will probabl=
y want to use logistic regression (or similar form of generalized linear mo=
del), which is more computationally tractable and statistically meaningful.=
</div><div><br></div></div>-- <br><div dir=3D"ltr" class=3D"gmail_signature=
">Robert Kern</div></div>

--00000000000045625805ee18a469--

--===============1964601830678225658==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
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]

--===============1964601830678225658==--