machine-learning-ex3_1

来源:互联网 发布:网络女主播萱萱 编辑:程序博客网 时间:2024/05/29 23:24

input_layer_size  = 400;

num_labels = 10; 

 %%Loading and Visualizing Data

load('ex3data1.mat');

m = size(X, 1);

rand_indices = randperm(m);
sel = X(rand_indices(1:100), :);

displayData(sel);

function [h, display_array] = displayData(X, example_width)if ~exist('example_width', 'var') || isempty(example_width) example_width = round(sqrt(size(X, 2)));endcolormap(gray);[m n] = size(X);example_height = (n / example_width);display_rows = floor(sqrt(m));display_cols = ceil(m / display_rows);pad = 1;display_array = - ones(pad + display_rows * (example_height + pad), ...                       pad + display_cols * (example_width + pad));curr_ex = 1;for j = 1:display_rowsfor i = 1:display_colsif curr_ex > m, break; end% Copy the patch% Get the max value of the patchmax_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;endif curr_ex > m, break; endendh = imagesc(display_array, [-1 1]);axis image offdrawnow;end
%%Vectorize Logistic Regression
lambda = 0.1;
[all_theta] = oneVsAll(X, y, num_labels, lambda);

function [all_theta] = oneVsAll(X, y, num_labels, lambda)m = size(X, 1);n = size(X, 2);all_theta = zeros(num_labels, n + 1);X = [ones(m, 1) X];for c = 1:num_labels    initial_theta = all_theta(c, :)';    options = optimset('GradObj', 'on', 'MaxIter', 50);    theta = fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), ...        initial_theta, options);    all_theta(c,:)=theta';endend
%%Predict for One-Vs-All 

pred = predictOneVsAll(all_theta, X);

function p = predictOneVsAll(all_theta, X)m = size(X, 1);num_labels = size(all_theta, 1);p = zeros(size(X, 1), 1);X = [ones(m, 1) X];[~,p]=max(X*all_theta.',[],2);end
%% Predict for One-Vs-All
pred = predictOneVsAll(all_theta, X);

fprintf('\nTraining Set Accuracy: %f\n', mean(double(pred == y)) * 100);

0 0
原创粉丝点击