赞
踩
二元分类问题的逻辑回归包含原理及实现。
这次做多元分类,目的是能够识别 20 × 20 20\times20 20×20像素的手写体数字。
来自MINST的手写数字数据库,矩阵X
中是5000张图片的灰度构成的数据集,因为图片像素是20×20,所以矩阵每行有400个元素,为图片的灰度。y
向量表明图片中的数字是什么,因为matlab数组下标从1开始,所以我们把数字0对应的数据映射到10上。
% Load saved matrices from file
load('ex3data1.mat');
% The matrices X and y will now be in your MATLAB environment
随机抽出几张图片瞅一瞅,吴恩达的代码没仔细研究,先放在这里:
function [h, display_array] = displayData(X, example_width) %DISPLAYDATA Display 2D data in a nice grid % [h, display_array] = DISPLAYDATA(X, example_width) displays 2D data % stored in X in a nice grid. It returns the figure handle h and the % displayed array if requested. % Set example_width automatically if not passed in if ~exist('example_width', 'var') || isempty(example_width) example_width = round(sqrt(size(X, 2))); end % Gray Image colormap(gray); % Compute rows, cols [m n] = size(X); example_height = (n / example_width); % Compute number of items to display display_rows = floor(sqrt(m)); display_cols = ceil(m / display_rows); % Between images padding pad = 1; % Setup blank display display_array = - ones(pad + display_rows * (example_height + pad), ... pad + display_cols * (example_width + pad)); % Copy each example into a patch on the display array curr_ex = 1; for j = 1:display_rows for i = 1:display_cols if curr_ex > m, break; end % Copy the patch % Get the max value of the patch max_val = max(abs(X(curr_ex, :))); display_array(pad + (j - 1) * (example_height + pad) + (1:example_height), ... pad + (i - 1) * (example_width + pad) + (1:example_width)) = ... reshape(X(curr_ex, :), example_height, example_width) / max_val; curr_ex = curr_ex + 1; end if curr_ex > m, break; end end % Display Image h = imagesc(display_array, [-1 1]); % Do not show axis axis image off drawnow; end
m = size(X, 1);
% Randomly select 100 data points to display
rand_indices = randperm(m);
sel = X(rand_indices(1:100), :);
displayData(sel);
ex3的意思是在ex2的基础上让代价-梯度函数向量化,即通过矩阵运算省略for
循环,不过我已经在ex2搞定了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。