使用 scipy 和二进制(虚拟)变量进行建模

发布于 2025-01-17 22:42:33 字数 648 浏览 2 评论 0原文

我正在尝试用 Python 优化一个方程。该方程必须将性别视为二元变量。

到目前为止,我分别针对性别对方程进行建模(即,我针对男性进行了优化,然后针对女性进行了优化),这很好。

现在,我需要在一个方程中对它们进行建模。

由于男性和女性之间的参考数据不同,我不确定如何做到这一点,因为针对一种性别进行优化会导致另一种性别的结果恶化(正如您所期望的,因为参考不同)。

所以,我需要一个统一的模型,知道性别可以是1或0,并且无论是1还是0,参考值都会不同,并且将优化方程中的其余值来解释这一点(注:我原来的函数更长且更复杂,这一点值得一提,因为显然在这个简化的场景中它更容易处理)。

方程如下所示:

W(time|SEX) = G*(time) + Bi *(SEX)

我使用这个函数:

def BinaryVar(parameters, time):
    if sex == 'Male':
        sex_dummy = 0
    else:
        sex_dummy = 1
    return parameters[0]*time + parameters[1]*sex_dummy

然后使用 scipy 的最小二乘法进一步优化。参数是我想要优化的一个或多个参数的列表,时间是时间点的列表。

I am trying to optimize an equation in Python. The equation has to account for sex as a binary variable.

Until now, I modelled the equations separately for sex (i.e., I optimized for male, then optimized for female), and this was fine.

Now, I need to model them together in one equation.

Since the reference data differs between the males and females, I am not sure how I can do this because optimizing for one sex then worsens the results for the other (as you'd expect, since the references are different).

So, I need a unified model that knows sex can be 1 or 0, and the reference values will be different whether it is 1 or 0, and will optimize the rest of the values in the equation to account for this (Note: My original function is longer and more complicated. This is worth mentioning since obviously in this simplified scenario it's a lot easier to deal with).

The equation would look like this:

W(time|SEX) = G*(time) + Bi *(SEX)

I use this function:

def BinaryVar(parameters, time):
    if sex == 'Male':
        sex_dummy = 0
    else:
        sex_dummy = 1
    return parameters[0]*time + parameters[1]*sex_dummy

to then optimize using scipy's least squares further down. Parameters is a list or parameters I want to optimize and time is a list of time points.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文