Orange Forum • View topic - Custom ExamplesDistance class in python

Custom ExamplesDistance class in python

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

Custom ExamplesDistance class in python

Postby mattdrew » Fri Jan 20, 2006 22:15

I'm trying to create a Tanimoto distance calculator for use with the kNNLearner, but I can't seem to get it quite right.

Here's what I'm trying:

Code: Select all
import orange

class ExamplesDistance_Tanimoto(orange.ExamplesDistance):
    def __init__(self, *args):
        orange.ExamplesDistance.__init__(self, *args)
    def __call__(self, ex1, ex2):
        #Actual code doesn't matter
        return 0.8

class ExamplesDistanceConstructor_Tanimoto(orange.ExamplesDistanceConstructor):
    def __init__(self, *args):
        orange.ExamplesDistanceConstructor.__init__(self, *args)
    def __call__(self, *args):
        return ExamplesDistance_Tanimoto()

trainingData = orange.ExampleTable(trainingFile)
knn = orange.kNNLearner(k=16)
knn.distanceConstructor = ExamplesDistanceConstructor_Tanimoto()
classifier = knn(trainingData)

Here's the error I get:

Traceback (most recent call last):
File "", line 46, in ?
knn.distanceConstructor = ExamplesDistanceConstructor_Tanimoto()
TypeError: error setting 'kNNLearner.distanceConstructor'

Can someone tell me what I'm doing wrong?[/code]

Postby Janez » Fri Mar 10, 2006 16:37

You cannot subtype any Orange class, just those for which the callback interface is implemented - the classes listed in

The reason why you had to wait so long for the answer is that I decided to program the callback interface for ExamplesDistance and ExamplesDistance_Constructor, but ran into a stupid bug I didn't know how to find, so I kept postponing it.

Your code should now work.

Return to Questions & Support