- ZI is a 1-by-|N| vector containing one row of X or y.
- ZJ is an M2-by-|N| matrix containing multiple rows of X or y.
- D2 is an M2-by-|1| vector of distances, and D2(k) is the distance between observations ZI and ZJ(J,:).
using fitcknn in matlab
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi
I want to use fitcknn but with an implemented Distance metric, in my case levenshtein:
mdl = fitcknn(citynames,citycodes,'NumNeighbors', 50, 'exhaustive','Distance',@levenshtein);
This doesn't work, although it says in the Documentation "Distance metric, specified as the comma-separated pair consisting of 'Distance' and a valid distance metric string or function handle."
The error I get:
Error using internal.stats.parseArgs (line 42) Wrong number of arguments.
Error in classreg.learning.generator.Partitioner.processArgs (line 65) [cvpart,crossval,kfold,holdout,leaveout,~,otherArgs] = ...
Error in ClassificationKNN.fit (line 728) Nfold = classreg.learning.generator.Partitioner.processArgs(varargin{:});
Error in fitcknn (line 263) this = ClassificationKNN.fit(X,Y,varargin{:});
Error in NNlevenshtein (line 8) mdl = fitcknn(citynames,citycodes,'NumNeighbors', 50, 'exhaustive','Distance',@levenshtein);
0 comentarios
Respuestas (1)
Star Strider
el 6 de Dic. de 2014
We need to see your code for levenshtein.
According to the documentation, your levenshtein function has to have the form:
function D2 = DISTFUN(ZI,ZJ)
% calculation of distance
...
where
2 comentarios
Star Strider
el 6 de Dic. de 2014
Editada: Star Strider
el 6 de Dic. de 2014
I had to look up Levenshtein distance. It is designed to measure the number of letter changes in two strings that would convert one string to another. I don’t see any reason for it not to work in a knn classifier.
I had to review the documentation on fitcknn since I’ve not used it in a while. I’ve also never encountered a problem such as yours.
You likely don’t have to specify 'exhaustive' since according to the documentation, the routine will do that by default. If you do specify it, you have to precede it with 'NSMethod'. Its presence in the argument list without that is likely throwing the error.
See if:
mdl = fitcknn(citynames,citycodes, 'NumNeighbors',50, 'NSMethod','exhaustive', 'Distance',@levenshtein);
or
mdl = fitcknn(citynames,citycodes,'NumNeighbors', 50,'Distance',@levenshtein);
(without 'exhaustive') works.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!