1.题目来源:
吴恩达机器学习week2作业
2.代码内容(写在文档里)
function J = computeCost(X, y, theta)
%COMPUTECOST Compute cost for linear regression
% J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
% parameter for linear regression to fit the data points in X and y
% Initialize some useful values
m = length(y); % number of training examples
% You need to return the following variables correctly
J = 0;
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
% You should set J to the cost.
predictions=X*theta;
sqrErrors=(predictions-y).^2;
J=1/(2*m)*sum(sqrErrors);
% =========================================================================
end
3.在Octave里面执行
运算是没有问题的
octave:6 J = computeCost(X, y, theta)
J = 0
但提交作业时候会报错,不知道为什么会跳出一个A矩阵。
A =
Diagonal Matrix
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
!! Submission failed: unexpected error: operator *: nonconformant arguments (op1 is 3x3, op2 is 2x1)
!! Please try again later.