correct and incorrect predictors

6 ビュー (過去 30 日間)
Saeed Magsi
Saeed Magsi 2022 年 9 月 17 日
回答済み: Dinesh 2023 年 6 月 8 日
Dear community members, i am stuck in a problem. I tried to search for the solution but i could not find. I want to get the input data (predictors) of an incorrect predicted class. How can i do that? i am just able to get the incorrect classes in the confusion chart but i need to find the input data of them.
for example lets take this example:
load satdata;
pt = cvpartition(satClass,'holdout',0.3);
predTrain = satData(training(pt),:);
classTrain = satClass(training(pt));
predValid = satData(test(pt),:);
classValid = satClass(test(pt));
knnClassifier = fitcknn(predTrain,classTrain,'Numneighbors',5);
yPred = predict(knnClassifier,predValid);
[c,lbls] = confusionmat(yPred,classValid);
Here i can only see the classes in yPred but i cant see the input data (predictors) of those classes. I hope i am clear to my question.
  3 件のコメント
Saeed Magsi
Saeed Magsi 2022 年 9 月 17 日
load fisheriris
pt = cvpartition(species,'holdout',0.3);
predTrain = meas(training(pt),:);
classTrain = species(training(pt));
predValid = meas(test(pt),:);
classValid = species(test(pt));
knnClassifier = fitcknn(predTrain,classTrain,'Numneighbors',5);
yPred = predict(knnClassifier,predValid);
[c,lbls] = confusionmat(yPred,classValid);
Sorry for uploading the wrong code. I have now written a matlab documentation example code for knn classifier.
Saeed Magsi
Saeed Magsi 2022 年 9 月 17 日
編集済み: Saeed Magsi 2022 年 9 月 17 日
I have now attached the excel files for yPred and classTrain. If you see in row 23 of yPred it has incorrect prediction of "virginica". Now i want to see its input data (predictors) which i dont know how can this be done? I need your help in this. Thank you once again.

サインインしてコメントする。

採用された回答

Dinesh
Dinesh 2023 年 6 月 8 日
Hi Saeed!
One simple way to find the incorrect prediction is to iterate over the predictions and store the indices that are not equal in yPred and classValid. Using that indices array we can get the input data predictors for which the predictions were wrong.
indexes = [];
for i = 1:length(yPred)
% Compare values and store index if they don't match
if ~strcmp(yPred{i}, classValid{i})
indexes(end+1) = i;
end
end
data = predValid(indexes, :);
disp(data);
You can do this without for loop also. Use the 'cellfun' fuction
res = cellfun(@isequal, yPred, classValid);
% Find the indexes of non-matching elements
indexes = find(~res);
indexes
data = predValid(indexes, :);
disp(data);
Please refer to the following MATLAB documentation for more details and examples to use cellfun
Hope this helps!
Thank you.

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by