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
% Copy each example into a patch on the display array curr_ex = 1; forj = 1:display_rows fori = 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, [-11]);
function[J, grad] = lrCostFunction(theta, X, y, lambda) %LRCOSTFUNCTION Compute cost and gradient for logistic regression with %regularization % J = LRCOSTFUNCTION(theta, X, y, lambda) computes the cost of using % theta as the parameter for regularized logistic regression and the % gradient of the cost w.r.t. to the parameters.
% Initialize some useful values m = length(y); % number of training examples J = 0; grad = zeros(size(theta));
function[all_theta] = oneVsAll(X, y, num_labels, lambda) %ONEVSALL trains multiple logistic regression classifiers and returns all %the classifiers in a matrix all_theta, where the i-th row of all_theta %corresponds to the classifier for label i % [all_theta] = ONEVSALL(X, y, num_labels, lambda) trains num_labels % logistic regression classifiers and returns each of these classifiers % in a matrix all_theta, where the i-th row of all_theta corresponds % to the classifier for label i
% Some useful variables m = size(X, 1); n = size(X, 2);
% You need to return the following variables correctly all_theta = zeros(num_labels, n + 1);
% Add ones to the X data matrix X = [ones(m, 1) X];
% ====================== YOUR CODE HERE ====================== options = optimset('GradObj', 'on', 'MaxIter', 200); initial_theta = zeros(n + 1, 1); fori = 1:10% 依次训练每一个分类器,y == i 只有 y 与 i 对应位置相等则为1否则0 [theta] = fmincg(@(t)(lrCostFunction(t, X, (y == i), lambda)), ... initial_theta, options); all_theta(i, :) = theta'; end % =========================================================================
end
预测
predictOneVsAll 中的参数 X 是一个矩阵,每一行代表一张图片。每一张图片应该经过所有分类器测试,返回概率最大的分类。
functionp = predictOneVsAll(all_theta, X) %PREDICT Predict the label for a trained one-vs-all classifier. The labels %are in the range 1..K, where K = size(all_theta, 1). % p = PREDICTONEVSALL(all_theta, X) will return a vector of predictions % for each example in the matrix X. Note that X contains the examples in % rows. all_theta is a matrix where the i-th row is a trained logistic % regression theta vector for the i-th class. You should set p to a vector % of values from 1..K (e.g., p = [1; 3; 1; 2] predicts classes 1, 3, 1, 2 % for 4 examples)
m = size(X, 1); num_labels = size(all_theta, 1);
% You need to return the following variables correctly p = zeros(size(X, 1), 1);
% Add ones to the X data matrix X = [ones(m, 1) X]; % ====================== YOUR CODE HERE ====================== test = sigmoid(X * all_theta'); % 向量化计算每一个样例 [m, p] = max(test, [], 2); % p 是最大概率对应的下标 % ========================================================================= end
functionp = predict(Theta1, Theta2, X) %PREDICT Predict the label of an input given a trained neural network % p = PREDICT(Theta1, Theta2, X) outputs the predicted label of X given the % trained weights of a neural network (Theta1, Theta2)