2016-09-29 129 views
1

我下面就Coursera的machine learning course和的一次演讲中提供了一个变量线性回归的成本函数的等高线图:如何绘制线性回归成本函数的等值线图?

enter image description here

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

我认为这从教育的角度来看,能够复制这张图表是有用的。我没有任何倍频程的经验,所以我需要一步一步的指导,我可以粘贴到八度命令窗口。

任何人都可以在这里帮助吗?


更新:

我结束了以下内容:

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) 
+0

看看'demo ezcontour'。您应该重写您的成本函数,以便接受theta0和theta1的矩阵输入 – Andy

+0

谢谢Andy。我在我的函数中使用了arrayfun的另一种解决方案。这是你的意思,让我的功能接受矩阵? –

回答

1

如果你不能够重写你的成本的Funktion,使其接受矩阵输入,你可以使用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