斯坦福机器学习实验之2-逻辑回归(Logistic Regression)
来源:互联网 发布:电路模拟软件 编辑:程序博客网 时间:2024/05/21 06:25
1.逻辑回归
目标:利用逻辑回归预测学生是否被录取。
数据集:一群学生的两次考试成绩作为输入和是否被录取作为输出。
1.1数据可视化(Visualizing the the data)
函数:find
% Find Indices of Positive and Negative Examplespos = find(y==1); neg = find(y == 0);% Plot Examplesplot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, ...'MarkerSize', 7);plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y', ...'MarkerSize', 7);
1.2 执行(Implementation)
1.2.1 sigmoid函数(sigmoid function)
函数:exp
function t=sigmoid(z) t=zeros(length(z)); t=1./(1+exp(-1*z)); end
1.2.2 代价函数和梯度(Cost function and gradient)
函数:log
function [J,grad] = costFunction(theta,X,y)%myFun - Description%% Syntax: [cost,grad] = costFunction(initial_theta,X,y)%% Long description m=length(y); J=0; grad=zeros(size(theta)); J=-1/m*sum(y.*log(sigmoid(X*theta))+(1-y).*log(1-sigmoid(X*theta))); grad=1/m*X'*(sigmoid(X*theta)-y);end
注:matlab里的自带的函数都是为处理向量/矩阵准备的!
1.2.3 利用fminunc学习参数
函数:optimset;fminunc
% Set options for fminuncoptions = optimset('GradObj', 'on', 'MaxIter', 400);% Run fminunc to obtain the optimal theta% This function will return theta and the cost[theta, cost] = ...fminunc(@(t)(costFunction(t, X, y)), initial theta, options);
1.2.4 评估逻辑回归(Evaluating logistic regression)
ex2.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 setp = 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) m=size(X,1); p=zeros(m,1); p(sigmoid(X*theta)>0.5)=1; end
阅读全文
0 0
- 斯坦福机器学习实验之2-逻辑回归(Logistic Regression)
- 机器学习2 逻辑回归Logistic Regression
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习算法之: 逻辑回归 logistic regression (LR)
- 机器学习总结之逻辑回归Logistic Regression
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习算法之逻辑回归(Logistic Regression)
- 机器学习之逻辑回归(Logistic Regression)
- 机器学习之逻辑回归 Logistic Regression(一)
- 机器学习: 逻辑回归(Logistic Regression)
- 机器学习-logistic Regression(逻辑回归)
- windows update独立安装程序一直在此计算机上搜索更新
- vim使用
- Tensorfloe教程-深入mnist
- matlab转为c或者c++
- cookie
- 斯坦福机器学习实验之2-逻辑回归(Logistic Regression)
- javascript中的数据类型
- Angular开发(十六)-HTTP请求的详细介绍
- Android初级教程
- linux 安装jdk1.8操作
- 使用Eclipse搭建Node.js开发环境
- 新手在虚拟机上搭建hadoop,zookeepe,hbase,spark
- Tensorflow教程-TensorFlow运作方式入门
- Dom对象,js对象和jquery对象的区别