Board logo

标题: [问题求助] [已解决]python多参数拟合 [打印本页]

作者: Liule    时间: 2020-7-16 12:30     标题: [已解决]python多参数拟合

函数z=(a*(y+273.15)+b+cx+d*(y+273.15)*LN(y+273.15)+e*(y+273.15)^2)/(8.3144*(y+273.15)*LN(10)), 现在有一些(x, y, z)的数据点(见附件),怎样拟合出最佳的参数 (a, b, c, d, e)?
作者: Blakelee    时间: 2020-7-16 15:37

回复 1# Liule
#!/usr/bin/env python3
from scipy.optimize import differential_evolution
from math import log
from math import e
import numpy as np

data = """1.0       300     -35.287
1.5     315     -34.047
2.0     330     -32.872
2.5     345     -31.757
====copy your data=====
5.0     880     -12.729
5.0     890     -12.547
5.0     900     -12.369"""

data = data.split("\n")
data = list(map(lambda x: filter(None, x.split(" ")), data))
data = map(lambda x: list(map(float, x)), data)
data = list(data)

def f(x, y, a, b, c, d, f):
    return (a * (y + 273.15) + b + c * x + d * (y + 273.15) * np.log(y+273.15) \
            + f * ((y+273.15) ** 2)) / (8.3144 * (y + 273.15) * log(10,e))

def fitness(parameters):
    error = 0
    for x, y, z in data:
        res = f(x, y, *parameters)
        error += abs(res - z) ** 2
    return error

from scipy.optimize import differential_evolution
bounds = [(-500000, 500000)] * 5
x = differential_evolution(fitness, bounds, disp=True).x
print(list(x))
作者: Liule    时间: 2020-7-16 15:44

谢谢,但你的结果和真实值[1584.427,-587474,1810,-203.3164,0.09271]有点差距,能否再改进一下?
作者: Blakelee    时间: 2020-7-16 17:42

根据计算结果缩小(a, b, c, d, f)各自的bound,直至f(x)的数值足够小(精度足够高)。




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2