source: orange/docs/reference/rst/code/freeviz-pca.py @ 10475:61c2249d671f

Revision 10475:61c2249d671f, 1.6 KB checked in by Matija Polajnar <matija.polajnar@…>, 2 years ago (diff)

Major refactorization of linear projections, fixing some bugs in the process.

Line 
1# Description: FreeViz projector
2# Category:    projection
3# Uses:        zoo
4# Referenced:  Orange.projection.linear
5# Classes:     Orange.projection.linear.FreeViz, Orange.projection.linear.Projector
6
7import Orange
8import numpy as np
9
10tab = Orange.data.Table('titanic')
11
12ind = Orange.data.sample.SubsetIndices2(p0=0.99)(tab)
13train, test = tab.select(ind, 0), tab.select(ind, 1)
14
15freeviz = Orange.projection.linear.FreeViz()
16freeviz.graph.set_data(train)
17freeviz.show_all_attributes()
18
19def mirror(tab):
20    a = tab.to_numpy("a")[0]
21    rotate = np.diagflat([-1 if val<0 else 1 for val in a[0]])
22    a = np.dot(a, rotate)
23    return Orange.data.Table(tab.domain, a)
24
25print "PCA"
26freeviz.find_projection(Orange.projection.linear.DR_PCA, set_anchors=True)
27projector = freeviz()
28for e, projected in zip(test, mirror(projector(test))):
29    print e, projected
30
31print "SPCA"
32freeviz.find_projection(Orange.projection.linear.DR_SPCA, set_anchors=True)
33projector = freeviz()
34for e, projected in zip(test, mirror(projector(test))):
35    print e, projected
36
37print "SPCA w/out generalization"
38freeviz.use_generalized_eigenvectors = False
39freeviz.find_projection(Orange.projection.linear.DR_SPCA, set_anchors=True)
40projector = freeviz()
41for e, projected in zip(test, mirror(projector(test))):
42    print e, projected
43
44print "PCA with 2 attributes"
45freeviz.graph.anchor_data = [(0,0, a.name) for a in freeviz.graph.data_domain
46                                                    .attributes[:2]]
47freeviz.find_projection(Orange.projection.linear.DR_PCA, set_anchors=True)
48projector = freeviz()
49for e, projected in zip(test, mirror(projector(test))):
50    print e, projected
Note: See TracBrowser for help on using the repository browser.