Forward selection with Random forest: error

조회 수: 2 (최근 30일)
Sowmya MR
Sowmya MR 2018년 2월 23일
답변: Charu 2025년 1월 29일
I am trying to run below code for feature selection using Random forest in matlab but it gives me some error. Can someone help me fix it?
rng(10)
c = cvpartition(Ytrain,'k',10);
opts = statset('display','iter');
fun = @(xtrain,ytrain,xtest,ytest) sum(ytest ~= predict(TreeBagger(500,xtrain,ytrain),xtest));
[fs,history] = sequentialfs(fun,Xtrain,Ytrain,'cv',c,'options',opts, 'direction', 'forward');
The function '@(xtrain,ytrain,xtest,ytest)sum(ytest~=predict(TreeBagger(500,xtrain,ytrain),xtest))' generated the
following error:
Undefined function 'ne' for input arguments of type 'cell'.

답변 (1개)

Charu
Charu 2025년 1월 29일
Hello Sowmya,
According to my understanding of the question you are trying to run a feature selection using random forest in MATLAB. The error you are encountering suggests that the "Ytrain" or "Ytest" variable is of type "cell", which is causing issues with the “~=” operator used for comparison. This usually happens when labels are stored as cell arrays, often due to categorical data being represented as strings.
To remove this error the possible modifications that can be done are:
1.Convert cell arrays to categorical or numeric arrays: Ensure that "Ytrain" and "Ytest" are not cell arrays. These can be converted into categorical or numeric arrays if they are currently stored as strings.
% Convert Ytrain to a categorical or numeric array if it's a cell array
if iscell(Ytrain)
Ytrain = categorical(Ytrain); % Convert to categorical array
2. Modify the function: Ensure that the labels are compatible with the "TreeBagger" function and the comparison operation.
fun = @(xtrain, ytrain, xtest, ytest) ...
sum(~strcmp(ytest, predict(TreeBagger(500, xtrain, ytrain), xtest)));
The comparison operator “~=” is replaced with "~strcmp" To know more about the "strcmp" function you can refer to this link of official documentation of MATLAB.
Hope this helps!

카테고리

Help CenterFile Exchange에서 Language Support에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by