【原】Andrew Ng斯坦福机器学习(5)——Week2 Programming Quiz—实现

 

plotData

 

 

compute costfunction

 1 function J = computeCost(X, y, theta)
 2 %COMPUTECOST Compute cost for linear regression
 3 %   J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
 4 %   parameter for linear regression to fit the data points in X and y
 5 
 6 % Initialize some useful values
 7 m = length(y); % number of training examples
 8 
 9 % You need to return the following variables correctly 
10 J = 0;
11 
12 % ====================== YOUR CODE HERE ======================
13 % Instructions: Compute the cost of a particular choice of theta
14 %               You should set J to the cost.
15 
16   % y is the class labels
17   
18   m = size(X, 1);   % number of training examples, size of rows
19   predictions = X * theta; % predictions of hapothesis on all m examples
20   sqrErrors = (predictions - y) .^ 2; % squared errors .^ 指的是对数据中每个元素平方
21   
22   J = 1 / (2 * m) * sum(sqrErrors); 
23   
24   %disp(sprintf("computeCost %0.2f \n", J));
25 % =========================================================================
26 
27 end

 

posted @ 2018-10-30 10:46  马小豆包  阅读(93)  评论(0编辑  收藏  举报