treder / MVPA-Light

Matlab toolbox for classification and regression of multi-dimensional data
MIT License
70 stars 35 forks source link

Binary LDA classifier throws error when trying to return probabilities per class #49

Closed benjybarnett closed 1 month ago

benjybarnett commented 1 month ago

Hi. I am trying to run a simple binary cross-decoding analysis, and I would like classifier output to show the predicted class probabilities.

I am using the following code:

cfgS = [];
cfgS.classifier = 'lda';
cfgS.metric = 'none';
cfgS.preprocess ={'undersample'};
cfgS.output_type = 'prob';
cfgS.repeat = 1;
cfgS.sample_dimension = 1;
cfgS.feature_dimension = 2;
[train_dot_accuracy,~] = mv_classify(cfgS, dot_data, binary_dot_lbls_recode, face_data, face_lbls); 

But I receive the following error:

Output argument "prob" (and possibly others) not assigned a value in the execution with
"test_lda" function.
Error in mv_get_classifier_output (line 35)
        [~, ~, out] = test_fun(cf,Xtest);
Error in mv_classify (line 490)
            cf_output{1,1,ix{:}} = mv_get_classifier_output(cfg.output_type, cf, test_fun, Xtest_ix); 

It looks like the test_LDA function cannot compute the probability. I think because the code in mv_classify is failing to pass on the correct output_type. Although I can't quite figure it out. Would appreciate any insights!

treder commented 1 month ago

Hi @benjybarnett , the problem occurs because one of LDA hyperparameters (cfg.hyperparameter.prob = 1) needs to be set accordingly.

However, in this case it makes sense to actually adapt the default value for LDA, which I now included in mv_check_inputs. Your example should work now out of the box, can you please pull the latest changes and confirm?

benjybarnett commented 1 month ago

Hi @treder thanks for looking into this! I see that I should have specified that now..however addressing that with my own cfg.hyperparameter.prob = 1 call or pulling the latest version just causes the following error to appear:

Unrecognized function or variable 'Sw'.
Error in train_lda (line 199)
    cf.Sw   = Sw;
Error in mv_classify (line 486)
        cf= train_fun(cfg.hyperparameter, X_ix, trainlabel); 

This is following the same code snippet as before. Cheers!

treder commented 1 month ago

I see - I think you have more features than samples right? The problem is that by default LDA chooses the dual form of the solution which has no covariance matrix (but it's needed for the probability calculation). So you would also have to set cfg.hyperparameter.form = 'primal' (the default is auto). Again I changed the default settings in the toolbox so if you pull again you shouldn't need to set this manually (hope it works this time round)

benjybarnett commented 1 month ago

ah - thank you very much :)