Forward selection with Random forest: error

14 vues (au cours des 30 derniers jours)
Sowmya MR
Sowmya MR le 23 Fév 2018
Réponse apportée : Charu le 29 Jan 2025 à 9:59
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'.

Réponses (1)

Charu
Charu le 29 Jan 2025 à 9:59
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!

Catégories

En savoir plus sur Signal Processing Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by