Error using gather when using predict (from ClassificationKNN)
5 views (last 30 days)
Show older comments
Thanks in advance.
My code uses ClassfificationKNN to predict classes for my data. The error I get is as follows:
Error using gather
Too many input arguments.
Error in ClassificationKNN/score (line 451)
[CIDX,dist,gindex,W] = gather(CIDX,dist,gindex,W);
Error in ClassificationKNN/predict (line 777)
[posteriors,gindex,CIDX] = score(this,X);
Using the debugger, line 451 raises the issue when trying to Transfer distributed array.
Part of the code I use (portion pertaining to this question):
Note: attached is one sample of training and testing dataset since this is embededded in a for loop.
dist_measure = {'cosine'; 'correlation'; 'spearman';};
distMeas = dist_measure{2};
distWeight = 'inverse';
breakties = 'nearest';
nNeighbors1 = 1;
nNeighbors2 = 9;
training_labels1 = items(ind); %111x1 (1,2,3...28)
training_labels2 = category(ind); %111x1 (1,1,1...2,2,2)
true_label1 = items(indtest); %exemplar (e.g., 1)
true_label2 = category(indtest); %category (e.g., 1)
%from https://lvdmaaten.github.io/drtoolbox/
if DRflag
%% Performs out-of-sample extension of the new datapoints in points.
training_data = out_of_sample(training_data,mapping0); %111x11 (convert 11 objects into 11 feature vectors)
predicted_data = out_of_sample(predicted_data,mapping0); %1 x 11
end
mdl1 = fitcknn(training_data,training_labels1,'distance',distMeas,'DistanceWeight',distWeight,...
'NumNeighbors',nNeighbors1,'BreakTies',breakties); %item decoding
mdl2 = fitcknn(training_data,training_labels2,'distance',distMeas,'DistanceWeight',distWeight,...
'NumNeighbors',nNeighbors2,'BreakTies',breakties); %category decoding
predicted_label1 = predict(mdl1,predicted_data);
predicted_label2 = predict(mdl2,predicted_data);
2 Comments
Ayush Aniket
on 22 Aug 2023
I implemented the code you shared with the data provided as shown below:
dist_measure = {'cosine'; 'correlation'; 'spearman';};
distMeas = dist_measure{2};
distWeight = 'inverse';
breakties = 'nearest';
nNeighbors1 = 1;
nNeighbors2 = 9;
training_labels1 = load('training_labels1.txt');
training_labels2 = load('training_labels2.txt');
training_data = load('training_data.txt');
predicted_data = load('predicted_data.txt');
mdl1 = fitcknn(training_data,training_labels1,'distance',distMeas,'DistanceWeight',distWeight,...
'NumNeighbors',nNeighbors1,'BreakTies',breakties); %item decoding
mdl2 = fitcknn(training_data,training_labels2,'distance',distMeas,'DistanceWeight',distWeight,...
'NumNeighbors',nNeighbors2,'BreakTies',breakties); %category decoding
predicted_label1 = predict(mdl1,predicted_data);
predicted_label2 = predict(mdl2,predicted_data);
I am not getting any error. I got the following values as answer.
predicted_label1 = 12
predicted_label2 = 1
Let me know in case of any discrepancies.
Accepted Answer
Milan Bansal
on 31 Aug 2023
Hi,
I understand that you are getting an error: "Too many input arguments" while using MATLAB's “gather” function.
It looks like the MATLAB's “gather” function that you are using is being shadowed by some other function or script with the same name. You can check the list of all functions or files with the name “gather” in the MATLAB path by the running the command as shown below.
which gather -all
Rename the shadowed file or remove it from the MATLAB path to resolve the issue.
Refer to the documentation link below to know about MATLAB's “gather” function in different toolboxes.
3 Comments
More Answers (1)
chadows
on 20 Jun 2024
I have tried to delete those files under this path:
D:\MATLAB\toolbox\eeglab\plugins\ICLabel\matconvnet\matlab\compatibility\parallel\
but when I add
g = which('gather');
disp(g);
code in D:\MATLAB\toolbox\stats\classreg\ClassificationKNN.m line 452, I found "g" is still "D:\MATLAB\toolbox\eeglab\plugins\ICLabel\matconvnet\matlab\compatibility\parallel\gather.m".
Finally, I delete the path: D:\MATLAB\toolbox\eeglab\plugins\ICLabel\matconvnet\matlab\compatibility\parallel\.
Then I succeeded.
>> which('gather')
D:\MATLAB\toolbox\matlab\bigdata\gather.m
I wonder if there is any other better ways.
:)
0 Comments
See Also
Categories
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!