0% found this document useful (0 votes)
68 views

Matlab Code:: All 'Train - CSV' 'Test - Org - CSV' 'Testme - CSV'

This Matlab code: 1) Reads in training and test data for a digit recognition problem from CSV files 2) Previews the first 25 images from the training data 3) Splits the training data into training and test sets for cross-validation 4) Loops through different sizes of a neural network to find the best model 5) Uses the best model to make predictions on the test data and calculates accuracy 6) Makes predictions on a separate test set and writes the results to a CSV file for submission.

Uploaded by

menosoft
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Matlab Code:: All 'Train - CSV' 'Test - Org - CSV' 'Testme - CSV'

This Matlab code: 1) Reads in training and test data for a digit recognition problem from CSV files 2) Previews the first 25 images from the training data 3) Splits the training data into training and test sets for cross-validation 4) Loops through different sizes of a neural network to find the best model 5) Uses the best model to make predictions on the test data and calculates accuracy 6) Makes predictions on a separate test set and writes the results to a CSV file for submission.

Uploaded by

menosoft
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Matlab Code:

clc;
clear all;
tr = csvread('train.csv', 1, 0); % read train.csv
sub = csvread('test_org.csv', 1, 0); % read test.csv

subMe = csvread('testme.csv', 1, 0);

figure % plot images


colormap(gray) % set to grayscale
for i = 1:25 % preview first 25 samples
subplot(5,5,i) % plot them in 6 x 6 grid
digit = reshape(tr(i, 2:end), [28,28])'; % row = 28 x 28 image
imagesc(digit) % show the image
title(num2str(tr(i, 1))) % show the label
end

n = size(tr, 1); % number of samples in the dataset


targets = tr(:,1); % 1st column is |label|
targets(targets == 0) = 10; % use '10' to present '0'
targetsd = dummyvar(targets); % convert label into a dummy variable
inputs = tr(:,2:end); % the rest of columns are predictors

inputs = inputs'; % transpose input


targets = targets'; % transpose target
targetsd = targetsd'; % transpose dummy variable
rng(1); % for reproducibility
c = cvpartition(n,'Holdout',n/3); % hold out 1/3 of the dataset

Xtrain = inputs(:, training(c)); % 2/3 of the input for training


Ytrain = targetsd(:, training(c)); % 2/3 of the target for training
Xtest = inputs(:, test(c)); % 1/3 of the input for testing
Ytest = targets(test(c)); % 1/3 of the target for testing
Ytestd = targetsd(:, test(c)); % 1/3 of the dummy variable for testing

%subMe = sub(1:9,:);

figure % plot images


colormap(gray) % set to grayscale
for i = 1:9 % preview first 25 samples
subplot(5,5,i) % plot them in 6 x 6 grid
digit = reshape(subMe(i, :), [28,28])'; % row = 28 x 28 image
imagesc(digit) % show the image
end

sweep = 200; % parameter values to test


scores = zeros(length(sweep), 1); % pre-allocation
models = cell(length(sweep), 1); % pre-allocation
x = Xtrain; % inputs
t = Ytrain; % targets
trainFcn = 'trainscg'; % scaled conjugate gradient
for i = 1:length(sweep)
hiddenLayerSize = sweep(i); % number of hidden layer neurons
net = patternnet(hiddenLayerSize); % pattern recognition network
net.divideParam.trainRatio = 70/100;% 70% of data for training
net.divideParam.valRatio = 15/100; % 15% of data for validation
net.divideParam.testRatio = 15/100; % 15% of data for testing
net = train(net, x, t); % train the network
models{i} = net; % store the trained network
p = net(Xtest); % predictions
[~, p] = max(p); % predicted labels
scores(i) = sum(Ytest == p) /... % categorization accuracy
length(Ytest);
end

n = size(subMe, 1); % num of samples


subMe = subMe'; % transpose
[~, highest] = max(scores); % highest scoring model
net = models{highest}; % restore the model
Ypred = net(subMe); % label probabilities
[~, Label] = max(Ypred); % predicted labels
Label = Label'; % transpose Label
Label(Label == 10) = 0;

disp (Label);% change '10' to '0'


ImageId = 1:n; ImageId = ImageId'; % image ids
writetable(table(ImageId, Label), 'submission.csv');% write to csv

Resource Data:

https://www.kaggle.com/c/digit-recognizer/data

You might also like