博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
matlab(5) : 求得θ值后用模型来预测 / 计算模型的精度
阅读量:4946 次
发布时间:2019-06-11

本文共 1765 字,大约阅读时间需要 5 分钟。

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

 ex2.m部分程序

%% ============== 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);   %若p==y,则返回1否则返回0;然后对这些0,1求平均值

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

 

转载于:https://www.cnblogs.com/yan2015/p/4843564.html

你可能感兴趣的文章
CSS3 transition属性配合Js实现超链接“背景”过渡渐变出现效果
查看>>
(巧用)事件代理
查看>>
Jmeter非命令行执行脚本
查看>>
Python学习笔记-05
查看>>
人情与面子
查看>>
JS加载获取父窗体传递的参数
查看>>
NumPy 学习笔记(三)
查看>>
选择排序详解
查看>>
SpringCloud系列五:Ribbon 负载均衡(Ribbon 基本使用、Ribbon 负载均衡、自定义 Ribbon 配置、禁用 Eureka 实现 Ribbon 调用)...
查看>>
【转】UML的9种图例解析
查看>>
python2.x到python3.x函数变化
查看>>
CSS之Position详解
查看>>
javascript面向对象重写右键菜单事件
查看>>
UVa10791 - Minimum Sum LCM
查看>>
Android底部导航栏——FrameLayout + RadioGroup
查看>>
NOI2016 优秀的拆分 后缀数组
查看>>
Java消息服务
查看>>
Jtester使用
查看>>
详解CSS样式的position属性
查看>>
Python机器学习(5)——朴素贝叶斯分类器
查看>>