Using Scipy curve_fit with variable number of parameters to optimize

落爺英雄遲暮 提交于 2019-12-02 07:42:28

问题


Assuming we have the below function to optimize for 4 parameters, we have to write the function as below, but if we want the same function with more number of parameters, we have to rewrite the function definition.

def radius (z,a0,a1,k0,k1,):
    k = np.array([k0,k1,])
    a = np.array([a0,a1,])
    w   = 1.0
    phi = 0.0
    rs = r0 + np.sum(a*np.sin(k*z +w*t +phi), axis=1)
    return rs

The question is if this can be done easier in a more automatic way, and more intuitive than this question suggests.

example would be as following which has to be written by hand.

def radius (z,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,k0,k1,k2,k3,k4,k5,k6,k7,k8,k9,):
    k = np.array([k0,k1,k2,k3,k4,k5,k6,k7,k8,k9,])
    a = np.array([a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,])
    w   = 1.0
    phi = 0.0
    rs = r0 + np.sum(a*np.sin(k*z +w*t +phi), axis=1)
    return rs

来源:https://stackoverflow.com/questions/58463550/using-scipy-curve-fit-with-variable-number-of-parameters-to-optimize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!