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