How do you obtain a vector of predicted classes generated after cross-valiation of a decision tree?

조회 수: 1 (최근 30일)
I have created a decision tree (T) to predict classes, Y, based on features X. I would like to cross-validate the decision tree. When I do so, I can only get the overall cross-validation error, not the vector of classes as they are predicted from the cross-validation.
Here is the code I am trying to use: T3 = classregtree(X,Y,'method','classification'; view(T3); cp = cvpartition(Y,'k',10);
dtClassFun = @(xtrain,ytrain,xtest)(eval(classregtree(xtrain,ytrain),xtest));
crossvalerrorrT = crossval('mcr',X,Y,'predfun',dtClassFun,'partition',cp)
This will output a scalar overall cross-validation error.
How can I output a vector of cross-validation predicted classes (so that I can look at cross-validation error for each class separately)?

채택된 답변

Tom Lane
Tom Lane 2012년 3월 28일
Consider this:
load fisheriris
cp = cvpartition(species,'k',10);
F = @(xtr,ytr,xtest,ytest){ytest, eval(classregtree(xtr,ytr),xtest)};
result = crossval(F,meas,species,'partition',cp)
This uses the other syntax of crossval. The function F packages up the observed and fitted Y values for each fold of the 1-fold cross-validation. The results is a cell array with ten rows, one for each fold, with each row containing the corresponding observed and fitted values.
Alternatively you could write the cross-validation loop yourself. Loop over j and get test(cp,j) and training(cp,j) each time.
  댓글 수: 2
Kelly
Kelly 2012년 3월 28일
Very helpful, thank you!
Just to clarify one point:
Column 1 in 'result' is the observed y, and column 2 in 'result' is the predicted/fitted y for each fold cross-validation?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ilya
Ilya 2012년 3월 29일
If you have 11a or later, you can use ClassificationTree:
load fisheriris;
cvtree = ClassificationTree(meas,species,'kfold',10);
Yfit = kfoldPredict(cvtree);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by