02/06/12 13:49:11 (2 years ago)
Jure Zbontar <jure.zbontar@…>

Started work on porting Classifiers in Python.

1 edited


  • docs/reference/rst/Orange.classification.rst

    r9666 r9708  
    5656              tuple with both 
     58You can often program learners and classifiers as classes or functions 
     59written entirely in Python and independent from Orange, as shown in 
     60Orange for Beginners. Such classes can participate, for instance, in 
     61the common evaluation functions like those available in modules orngTest 
     62and orngStat. 
     64On the other hand, these classes can't be used as components for pure C++ 
     65classes. For instance, TreeLearner's attribute nodeLearner should contain 
     66a (wrapped) C++ object derived from Learner, such as MajorityLearner 
     67or BayesLearner, and Variables's getValueFrom can only store classes 
     68derived from Classifier, like for instance ClassifierFromVar. They cannot 
     69accommodate Python's classes or even functions. 
     71There's a workaround, though. You can subtype Orange classes Learner 
     72or Classifier as if the two classes were defined in Python, but later 
     73use your derived Python classes as if they were written in Orange's 
     74core. That is, you can define your class in a Python script like this: 
     76    class MyLearner(orange.Learner):  
     77        def __call__(self, examples, weightID = 0):  
     78            <do something smart here> 
     80Such a learner can then be used as any regular learner written in 
     81Orange. You can, for instance, construct a tree learner and use your 
     82learner to learn node classifier: 
     84    treeLearner = orange.TreeLearner() 
     85    treeLearner.nodeLearner = MyLearner() 
     87If your learner or classifier is simple enough, you even don't need 
     88to derive a class yourself. You can define the learner or classifier 
     89as an ordinary Python function and assign it to an attribute of Orange 
     90class that would expect a Learner or a Classifier. Wrapping into a class 
     91derived from Learner or Classifier is done by Orange. ::  
     93    def myLearner(examples, weightID = 0):  
     94        <do something less smart here> 
     96    treeLearner = orange.TreeLearner() 
     97    treeLearner.nodeLearner = myLearner 
     99Finally, if your learner is really simple (that is, trivial :-), you 
     100can even stuff it into a lambda function. :: 
     102    treeLearner = orange.TreeLearner() 
     103    treeLearner.nodeLearner = lambda examples, weightID = 0: <do something trivial> 
     105Detailed description of the mechanisms involved and example scripts are 
     106given in a separate documentation on subtyping Orange classes in Python. 
    58108Orange contains implementations of various classifiers that are described in 
    59109detail on separate pages. 
Note: See TracChangeset for help on using the changeset viewer.