0
votes

Is it possible to implement a custom distance measure in Matlab "knnclassify" function? In particular, I am interested in classifying an example according to the distance between two vectors to be equal to histogram intersection (vectors are considered to be histograms). For two N-dimensional vectors, w1 and w2, the distance is:

dist(w1, w2)=sum_i_to_N min(w1(i), w2(i))
1
there is a class knnclassify. Do you want to know how to use that class for your problem, do you want to create your own class/function with more or less the same effect, or what is it that you are asking for? - The Minion
I want to use a different distance measure in knnclassify function. I hope the edited post is clearer. - cerebrou
@cerebrou: I have edited my answer. This should help - rayryeng

1 Answers

1
votes

By examining the source of knnclassify, this relies on using knnsearch. The parameter of the distance to use is supplied to this function when you look at the knnclassify source. By looking at knnsearch, you certainly can implement this function yourself. knnsearch allows you to specify a custom function as long as it can take in only two vectors of the same size. These vectors are from the data sets that you are applying knnclassify to. As such, create a new function or you can do it anonymously using either:

function [d] = histogramIntersection(w1, w2)
    d = sum(min([w1,w2],[],2));

... or you can do this anonymously:

f = @(w1,w2) sum(min([w1,w2],[],2));

However, what you're going to have to do to incorporate this into knnclassify is that you will have to modify the source and include an additional condition in the switch statement so that you can include histogram intersection as a choice. Once you do that, you can either provide @f as input into the knnsearch call, or make some room in the code and define a histogramIntersection method like above, then use @histogramIntersection as input into knnclassify. This input should replace the string that is input into knnclassify that specifies the kind of distance measure you want.


tl;dr: You can do it but you'll have to modify the knnclassify source if you want to do this. Alternatively, you can see what knnclassify is doing, then just pull out the relevant calls that pertain to just your case and place your custom histogram intersection method accordingly, create a new file and just run this file. That way you don't need to mess with MATLAB's original source.