Orange Forum • View topic - Custom distance constructor for kNN classifier

Custom distance constructor for kNN classifier

A place to ask questions about methods in Orange and how they are used and other general support.

Custom distance constructor for kNN classifier

Postby has207 » Tue Feb 11, 2014 4:17

Hello all,

I'm trying to add custom distance constructor to the kNN classifier widget. I've plugged in the Python Script widget so I think can do something like:

Code: Select all
import copy
out_learner = copy.deepcopy(in_learner)
out_learner.distance_constructor = ???


I tried to code search and poke at the existing distance_constructor object (Orange.distance.Hamming 'hamming') but it seems to be in C++(?) and doesn't seem to have any likely methods I can override, so I can't seem to figure out how to create a custom one.

Any help greatly appreciated :)

Re: Custom distance constructor for kNN classifier

Postby Ales » Tue Feb 11, 2014 10:56

has207 wrote:I tried to code search and poke at the existing distance_constructor object (Orange.distance.Hamming 'hamming') but it seems to be in C++(?) and doesn't seem to have any likely methods I can override, so I can't seem to figure out how to create a custom one.

You have to override it's __call__ method (see the docs).
For instance
Code: Select all
class MyDistConstuctor(Orange.distance.DistanceConstructor):
    def __call__(self, data, weightID=None):
        return MyDistance(maybe_some_computed_parameters(data, weigthID))

class MyDistance(Orange.distance.Distance):
    def __call__(self, instance1, instance2):
        return ...

Re: Custom distance constructor for kNN classifier

Postby has207 » Tue Feb 11, 2014 12:53

Thanks! I'll give that a try.

I actually played around with the __call__ method but somehow couldn't figure it out. Excellent pointer to the docs as well, I googled around before asking and even downloaded the source and went grepping around, but still failed to find it :(


Return to Questions & Support