是否有一个python(或matlab)函数在给定的一组输出向量和一组计算出的向量之间达到最小MSE? [英] is there a python (or matlab) function that achieves the minimum MSE between given set of output vector and calculated set of vector?

查看:58
本文介绍了是否有一个python(或matlab)函数在给定的一组输出向量和一组计算出的向量之间达到最小MSE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些简单的问题要解决,想知道是否有更聪明/更好的方法来实现以下目标:

I have some simple problem to solve, and wonder if there is any smarter/better way to achieve the following:

这是场景.我有一组给定的向量'h(x)'和两个函数f(x,a)和g(x,b).这两个函数采用参数"a"和"b",并创建一维向量.现在,我想将给定的向量"h(x)"与通过添加f(x,a)和g(x,b)创建的向量进行比较.我的最终目标是找到在向量"h(x)"和向量f(x,a)+ g(x,b)之间产生最小MSE的自变量"a"和"b".

Here is the scenario. I have a set of given vector 'h(x)' and two functions, f(x,a) and g(x,b). These two functions take argument 'a' and 'b', and create 1-D vectors. Now, I want to compare the given vector 'h(x)' to a vector created by adding f(x,a) and g(x,b). my ultimate goal is finding the argument 'a' and 'b' that yields the minimum MSE between vector 'h(x)' and a vector of f(x,a)+g(x,b).

实现此目标的最佳方法是什么?

What would be the best way to achieve this?

我提出的当前解决方案是简单地将参数"a"和"b"扫描任意范围,手动计算MSE,然后找到达到最小MSE的"a"和"b".

The current solution I came up is simply sweeping the argument 'a' and 'b' for some arbitrary range, manually calculate the MSE, then find the 'a' and 'b' that achieves the minimum MSE.

我想知道是否有更好/更方便的方法.

I wonder if there is better/more convenient way to do this.

谢谢

推荐答案

您可以创建一个将a和b作为输入参数并输出拟合度(mse)的目标函数,然后使用内置的MATLAB中的一个函数使误差最小化以获得最佳a和b.我在下面快速创建了一个示例代码.您可以修改它.阅读有关fmincon的更多信息-它允许您在解决方案上施加约束等.

You can create an objective function that takes a and b as the input parameters and output a measure of fit (mse) and then use one of MATLAB in-built functions to minimise the error to get the best a and b. I have quickly create an example code below. You can modify it. Read more about fmincon - it allows you to put constraints on the solution etc.

function [a,b] = optimize_ab(x, h,f,g)
% h= h(x) should take vector x and yield vector output
% f=f(x,a)
% g=g(x,b)
% x: input vector
% ========================================================
% Example: find a function to approximate f=x^2 with a*x^1.5+b*x^2.5
% x=1:4;
% h(x)=x.^2;
% f=@(x,a)a*x.^1.5;
% g(x)=g=@(x,b)b*x.^2.5;
% [a,b] = optimize_ab(x,h,f,g)
%=========================================================
h_=h(x);
N =length(x);
res = fmincon(@MSE, [0,0], [], []);
a=res(1);
b=res(2);

    function mse = MSE(x_)
        a_=x_(1);
        b_=x_(2);
        mse =sqrt(sum((h_-(f(x,a_)+g(x,b_))).^2)/N);
    end
end

这篇关于是否有一个python(或matlab)函数在给定的一组输出向量和一组计算出的向量之间达到最小MSE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆