matlab(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
最新文章
- Linux创建定时任务
- webapi相关知识
- Html简单介绍
- dataview将excel表格的数据导出成txt文件
- 软件工程day8
- Mac下安装MySQL
- JavaScript入门篇 第一天
- 初始maven
- Linux下*.tar.bz2等文件如何解压--转
- 敏捷开发的价值观(转自MBAlib)
- RHEL-resolv.conf文件修改后重启被还原
- 【css基础】文本对齐,水平对齐,垂直对齐
- AngularJS高级程序设计读书笔记 -- 指令篇 之 自定义指令
- JAVA程序员成长历程(一)
- MySQL下载安装、基本配置、问题处理
- python抢火车票的脚本
- PHP接口的思考
- C++ 模态与非模态对话框
- DevExpress中的GridControl控件设置了列Readonly后,想双击弹出明细的实现
- 黄聪:C#使用Application.Restart重启程序出错解决办法
热门文章
- mysql 库、表、数据的增删改
- 编译Cython代码时遇到的问题: fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'
- 《构建之法》——GitHub和Visual Studio的基础使用
- Jackson使用
- 多生产者多消费者(第一种方式),基于synchronized,wait,notifyAll
- python 2.7 环境配置
- 记一次SQL优化
- TCP,SYN,FIN扫描
- Linux 基础 目录介绍
- vue的安装配置与项目创建