码迷,mamicode.com
首页 > 其他好文 > 详细

matlab(5) : 求得θ值后用模型来预测 / 计算模型的精度

时间:2015-09-28 11:46:28      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:

求得θ值后用模型来预测 / 计算模型的精度

 

%% ============== Part 4: Predict and Accuracies ==============
% After learning the parameters, you‘ll like to use it to predict the outcomes
% on unseen data. In this part, you will use the logistic regression model
% to predict the probability that a student with score 45 on exam 1 and
% score 85 on exam 2 will be admitted.
%
% Furthermore, you will compute the training and test set accuracies of
% our model.
%
% Your task is to complete the code in predict.m

% Predict probability for a student with score 45 on exam 1
% and score 85 on exam 2

prob = sigmoid([1 45 85] * theta);
fprintf([‘For a student with scores 45 and 85, we predict an admission ‘ ...
‘probability of %f\n\n‘], prob);

% Compute accuracy on our training set
p = predict(theta, X);

fprintf(‘Train Accuracy: %f\n‘, mean(double(p == y)) * 100);

fprintf(‘\nProgram paused. Press enter to continue.\n‘);
pause;  

 

predict.m

function p = predict(theta, X)
%PREDICT Predict whether the label is 0 or 1 using learned logistic
%regression parameters theta
% p = PREDICT(theta, X) computes the predictions for X using a
% threshold at 0.5 (i.e., if sigmoid(theta‘*x) >= 0.5, predict 1)

m = size(X, 1); % Number of training examples

% You need to return the following variables correctly
p = zeros(m, 1);

% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
% your learned logistic regression parameters.
% You should set p to a vector of 0‘s and 1‘s
%
for i=1:m
if sigmoid(X(i,:) * theta) >=0.5
p(i) = 1;
else
p(i) = 0;
end
end

% =========================================================================


end

 

matlab(5) : 求得θ值后用模型来预测 / 计算模型的精度

标签:

原文地址:http://www.cnblogs.com/yan2015/p/4843564.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!