source: orange/docs/reference/rst/code/mds-scatterplot.py @ 9891:66ee6522a40f

Revision 9891:66ee6522a40f, 1.1 KB checked in by gregorr, 2 years ago (diff)

SymMatrix documentation (data -> misc).

Line 
1# Description: 2D MDS scatterplot with Euclid distance, 100 optimization steps
2# Category:    projection
3# Uses:        iris
4# Referenced:  Orange.projection.mds
5# Classes:     Orange.projection.mds.MDS
6
7import Orange
8
9# Load some data
10iris = Orange.data.Table("iris.tab")
11
12# Construct a distance matrix using Euclidean distance
13euclidean = Orange.distance.Euclidean(iris)
14distance = Orange.misc.SymMatrix(len(iris))
15for i in range(len(iris)):
16   for j in range(i + 1):
17       distance[i, j] = euclidean(iris[i], iris[j])
18
19# Run 100 steps of MDS optimization
20mds = Orange.projection.mds.MDS(distance)
21mds.run(100)
22
23# Initialize matplotlib
24import pylab
25colors = ["red", "yellow", "blue"]
26
27# Construct points (x, y, instanceClass)
28points = []
29for (i, d) in enumerate(iris):
30   points.append((mds.points[i][0], mds.points[i][1], d.getclass()))
31
32# Paint each class separately
33for c in range(len(iris.domain.class_var.values)):
34    sel = filter(lambda x: x[-1] == c, points)
35    x = [s[0] for s in sel]
36    y = [s[1] for s in sel]
37    pylab.scatter(x, y, c=colors[c])
38
39pylab.savefig('mds-scatterplot.py.png')
Note: See TracBrowser for help on using the repository browser.