当前位置:   article > 正文

机器学习:使用matlab实现逻辑回归解决数字识别(多元分类)问题_matlab 逻辑回归

matlab 逻辑回归

前置章节

二元分类问题的逻辑回归包含原理及实现。

这次做多元分类,目的是能够识别 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
  • 1
  • 2
  • 3

数据可视化

随机抽出几张图片瞅一瞅,吴恩达的代码没仔细研究,先放在这里:

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
m = size(X, 1);
% Randomly select 100 data points to display
rand_indices = randperm(m);
sel = X(rand_indices(1:100), :);
displayData(sel);
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述

代价-梯度函数

ex3的意思是在ex2的基础上让代价-梯度函数向量化,即通过矩阵运算省略for循环,不过我已经在ex2搞定了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/475861
推荐阅读
相关标签