SVM prediction does not predict OK although the support vectors are valid - matlab

I have a following(fig 1) unlabeled training set which I am trying to detect the outliers, have come up with a procedure to label the data with 0:normal data and 1:outlier and want to train it with SVM.
I followed this instructions to train the SVM's model but when I am trying to predict the labels of same data I have trained the SVM it does not predict any(fig 2)!
fig 1: the support vectors after training
fig 2: the prediction of SVM model on the same data it has been training with
The output of prediction is not supposed to look like this!
The code I have used for prediction is:
out = predict(model, data');
Question:
What is wrong with my approach?

For what it worth, I have found the answer to my question and now its working fine.
The result of prediction after using a non-linear kernel, but I don't know why this happened?

Related

SVM-Classification of multiclass

I've used Matlab Classification Learner App to train my SVM classifier and i have 99.9% of accuracy in prediction (i tested it with the function predict on matlab). What i wanted to do now was to predict without usind this function but using the hyperplane. I exported the trained classifier and so i have all the weights and the bias to find the hyperplane. Which formula should i use to predict new data? I tryed computing the sign of w'x but it works only in few cases. Can you help me understand what should i do?
Thanks a lot!

Random component on fitcsvm/predict

I have a train dataset and a test dataset, and I train a SVM with fitcsvm in MATLAB. Then, I proceed to test the trained model with predict. I'm always using the same datasets, but I keep getting different AUCs for the same model, which makes me wonder where in the process is there a random component. Note that
I'm aware of the fact that formally there isn't such thing as ROC curve or AUC and
I'm not asking for the statistical background of the SVM problem. It is relative to the matlab implementation of the training/test algorithm. I expected to have the same results because the training algorithm is, afaik, a deterministic process.

How to change a binary KNN classifier to be a SVM classifier?

I am classifying gender using a KNN classifier.
I want to add an SVM classifier instead of KNN classifier with the same labels of 0 and 1 (0 for women and 1 for men)
I have a matrix of test examples, sample, a matrix of training examples, training, and a vector with the labels for the training examples group. I want class, a vector of the labels for the test examples.
class = knnclassify(sample, training, group);
if class==1
x='Male';
else
x='Female';
end
How can I change this code to find class using an SVM?
To train an SVM, you will need the Statistics and Machine Learning Toolbox.
The biggest difference between the knnclassify and using an SVM classifier is that training and classifying new labels will be two separate steps.
1. Train your SVM : fitcsvm
This step teaches the classifier how to distinguish between your two classes. It is learning a linear separator (or a weighted combination of the features) which has the largest margin between positive and negative examples. All the examples you give it need to have ground truth labels.
SVM's have many tunable parameters that you can adjust during the training step. There are several good tutorials in the Matlab documentation which describe the differences, but for the most basic version, you can just use your training examples
model = fitcsvm(training,group);
This model will be used in the next step.
2. Classify new examples : predict
To classify your new example, run
class = predict(sample, model);
Notes:
Using your model, you can also run cross-fold validation, useful for accuracy analysis.
cvModel = crossval(model);
classError = kfoldLoss(cvModel);
You can also save your model, like any other Matlab variable for future use.
save('model.m', 'model');
knnclassify comes from the bioinformatics toolbox. In the Statistics and Machine Learning Toolbox, there is also a KNN model which you train with fitcknn and classify with predict. The benefit is that you can reuse your KNN model with several sets of data, compare cross-validation results, and save it for future use.

I couldn't understand how to make a k-fold cross validation test with multi-class SVM

i am very new to matlab and SVM and i am reproducing this experiment from strach http://bioinformatics.oxfordjournals.org/content/19/13/1656.full.pdf
*There they say "In the training of SVMs, we use the method of one versus
the others, or one versus the rest" . ok there are 12 classes so they produce 12 SVMs. they train each one with positives vs all rest.
*but then they say "The prediction performance was examined by the 5-fold cross-validation test"
My noob question is, How can they make a k-fold cross validation after! they train SVMs. what i think(likely faulty) is when you make a k-fold cross validation, you construct a new svm from beginning. they may be similar but svm model is different in every loop. there are k different svm models. but, if they train svm models beforehand, How can they make a cross validation test? What am i missing? Please help and thank you very much
First they produce cross-validated datasets. Then they train 5 models (one for each fold) and repeatedly train-test. You can do this as follows:
% I assume use of LIBSVM for svm training-testing in this code snippet
% Create a random data
data=1+2*randn(1000,10);
labels=randi(12,[1000,1]);
%do 5-fold cross-validation
ind=crossvalind('Kfold',labels,5);
for i=1:5
% (4/5)^th random data for training
trainingData=data(ind~=5,:); %notice ~=
trainingLabels=labels(ind~=5);
% (1/5)^th random data for testing
testingData=data(ind==5,:); %notice ==
testingLabels=labels(ind==5);
% train svm
model{i,1}=svmtrain(trainingLabels,trainingData);
%test svm
[predLabels{i,1},accuracy(i,1)]=svmpredict(testingLabels,testingData,model{i,1});
end
% I think this is what they mean when they say, we analyze the performance
% using 5 -fold cross validation
% following two things is what you will report
plot(accuracy); %how accuracy varies over random selection of data
avgAccuracy=mean(accuracy); %what is the average accuracy over 5 runs?

svm classification

I am a beginner in MATLAB and doing my Programming project in Digital Image Processing,i.e. Magnetic Resonance image classification using wavelet features+SVM+PCA+ANN. I executed the example SVM classification from MATLAB tool and modified that to fit my requirements. I am facing problems in storing more than one feature in an input vector and in giving new input to SVM. Please help.
Simply feed multidimensional feature data to svmtrain(Training, Group) function as Training parameter (Training can be matrix, each column represents separate feature). After that use svmclassify(SVMStruct, Sample) for testing data classification.