

toydata1 = [2 2; 3 3; 4 4; 5 5; 4 6; 3 7; 4 8; 5 9; 6 10; 6 2; 7 3; 8 4; 9 5; 8 6; 7 7; 7 8; 7 9; 8 10];
toylab1 = [1 1 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ];
model = svmtrain(toydata1,toylab1,'kernel_function','linear','ShowPlot',true,'autoscale','true','BoxConstraint',100000)
%model = svmtrain(toydata1,toylab1,'kernel_function','rbf','rbf_sigma',0.01,'ShowPlot',true,'autoscale','true','BoxConstraint',0.5)
t1 = toydata1(1:9,:);
t2 = toydata1(10:18,:);
mean(t1);
mean(t2);


% Load a dataset (Choose by uncommenting)
%load myNormalDistDataset % Feature data are stored in 'A', and corresponding labels in 'a' 
 load myBananaDataset; % Feature data are stored in 'A' and 'B', and corresponding labels in 'a' and 'b', respectively

% Train our SVM classifier
%model = svmtrain(a, A, '-t 0 -c 1');  % "-t 0" -> Linear kernel
%model = svmtrain(A,a,'kernel_function','linear','ShowPlot',true,'autoscale','true','BoxConstraint',1)
model = svmtrain(A,a,'kernel_function','rbf','rbf_sigma',2,'ShowPlot',true,'autoscale','true','BoxConstraint',1)



groups = a
%# number of cross-validation folds:
%# If you have 50 samples, divide them into 10 groups of 5 samples each,
%# then train with 9 groups (45 samples) and test with 1 group (5 samples).
%# This is repeated ten times, with each group used exactly once as a test set.
%# Finally the 10 results from the folds are averaged to produce a single 
%# performance estimation.
k=10;

cvFolds = crossvalind('Kfold', groups, k);   %# get indices of 10-fold CV
cp = classperf(groups);                      %# init performance tracker

C_arr = [2^-5 2^-3 2^-1 2^1 2^3 2^5 2^7 2^9 2^11 2^13 2^15]
sigma_arr = [2^-15 2^-13 2^-11 2^-9 2^-7 2^-5 2^-3 2^-1 2^1 2^3]
nc = length(C_arr)
ns = length(sigma_arr)

% Loop over all parameter values, store the best error, C, and sigma
best_rate = 0.0
best_C = 0;
best_sigma = 0;
rate_vals = zeros(1,k);
 
for c=1:nc
    for s=1:ns

    for i = 1:k                                  %# for each fold
        testIdx = (cvFolds == i);                %# get indices of test instances
        trainIdx = ~testIdx;                     %# get indices training instances

        %# train an SVM model over training instances
        svmModel = svmtrain(A(trainIdx,:), groups(trainIdx), ...
                 'Autoscale',true, 'Showplot',false, 'Method','QP', ...
                 'BoxConstraint',C_arr(c), 'Kernel_Function','rbf', 'RBF_Sigma',sigma_arr(s));

        %# test using test instances
        pred = svmclassify(svmModel, A(testIdx,:), 'Showplot',false);

        %# evaluate and update performance object
        correct_rate = sum(pred==a(testIdx))/length(pred);
        rate_vals(i) = correct_rate;
        
    end

%# get accuracy
    aver_rate = mean(rate_vals);

    if aver_rate>best_rate
        best_rate = aver_rate;
        best_C = C_arr(c);
        best_sigma = sigma_arr(s);
        %best_C
        %best_sigma
        %best_rate
        tt=1;
    end
    
%# get confusion matrix
%# columns:actual, rows:predicted, last-row: unclassified instances
%cp.CountingMatrix

    end
end

figure;
% NOw train again on the full training data set with the best parameters
svmModel = svmtrain(A, a, ...
                 'Autoscale',true, 'Showplot',true, 'Method','QP', ...
                 'BoxConstraint',best_C, 'Kernel_Function','rbf', 'RBF_Sigma',best_sigma);

%best_rate
%best_C
%best_sigma

fulltrainpred = svmclassify(svmModel, A);
cptrain = classperf(fulltrainpred, a);
cptrain.CorrectRate

testpred = svmclassify(svmModel, B);
cptest = classperf(testpred, b);
cptest.CorrectRate



