Skip to content

Instantly share code, notes, and snippets.

@ravikiranj
Created December 8, 2012 17:18
Show Gist options
  • Save ravikiranj/4241081 to your computer and use it in GitHub Desktop.
Save ravikiranj/4241081 to your computer and use it in GitHub Desktop.
SVM Matlab - Large Sparse Matrices (20000 feature vector size)
clc;clear;
% start time
tic;
%feature vector size
featSize = 20000;
% training data
N = 300;
neg = randi([1, N], int16(N/3), 1);
trainY = ones(N, 1);
trainY(neg) = -1;
trainX = randi([0,1], N, featSize);
% convert trainX to sparse matrix
trainX = sparse(trainX);
% testing data
N2 = 100;
neg = randi([1, N2], int16(N2/3), 1);
testY = ones(N2, 1);
testY(neg) = -1;
testX = randi([0,1], N2, featSize);
% convert testX to sparse matrix
testX = sparse(testX);
% svmtrain
options = optimset('maxiter', 100);
SVMStruct = svmtrain(trainX, trainY, ...
'kernel_function', 'linear', 'Method','QP', 'quadprog_opts',options);
% svmclassify
predY = svmclassify(SVMStruct, testX);
match = 0;
for i = 1 : length(testY)
if(predY(i) == testY(i))
match = match + 1;
end
end
% compute time elapsed
tElapsed = toc;
fprintf('Prediction Accuracy = %.2f\n', (match/length(testY))*100);
fprintf('Time Elapsed = %f seconds\n', tElapsed);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment