source: orange/Orange/doc/reference/SymMatrix.htm @ 9671:a7b056375472

Revision 9671:a7b056375472, 5.7 KB checked in by anze <anze.staric@…>, 2 years ago (diff)

Moved orange to Orange (part 2)

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<h1>Symmetric Matrices</h1>
7<index name="symmetric matrices">
8<index name="distance matrix">
9
10<P>Symmetric matrix, <CODE><INDEX name="classes/SymMatrix">SymMatrix</CODE> is a symmetric (square) matrix of size fixed at construction time (and stored to the attribute <CODE>dim</CODE>). The constructor expects a sequence of sequences (eg. a list of lists, a list of tuples...) or the matrix dimension. An optional additional argument gives the default value; the default is 0.</P>
11
12<P>For instance, a nice list to initialize the matrix with looks like this</P>
13<P class="header"><A href="hclust_art.py">hclust_art.py</A></P>
14<XMP class=code>m = [[],
15     [ 3],
16     [ 2,  4],
17     [17,  5,  4],
18     [ 2,  8,  3,  8],
19     [ 7,  5, 10, 11, 2],
20     [ 8,  4,  1,  5, 11, 13],
21     [ 4,  7, 12,  8, 10,  1,  5],
22     [13,  9, 14, 15,  7,  8,  4,  6],
23     [12, 10, 11, 15,  2,  5,  7,  3,  1]]
24matrix = orange.SymMatrix(m)
25</XMP>
26
27<P>This matrix, meant as a distance matrix, is used in example from the description of <a href="clustering.htm">hierarchical clustering</a>. <CODE>SymMatrix</CODE> also stores the diagonal elements; here they are not specified, so they are set to 0. The matrix needn't by so nice. If any line was shorter, the missing elements would be set to 0 as well. Any line could also be longer, spreading across the diagonal, in which case the constructor would check for asymmetries. For instance, if the matrix started by</P>
28<XMP class=CODE>m = [[],
29     [ 3,  0, f],
30     [ 2,  4],
31</XMP>
32<P>this would only be OK if <CODE>f</CODE> equals 2; otherwise, the matrix would be asymmetric.</P>
33
34<P>Finally, no line can be longer than the total number of lines. Here we have 10 rows, so no row may have more than 10 columns.</P>
35
36<P>So much for construction. Indexing is implemented so that order of indices is unimportant (unless set otherwise, see below), eg, if <CODE>m</CODE> is an instance of <CODE>SymMatrix</CODE>, then <CODE>m[2, 4]</CODE> addresses the same element as <CODE>m[4, 2]</CODE>.</P>
37
38<p class="header"><A href="random_classifier.py">symmatrix.py</a></p>
39<XMP class=code>import orange
40
41m = orange.SymMatrix(4)
42for i in range(4):
43    for j in range(i+1):
44        m[i, j] = (i+1)*(j+1)
45</XMP>
46
47<P>Although we set only the lower left half of the matrix (if we interpret the first index, i, as the row index), we have actually constructed a whole symmetric matrix.
48
49<XMP class=code>>>> print m
50(( 1.000,  2.000,  3.000,  4.000),
51 ( 2.000,  4.000,  6.000,  8.000),
52 ( 3.000,  6.000,  9.000, 12.000),
53 ( 4.000,  8.000, 12.000, 16.000))
54</XMP>
55
56<P>Other manipulations also respect the symmetricity, for instance, increasing an element <CODE>m[3, 2] += 15</CODE> will also increase <CODE>m[2, 3]</CODE> (since this is, in fact, one and the same element).</P>
57
58<P>The matrix has an attribute <CODE>matrixType</CODE> whose value can be set to <CODE>SymMatrix.Lower</CODE> (0), <CODE>SymMatrix.Upper</CODE> (1), <CODE>SymMatrix.Symmetric</CODE> (2, default), <CODE>SymMatrix.Lower_Filled</CODE> (3), <CODE>SymMatrix.Upper_Filled</CODE> (4). By setting it to <CODE>Lower</CODE> or <CODE>Upper</CODE>, we limit the matrix to the lower or upper half; attempts to index anything above or below the diagonal, respectively, will yield an error. With <CODE>Lower_Filled</CODE> and <CODE>Upper_Field</CODE>, the elements of the other half (upper or lower, respectively) still exist, but are set to zero and can be read, but cannot be modified. The matrix type is by default initially set to symmetric, but can be changed at any time. If it is, for instance, changed from lower to upper, the matrix gets transposed (actually, nothing really happens, the change only affects indexing (and printing) while the internal matrix representation stays the same, so changing the matrix type takes no time).</P>
59
60<P>If we, for instance, change the matrix type of the above matrix to <CODE>SymMatrix.Upper</CODE>, it gets printed as</P>
61<XMP class=code>>>> m.matrixType = m.Upper
62>>> print m
63(( 1.000,  2.000,  3.000,  4.000),
64 (         4.000,  6.000,  8.000),
65 (                 9.000, 12.000),
66 (                        16.000))
67</XMP>
68
69<P>Changing the type to <CODE>SymMatrix.Lower_Filled</CODE> will change the printout to</P>
70<XMP CLASS=code>>>> m.matrixType = m.Lower_Filled
71>>> print m
72(( 1.000,  0.000,  0.000,  0.000),
73 ( 2.000,  4.000,  0.000,  0.000),
74 ( 3.000,  6.000,  9.000,  0.000),
75 ( 4.000,  8.000, 12.000, 16.000))
76</XMP>
77
78<P>It is also possible to index entire rows by using a single index instead of two.</P>
79<XMP class=code>>>> print m[1]
80(3.0, 6.0, 9.0, 0.0)
81</XMP>
82
83<P>In the similar manner, you can iterate over the matrix using a for loop.</P>
84<XMP class=CODE>>>> m.matrixType = m.Lower
85>>> for row in m:
86...     print row
87(1.0,)
88(2.0, 4.0)
89(3.0, 6.0, 9.0)
90(4.0, 8.0, 12.0, 16.0)
91</XMP>
92
93<P>Slicing also works, but what you get by taking, for instance, <CODE>m[:3]</CODE> is a tuple containing the first three lines of the matrix (again represented as tuples).</P>
94
95<P>Started to wonder why always those annoying tuples and not lists that you can change as you will? To give you a clear message about one thing you cannot do with the matrix: you cannot change its contents by manipulating the rows you get by row indexing or slicing. Also, you cannot assign whole rows to matrices:</P>
96<XMP class=code>>>> m[1] = (0, 0, 0, 0)
97Traceback (most recent call last):
98  File "<interactive input>", line 1, in ?
99IndexError: two integer indices expected
100</XMP>
101
102<P>If you want to manipulate the row contents for your purposes, knowing that it doesn't change the matrix, convert it to list by calling <CODE>list(m[i])</CODE>.</P>
103
104</BODY></HTML>
Note: See TracBrowser for help on using the repository browser.