machine-learning-ex2_1
来源:互联网 发布:淘宝营业执照代办 编辑:程序博客网 时间:2024/05/16 18:40
%% Load Data
data = load('ex2data1.txt');
X = data(:, [1, 2]); y = data(:, 3);
%%Plotting
plotData(X, y);
function plotData(X, y)figure;hold on;pos=find(y==1);neg=find(y==0);plot(X(pos,1),X(pos,2),'k+','LineWidth',2,... 'MarkerSize',7);plot(X(neg,1),X(neg,2),'ko','MarkerFaceColor','y',... 'MarkerSize',7);hold off;endhold on;
xlabel('Exam 1 score')
ylabel('Exam 2 score')
legend('Admitted', 'Not admitted')
hold off;
%%Compute Cost and Gradient
[m, n] = size(X);
X = [ones(m, 1) X];
initial_theta = zeros(n + 1, 1);
[cost, grad] = costFunction(initial_theta, X, y);
function [J, grad] = costFunction(theta, X, y)m = length(y);J = 0;grad = zeros(size(theta));sig=sigmoid(X * theta);J=-(1/m)*sum(y' * log(sig)+ (1-y') * log(1- sig));grad=(1/m )* (X'* (sig-y));endfprintf('Cost at initial theta (zeros): %f\n', cost);
fprintf('Gradient at initial theta (zeros): \n');
fprintf(' %f \n', grad);
%%Optimizing using fminunc
options = optimset('GradObj', 'on', 'MaxIter', 400);
[theta, cost] = ...
fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);
fprintf('Cost at theta found by fminunc: %f\n', cost);
fprintf('theta: \n');
fprintf(' %f \n', theta);
plotDecisionBoundary(theta, X, y);
function plotDecisionBoundary(theta, X, y)plotData(X(:,2:3), y);hold onif size(X, 2) <= 3 plot_x = [min(X(:,2))-2, max(X(:,2))+2]; plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1)); plot(plot_x, plot_y) legend('Admitted', 'Not admitted', 'Decision Boundary') axis([30, 100, 30, 100])else u = linspace(-1, 1.5, 50); v = linspace(-1, 1.5, 50); z = zeros(length(u), length(v)); for i = 1:length(u) for j = 1:length(v) z(i,j) = mapFeature(u(i), v(j))*theta; end end z = z'; contour(u, v, z, [0, 0], 'LineWidth', 2)endhold offendhold on;
xlabel('Exam 1 score')
ylabel('Exam 2 score')
legend('Admitted', 'Not admitted')
hold off;
%%Predict and Accuracies
prob = sigmoid([1 45 85] * theta);
function g = sigmoid(z)g = zeros(size(z));g=1./(1+exp(-z));end
fprintf(['For a student with scores 45 and 85, we predict an admission ' ...
'probability of %f\n\n'], prob);
p = predict(theta, X);
function p = predict(theta, X)m = size(X, 1);p = zeros(m, 1);sig=sigmoid(X * theta);for iter=1:m if sig(iter)>=0.5 p(iter)=1; else p(iter)=0; end;end;endfprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100);
fprintf('\nProgram paused. Press enter to continue.\n');
pause;
- machine-learning-ex2_1
- machine learning
- Machine Learning
- machine learning
- Machine Learning
- machine learning
- Machine Learning
- machine learning
- machine learning
- Machine Learning
- Machine Learning
- machine-learning
- machine-learning
- Machine Learning
- Machine Learning
- Machine Learning
- machine learning
- Machine learning
- Android-自定义preference和PreferenceFragment实例详解
- Vsftpd使用FtpClient上传文件踩过的坑
- 我自己本机配置了一个maven环境。每次新开一个项目都要重新选择一个maven的配置,默认的都是这个。如图:
- 机器学习-线性回归-正规方程
- HTML5(李炎恢)学习笔记二 ------------- HTML5的结构
- machine-learning-ex2_1
- hdu5816 Hearthstone(状压DP)
- 磁条卡驱动
- absolute绝对定位的非绝对定位用法 张鑫旭博客
- SSRF Tips
- 初心
- Moscow Subregional 2010 Problem K. KMC Attacks 交互题、队列优化、枚举
- 从零开始学_JavaScript_系列(27)——myblog的优化【1】样式表分离、localStorage
- Qt Creator插件制作小插曲:有关QT_NO_CAST_FROM_ASCII的注意事项