使用遗传算法进行曲线拟合

遗传算法可以做最优化,这是因为回归模型的算法关键是最优化,而遗传算法可以做最优化。

例如,把残差当成目标函数,形如 :

ming(a,b)=i=0n(f(xi;a,b)yi)2\min g(a,b)=\sum\limits_{i=0}^n (f(x_i;a,b)-y_i)^2

然后针对a,b 做优化

我们使用 scikit-opt来编程实现,需要安装 scikit-opt

随机生成训练数据

import numpy as np
import matplotlib.pyplot as plt
from sko.GA import GA

x_true = np.linspace(-1.2, 1.2, 30)
y_true = x_true ** 3 - x_true + 0.4 * np.random.rand(30)
plt.plot(x_true, y_true, 'o')

构造残差

def f_fun(x, a, b, c, d):
    return a * x ** 3 + b * x ** 2 + c * x + d


def obj_fun(p):
    a, b, c, d = p
    residuals = np.square(f_fun(x_true, a, b, c, d) - y_true).sum()
    return residuals

使用 scikit-opt 做最优化

ga = GA(func=obj_fun, n_dim=4, size_pop=100, max_iter=500,
        lb=[-2] * 4, ub=[2] * 4)

best_params, residuals = ga.run()
print('best_x:', best_params, '\n', 'best_y:', residuals)

画出拟合效果图

y_predict = f_fun(x_true, *best_params)

fig, ax = plt.subplots()

ax.plot(x_true, y_true, 'o')
ax.plot(x_true, y_predict, '-')

plt.show()

参考:

使用遗传算法进行曲线拟合

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章