Orange Forum • View topic - Error in example

Error in example

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

Error in example

Postby rixder » Sat Jan 17, 2009 21:52

Hi!
First of all... Thanks for the great software :-)
I try run example from c_nb.htm :


Code: Select all
C:\Orange>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import orange, bayes
>>> data = orange.ExampleTable("voting")
>>> classifier = bayes.Learner(data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python25\lib\site-packages\bayes.py", line 11, in __new__
    return learner(examples)
  File "C:\Python25\lib\site-packages\bayes.py", line 54, in __call__
    pc[i][j][k] = (pc[i][j][k] + self.m * p_class[k])/(n_class[k] + self.m)
AttributeError: 'Learner' object has no attribute 'm'
>>>


Any ideas?

Best Regards...

Postby Blaz » Sun Mar 01, 2009 18:35

I was never too confident in using __new__ as a class method. Previous implementation of bayes.py (wrongly) had a function __new__ within Learner class defined as:

Code: Select all
    def __new__(cls, examples=None, **kwds):
        learner = object.__new__(cls, **kwds)
        if examples:
            return learner(examples)
        else:
            return learner


Hence, when examples where not None (e.g., when the data set was given just like in your example), function __init__ was never called but went to __call__ directly. To correct this, here's a new code:

Code: Select all
    def __new__(cls, examples=None, **kwds):
        learner = object.__new__(cls, **kwds)
        if examples:
            learner.__init__(**kwds)
            return learner(examples)
        else:
            return learner


I have updated the examample and documentation accordingly. Thanks for reporting this bug.[/code]


Return to Questions & Support



cron