# source:orange/orange/doc/reference/CostMatrix.htm@6538:a5f65d7f0b2c

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

Made XPM version of the icon 32x32.

Line
1<html>
6
7<BODY>
8<index name="cost matrices">
9<h1>Cost Matrix</h1>
10
11<P><CODE><INDEX name="classes/CostMatrix">CostMatrix</CODE> is an object that stores costs of (mis)classifications. Costs can be either negative or positive.</P>
12
13<P class=section>Attributes</P>
14<DL class=attributes>
15<DT>classVar</DT>
16<DD>The (class) attribute to which the matrix applies. This can also be <CODE>None</CODE>.</DD>
17
19<DD>Matrix dimension, ie. number of classes.</DD>
20</DL>
21
22<P class=section>Methods</P>
23<DL class=attributes>
24<DT>CostMatrix(dimension[, default cost])</DT>
25<DD>Constructs a matrix of the given size and initializes it with the default cost (1, if not given). All elements of the matrix are assigned the given cost, except for the diagonal that have the default cost of 0. (Diagonal elements represent correct classifications and these usually have no price; you can, however, change this.)
26
28<XMP class=code style="margin-top: 0mm">import orange
29
30cm = orange.CostMatrix(3)
31print "classVar =", cm.classVar
32for pred in range(3):
33    for corr in range(3):
34        print cm.getcost(pred, corr),
35    print
36</XMP>
37
38<P>This initializes the matrix and print it out:</P>
39<XMP class=code>0.0 1.0 1.0
401.0 0.0 1.0
411.0 1.0 0.0
42</XMP>
43</P>
44</DD>
45
46
47<DT>CostMatrix(class descriptor[, default cost])</DT>
48<DD>Similar as above, except that <CODE>classVar</CODE> is also set to the given descriptor. The number of values of the given attribute (which must be discrete) is used for dimension.</P>
49
51(uses <a href="iris.tab">iris.tab</a>)</p>
52<XMP class=code style="margin-top: 0mm">data = orange.ExampleTable("iris")
53cm = orange.CostMatrix(data.domain.classVar, 2)
54</XMP>
55
56<P>This constructs a matrix similar to the one above (the class attribute in iris domain is three-valued) except that the matrix contains 2s instead of 1s.</P>
57</DD>
58
59<DT>CostMatrix([attribute descriptor, ]matrix)</DT>
60<DD>Initializes the matrix with the elements given as a sequence of sequences (you can mix lists and tuples if you find it funny). Each subsequence represents a row.</P>
61
63(uses <a href="iris.tab">iris.tab</a>)</p>
64<XMP class=code  style="margin-top: 0mm">cm = orange.CostMatrix(data.domain.classVar, [(0, 2, 1), (2, 0, 1), (2, 2, 0)])
65</XMP>
66
67<P>If you print this matrix out, will it look like this:</P>
68<XMP class=code>0.0 2.0 1.0
692.0 0.0 1.0
702.0 2.0 0.0
71</XMP>
72</DD>
73
74<DT>setcost(predicted value, correct value, cost)</DT>
75<DD>Set the misclassification cost.
76
77<P>The matrix above could be constructed by first initializing it with 2s and then changing the prices for virginica's into 1s.</P>
78
80(uses <a href="iris.tab">iris.tab</a>)</p>
81<XMP class=code  style="margin-top: 0mm">cm = orange.CostMatrix(data.domain.classVar, 2)
82cm.setcost("Iris-setosa", "Iris-virginica", 1)
83cm.setcost("Iris-versicolor", "Iris-virginica", 1)
84</XMP>
85</Dd>
86
87<DT>getcost(predicted value, correct value)</DT>
88<DD>Returns the cost of prediction. Values must be integer indices; if <CODE>classVar</CODE> is set, you can also use symbolic values (strings).</P>
89</DD>
90</DL>
91
92<P>Note that there's no way to change the size of the matrix. Size is set at construction and does not change.</P>
93
94<P>For the final example, we shall compute the profits of knowing attribute values in the dataset lenses with the same cost-matrix as printed above.</P>
95
97(uses <a href="lenses.tab">lenses.tab</a>)</p>
98<XMP class=code>data = orange.ExampleTable("lenses")
99meas = orange.MeasureAttribute_cost()
100meas.cost = ((0, 2, 1), (2, 0, 1), (2, 2, 0))
101for attr in data.domain.attributes:
102    print "%s: %5.3f" % (attr.name, meas(attr, data))
103</xmp>
104
105<P>As the script shows, you don't have to (and usually won't) call the constructor explicitly. Instead, you will set the corresponding field (in our case <CODE>meas.cost</CODE>) to a matrix and let Orange convert it to <CODE>CostMatrix</CODE> automatically.</P>
106
107<P>Funny as it might look, but since Orange uses constructor to perform such conversion, even the above statement is correct (although the cost matrix is rather dull, with 0s on the diagonal and 1s around):</P>
108
109<XMP class=code>meas.cost = data.domain.classVar
110</XMP>
111
112</BODY></HTML>
Note: See TracBrowser for help on using the repository browser.