Question

I would like to use knnimpute to fill some missing values in my dataset. Thing is, I would like to use my own distance function, instead of the typical ones (Euclidean, Manhattan...).

For what I've read, knnimpute allows me to use a function handle, that calculates the distance according to Heterogeneous Euclidean-Overlap Metric (HEOM)

I've implemented this function as a regular function, but not as a handle function. So, I cannot use the distance matrix from my "normal" function, because this has to be done inside knnimpute, somehow, as a handler...

I'm confuse, can someone help me understand what I need to do?

Était-ce utile?

La solution

As long as your implementation of a distance function has the same signature as the standard distance functions, then you should be able to easily pass your function in.

From the knnimpute documentation (matlab knnimpute) it states that you can pass "A handle to a distance function, specified using @, for example, @distfun." It then refers the reader to the pdist function which provides more details (matlab pdist) about the custom distance function:

A distance function specified using @: D = pdist(X,@distfun) A distance function must be of form d2 = distfun(XI,XJ) taking as arguments a 1-by-n vector XI, corresponding to a single row of X, and an m2-by-n matrix XJ, corresponding to multiple rows of X. distfun must accept a matrix XJ with an arbitrary number of rows. distfun must return an m2-by-1 vector of distances d2, whose kth element is the distance between XI and XJ(k,:).

So as long as your distance function, as defined in your *.m file matches this signature and so can support these inputs, then there shouldn't be any problems.

Suppose that your distance function is in the mydistFunc.m file, and it's signature matches the above requirements, then all you should need to do is:

% call knnimpute with the data and your function
knnimpute(inputData,'Distance',@mydistFunc);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top