source: orange/orange/doc/reference/classifiersInPython.htm @ 6538:a5f65d7f0b2c

Revision 6538:a5f65d7f0b2c, 2.8 KB checked in by Mitar <Mitar@…>, 4 years ago (diff)

Made XPM version of the icon 32x32.

Line 
1<html> <HEAD>
2<LINK REL=StyleSheet HREF="../style.css" TYPE="text/css">
3<LINK REL=StyleSheet HREF="style-print.css" TYPE="text/css" MEDIA=print>
4</HEAD> <body>
5
6<index name="classifiers/in Python">
7<h1>Classifiers in Python</h1>
8
9<P>Classifiers and learners can be written in Python.</P>
10
11<P>You can often program them as classes or functions written entirely in Python and independent from Orange, as shown in  <A href="../ofb/c_nb_disc.htm">Orange for Beginners</A>. Such classes can participate, for instance, in the common evaluation functions like those available in modules <CODE>orngTest</CODE> and <CODE>orngStat</CODE>.</P>
12
13<P>On the other hand, these classes can't be used as components for pure C++ classes. For instance, <CODE>TreeLearner</CODE>'s attribute <CODE>nodeLearner</CODE> should contain a (wrapped) C++ object derived from <CODE>Learner</CODE>, such as <CODE>MajorityLearner</CODE> or <CODE>BayesLearner</CODE>, and <CODE>Variables</CODE>'s <CODE>getValueFrom</CODE> can only store classes derived from <CODE>Classifier</CODE>, like for instance <CODE>ClassifierFromVar</CODE>. They cannot accommodate Python's classes or even functions.</P>
14
15<P>There's a workaround, though. You can subtype Orange classes <CODE>Learner</CODE> or <CODE>Classifier</CODE> as if the two classes were defined in Python, but later use your derived Python classes as if they were written in Orange's core. That is, you can define your class in a Python script like this:</P>
16
17<XMP class=code>class MyLearner(orange.Learner):
18  def __call__(self, examples, weightID = 0):
19     < do something smart here >
20</XMP>
21
22<P>Such a learner can then be used as any regular learner written in Orange. You can, for instance, construct a tree learner and use your learner to learn node classifier:</P>
23
24<XMP class=code>treeLearner = orange.TreeLearner()
25treeLearner.nodeLearner = MyLearner()
26</XMP>
27
28<P>If your learner or classifier is simple enough, you even don't need to derive a class yourself. You can define the learner or classifier as an ordinary Python function and assign it to an attribute of Orange class that would expect a <CODE>Learner</CODE> or a <CODE>Classifier</CODE>. Wrapping into a class derived from <CODE>Learner</CODE> or <CODE>Classifier</CODE> is done by Orange.</P>
29
30<XMP class=code>def myLearner(examples, weightID = 0):
31  < do something less smart here >
32
33treeLearner = orange.TreeLearner()
34treeLearner.nodeLearner = myLearner
35</XMP>
36
37<P>Finally, if your learner is really simple (that is, trivial :-), you can even stuff it into a lambda function.</P>
38
39<XMP class=code>treeLearner = orange.TreeLearner()
40treeLearner.nodeLearner = lambda examples, weightID = 0: <do something trivial>
41</xmp>
42
43<P>Detailed description of the mechanisms involved and example scripts are given in a separate documentation on <A href="callbacks.htm">subtyping Orange classes in Python</A>.</P>
44
45</BODY>
46</HTML> 
Note: See TracBrowser for help on using the repository browser.