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
- machine-learning-ex3_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
- VC中,编译工具条内的Debug与Release选项是什么含义?
- 自定义jsp标签
- ocx
- 函数assert的用法?
- 解决WebLogic启动时BEA-171522异常(启动时报classcast exception)
- machine-learning-ex3_1
- 引用和指针的区别。
- java 多线程基础之银行取号排队系统
- HDU 1269 迷宫城堡 [强连通分量] [Tarjan]
- javaee之javamail邮件练习
- HDU5573 2015 上海 B题(模拟-找规律)
- 有了 malloc/free 为什么还要 new/delete ?
- C、C++和VC++之间的区别
- 学习记录:android从程序员到架构师的高手之路