Search code examples
matlabpattern-recognition

Issues related to plots in pattern recognition(Part1)


I cannot follow crossval() & cvpartition() function given in MATLAB documentation crossval(). What goes in the parameter and how would it help to compare performance and accuracy of different classifiers. Would be obliged if a simpler version of it is provided here.


Solution

  • Let's work on Example 2 from CROSSVAL documentation.

    load('fisheriris');
    y = species;
    X = meas;
    

    Here we loaded the data from example mat-file and assigned variable to X and y. meas amtrix contains different measurements of iris flowers and species are tree classes of iris, what we are trying to predict with the data.

    Cross-validation is used to train a classifier on the same data set many times. Basically at each iteration you split the data set to training and test data. The proportion is determined by k-fold. For example, if k is 10, 90% of the data will be used for training, and the rest 10% - for test, and you will have 10 iterations. This is done by CVPARTITION function.

    cp = cvpartition(y,'k',10); % Stratified cross-validation
    

    You can explore cp object if you type cp. and press Tab. You will see different properties and methods. For example, find(cp.test(1)) will show indices of the test set for 1st iteration.

    Next step is to prepare prediction function. This is probably where you had the main problem. This statement create function handle using anonymous function. @(XTRAIN, ytrain,XTEST) part declare that this function has 3 input arguments. Next part (classify(XTEST,XTRAIN,ytrain)) defines the function, which gets training data XTRAIN with known ytrain classes and predicts classes for XTEST data with generated model. (Those data are from cp, remember?)

    classf = @(XTRAIN, ytrain,XTEST)(classify(XTEST,XTRAIN,ytrain));
    

    Then we are running CROSSVAL function to estimate missclassification rate (mcr) passing the complete data set, prediction function handle and partitioning object cp.

    cvMCR = crossval('mcr',X,y,'predfun',classf,'partition',cp)
    cvMCR =
        0.0200
    

    Still have questions?