Orange Forum • View topic - Need a little help implementing my own Learner/Classifier

Need a little help implementing my own Learner/Classifier

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

Need a little help implementing my own Learner/Classifier

Postby acameron » Mon Nov 16, 2009 18:01

I've done most of the work at this point, but there's some problems with Test Learners. This is the output when I try to change target class in the Test Learners GUI:

Code: Select all
Unhandled exception of type AttributeError occured at 11:58:28:
Traceback:
  File: OWGUI.py, line 1397 in __call__
  Code: f(**kwds)
    File: OWTestLearners.py, line 485 in changedTarget
    Code: self.recomputeCM()
      File: OWTestLearners.py, line 317 in recomputeCM
      Code: cm = orngStat.computeConfusionMatrices(self.results, classIndex = self.targetClass)
        File: orngStat.py, line 564 in confusionMatrices
        Code: tfpns = [ConfusionMatrix() for i in range(res.numberOfLearners)]
          AttributeError: 'NoneType' object has no attribute 'numberOfLearners'


My Widget code is in OWMED.py (located in /usr/lib/python2.6/dist-packages/orange/OrangeWidgets/Classify/) and my Learner/Classifier code is in orngMED.py (located in /usr/lib/python2.6/dist-packages/orange/).
Link to OWMED.py
Link to orngMED.py

Thanks in advance! Once I've figured this out, implementing more Learners/Classifiers will be much simpler, I'm sure.

Postby acameron » Wed Nov 18, 2009 19:58

I've made a few changes and run some tests. In an interactive Python session, I can create an object of type orngMED.MEDLearner just fine, and I can create an object of type orng.MEDClassifier by calling my MEDLearner object with an orange.ExampleTable object. Furthermore, I can classify an orange.Example object with my MEDClassifier's __call__ function, which returns an orange.Value object. I can also create a MEDClassifier directly by passing an ExampleTable as a parameter to a MEDLearner constructor.

Nevertheless, when connecting the OWMED widget to Test Learners (making sure to connect the Learner output to the Learner input), Test Learners isn't able to generate any classification statistics. There is a row in the Evaluation Results table whose method is "MED", but all of the other fields are empty, and the Evaluation Results I pass to Confusion Matrix seem to be empty (Confusion Matrix has no learners available to it).

When changing the target class in Test Learners, the following error is generated:

Code: Select all
Unhandled exception of type AttributeError occured at 13:50:04:
Traceback:
  File: OWGUI.py, line 1397 in __call__
  Code: f(**kwds)
    File: OWTestLearners.py, line 485 in changedTarget
    Code: self.recomputeCM()
      File: OWTestLearners.py, line 317 in recomputeCM
      Code: cm = orngStat.computeConfusionMatrices(self.results, classIndex = self.targetClass)
        File: orngStat.py, line 564 in confusionMatrices
        Code: tfpns = [ConfusionMatrix() for i in range(res.numberOfLearners)]
          AttributeError: 'NoneType' object has no attribute 'numberOfLearners'


Here are links to the code in the relevant files:
orngMED.py
OWMED.py

Why isn't my Learner or Classifier working properly with Orange? Thanks in advance.

Postby Ales » Thu Nov 19, 2009 12:06

First you need to define the MEDLearner.__new__ with an extra argument weight, like this:
Code: Select all
def __new__(self, examples=None, weight=0):


Second, in the MEDClassifier.__call__ you need to respect the format argument. Just add this code to the end
Code: Select all
      probs = lambda cls: [1.0 if cls == mean.getclass()  else 0.0 for mean in self.classMeans]
      if format == orange.GetValue:
         return minclass.getclass()
      elif format == orange.GetBoth:
         return minclass.getclass(), probs(minclass.getclass())
      else:
         return probs(minclass.getclass())

Postby acameron » Thu Nov 19, 2009 16:40

Thanks for the help. Here are the files as they now stand:

OWMED.py
orngMED.py

Unfortunately, I'm still getting the same problem. Test Learners shows an entry for MED, but has no classification statistics for it. (Changing the target class produces the same error output I've pasted in my previous messages.)

On inspecting this tutorial, it seems my __new__ method is slightly different - the one in the tutorial is highlighted here for convenience. Unfortunately, that doesn't seem to be the solution either, as Python gives me a TypeError and says that that constructor is unsafe.

Postby Ales » Fri Nov 20, 2009 12:02

The Test Learners widget suppressed an exception in the learning process (division by zero). Use this code in calcMeans:
Code: Select all
curMean[j] /= len(classSamples) or 1

instead of
Code: Select all
curMean[j] /= len(classSamples)


I have also fixed the widget so it now issues an appropriate warning.

Postby acameron » Fri Nov 20, 2009 20:46

Brilliant; thank you very much! Looks like I need to pay a little more attention to edge cases in future.


Return to Questions & Support



cron