# source:orange/docs/reference/rst/code/mds-scatterplot.py@9827:71697b7e31c7

Revision 9827:71697b7e31c7, 1.1 KB checked in by Miha Stajdohar <miha.stajdohar@…>, 2 years ago (diff)

Renamed EuclideanConstructor to Eucledean.

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
10table = Orange.data.Table("iris.tab")
11
12# Construct a distance matrix using Euclidean distance
13euclidean = Orange.distance.Euclidean(table)
14distance = Orange.core.SymMatrix(len(table))
15for i in range(len(table)):
16   for j in range(i + 1):
17       distance[i, j] = euclidean(table[i], table[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(table):
30   points.append((mds.points[i][0], mds.points[i][1], d.getclass()))
31
32# Paint each class separately
33for c in range(len(table.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.