寻找在Python中具有两个变量的函数的优化参数

huangapple go评论67阅读模式
英文:

Find optimised function parameters in python for function with two variables

问题

我是新来的,对Python也不熟悉。我意识到Python可以是优化曲线和拟合数据的强大工具,所以我在这里。我有一些关于VBA的经验,所以我对编程有一些基本的了解。我有一个问题,我会尽量描述得尽可能好。

我有一组直线,这些直线具有不同的斜率和不同的截距。每条直线代表第二个参数保持不变的条件。

举个例子:

  • 对于第一条直线:x1[0,1,2,3,4],y1[1,2,3,4,5]和z1[1,1,1,1,1],
  • 对于第二条直线:x2[0,1,2,3,4],y2[2,4,6,8,10]和z2[2,2,2,2,2],
  • 对于第三条直线:x3[0,1,2,3,4],y3[3,6,9,12,15]和z3[3,3,3,3,3]。

为了将这些变量表示为一个函数,我可以用z来表示斜率和截距,这样所有三条直线的组合函数将是:

y=(a1*z+b1)*x+(a2*z+b2),其中a1=1,b1=0,a2=1,b2=0。

从Python程序中是否可以获取a1、b1、a2和b2的值?

我以这个非常简单的示例来提问,因为实际上我有测量数据,因此数据通常会有一些波动。

我想要玩弄这个函数,以描述斜率和截距,以最佳方式拟合整个数据集。我知道我想要描述z的常数线性,我想要有最适合描述我的数据的通用函数。描述斜率的函数可以是线性的,也可以是其他任何函数。

我想要测试哪个拟合效果最好。例如,如果我更改斜率函数,那么在z1=const.的一个数据点集可能拟合效果不佳,而在z2=const.的另一个数据点集可能拟合效果更好,以此类推,以便最终整个数据集的误差最小。我希望这对你有意义。如果可能的话,我会很高兴如果你告诉我这是否可能以及我需要如何编写我的代码。

是否有限制可以拟合多少条直线?有时我有5条,有时有15条。

提前谢谢! 寻找在Python中具有两个变量的函数的优化参数

我尝试在Excel中获取最优化版本,但我认为会花费很长时间!而且视觉分析不如数学分析好,对我来说难以降低多条直线的误差。

英文:

i am new here and also new to Python. I realized that Python can be a powerful tool to optimize curves and fit data, so here I am. I have some experience with VBA, so i have some general knowledge about programming. I have a problem and I will try to describe it to you as good as possible.

I have a system of straight lines, which have different slopes and different intercepts. Each line represents a condition at which a second parameter is constant.

So for example:

  • x1[0,1,2,3,4] y1[1,2,3,4,5] and z1[1,1,1,1,1] for the first line,
  • x2[0,1,2,3,4] , y2[2,4,6,8,10] and z2[2,2,2,2,2] for the second line and
  • x3[0,1,2,3,4], y3[3,6,9,12,15] and z3[3,3,3,3,3] for the third line.

To express these variables in one function I can express the slope and the intercept in terms of z, so that the combined function for all three lines would be:

y=(a1*z+b1)*x+(a2*z+b2) with a1=1, b1=0, a2=1, b2=0

Is it possible to get the values for a1, b1, a2 and b2 from a Python program?

I ask with this very easy example, because in reality I have measured data and the data therefore normally fluctuate a bit.

I want to play around with the function to describe the slope and intercepts, To optimise for fitting the whole set of data best. I know I want to describe the lines of constant z as linear and i want to have the best fitting general function to describe my data. The function to describe the slopes can be linear or anything else.

I want to test, which fits best. If I change the slope function for example one set of data points at z1=const. will not fit so well, while another set with z2=const. will fit better and so on, so that I have the smallest error possible in the end for the whole data set. I hope that makes sense to you. I would be happy if you could tell me if this is possible at all and how I need to build my code.

Is there a limit of how many lines I can fit? Sometimes I have 5, sometimes 15.

Thank you in advance! 寻找在Python中具有两个变量的函数的优化参数

I tried to get the optimised version in Excel but I think it will take ages! Also the visual analysis is not as good as mathematical anaylsis and reducing the error in multiple lines is difficult for me.

答案1

得分: 1

这是一个相当简单的线性代数问题。以下是Python代码的翻译部分:

import numpy as np
from numpy.linalg import lstsq

x = np.array((0, 1, 2, 3, 4, 0, 1, 2, 3,  4, 0, 1, 2,  3,  4))
y = np.array((1, 2, 3, 4, 5, 2, 4, 6, 8, 10, 3, 6, 9, 12, 15))
z = np.array((1, 1, 1, 1, 1, 2, 2, 2, 2,  2, 3, 3, 3,  3,  3))

a = np.stack((x*z, z, x, np.ones_like(x)), axis=1)

params, *_ = lstsq(a, y)
print(params)
print(a @ params)
[ 1.00000000e+00  1.00000000e+00 -8.11406854e-16  3.35250086e-15]
[ 1.  2.  3.  4.  5.  2.  4.  6.  8. 10.  3.  6.  9. 12. 15.]

请注意,代码部分未进行翻译。

英文:

This is a quite simple linear algebra problem:

import numpy as np
from numpy.linalg import lstsq

'''
x1[0,1,2,3,4], y1[1,2,3,4,5]   and z1[1,1,1,1,1] for the first line,
x2[0,1,2,3,4], y2[2,4,6,8,10]  and z2[2,2,2,2,2] for the second line and
x3[0,1,2,3,4], y3[3,6,9,12,15] and z3[3,3,3,3,3]

y=(a1*z + b1)*x + (a2*z + b2) with a1=1, b1=0, a2=1, b2=0

[xz z x 1][a1] = [y]
[xz z x 1][a2]   [y]
[xz z x 1][b1]   [y]
[xz z x 1][b2]   [y]
[xz z x 1]       [y]
[...     ]       [...]
'''

x = np.array((0, 1, 2, 3, 4, 0, 1, 2, 3,  4, 0, 1, 2,  3,  4))
y = np.array((1, 2, 3, 4, 5, 2, 4, 6, 8, 10, 3, 6, 9, 12, 15))
z = np.array((1, 1, 1, 1, 1, 2, 2, 2, 2,  2, 3, 3, 3,  3,  3))

a = np.stack((
    x*z, z, x, np.ones_like(x),
), axis=1)

params, *_ = lstsq(a, y)
print(params)
print(a @ params)
[ 1.00000000e+00  1.00000000e+00 -8.11406854e-16  3.35250086e-15]
[ 1.  2.  3.  4.  5.  2.  4.  6.  8. 10.  3.  6.  9. 12. 15.]

huangapple
  • 本文由 发表于 2023年3月7日 16:12:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659420.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定