gpt4 book ai didi

octave - 如何绘制线性回归成本函数的等高线图?

转载 作者:行者123 更新时间:2023-12-05 05:22:40 33 4
gpt4 key购买 nike

我正在关注 machine learning course在 Coursera 上,其中一个讲座提供了一个变量的线性回归成本函数的等值线图:

enter image description here

来源:https://www.coursera.org/learn/machine-learning/lecture/nwpe2/cost-function-intuition-ii

我认为从教育的角度来看,能够重现这张图表会很有用。我没有任何 Octave 音阶经验,因此我需要可以将其粘贴到 Octave 音阶命令窗口中的分步说明。

这里有人可以帮忙吗?


更新:

我得到了以下结果:

function cost = calc_cost (theta0, theta1)
x = 1:10;
y = x.*2;
cost = arrayfun( @(t0, t1) ( 1/(length(x)) * sum( ((t0 + t1*x) - y).^2 )), theta0, theta1);
endfunction

[xx, yy] = meshgrid( -3000:50:3000, -3000:50:3000) ;
zz = calc_cost(xx, yy);
contour(xx, yy, zz )

最佳答案

如果您无法重写成本函数使其接受矩阵输入,您可以使用 arrayfun:

function cost = calc_cost (theta0, theta1)
x = 1:10;
y = x.*2;
cost = ( 1/(length(x)) * sum( ((theta0 + theta1*x) - y).^2 ) );
endfunction

[x,y] = meshgrid (linspace(-5000,5000,20), linspace(-500,500,20));
z = arrayfun (@calc_cost, x, y);
contour (x, y, z)
print out.png

给予 print

关于octave - 如何绘制线性回归成本函数的等高线图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39776501/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com