source: orange/docs/reference/rst/code/mds-advanced.py @ 9372:aef193695ea9

Revision 9372:aef193695ea9, 1.1 KB checked in by mitar, 2 years ago (diff)

Moved documentation to the separate directory.

Line 
1# Description: Advanced MDS test: 1000 optimization iterations, stress calculation after every 10th
2# Category:    projection
3# Uses:        iris
4# Referenced:  Orange.projection.mds
5# Classes:     Orange.projection.mds.MDS
6
7import Orange
8import math
9
10# Load some data
11table = Orange.data.Table("iris.tab")
12
13# Construct a distance matrix using Euclidean distance
14dist = Orange.core.ExamplesDistanceConstructor_Euclidean(table)
15matrix = Orange.core.SymMatrix(len(table))
16for i in range(len(table)):
17   for j in range(i+1):
18       matrix[i, j] = dist(table[i], table[j])
19
20# Run the Torgerson approximation and calculate stress
21mds = Orange.projection.mds.MDS(matrix)
22mds.Torgerson()
23mds.calc_stress(Orange.projection.mds.KruskalStress)
24
25# Optimization loop; calculate the stress only after each 10 optimization steps:
26for i in range(100):
27    old_stress = mds.avg_stress
28    for j in range(10):
29        mds.SMACOFstep()
30
31    mds.calc_stress(Orange.projection.mds.KruskalStress)
32    if old_stress * 1e-3 > math.fabs(old_stress - mds.avg_stress):
33        break
34
35# Print the points out
36for (p, e) in zip(mds.points, table):
37    print p, e
Note: See TracBrowser for help on using the repository browser.