source: orange/orange/doc/Orange/rst/code/kmeans-trace.py @ 7130:12a00963a5e8

Revision 7130:12a00963a5e8, 1.1 KB checked in by markotoplak, 3 years ago (diff)

Added Makefile for doc/Orange.

Line 
1import orange
2import Orange.cluster
3import pylab
4import random
5
6def plot_scatter(data, km, attx, atty, filename="kmeans-scatter", title=None):
7    #plot a data scatter plot with the position of centeroids
8    pylab.rcParams.update({'font.size': 8, 'figure.figsize': [4,3]})
9    x = [float(d[attx]) for d in data]
10    y = [float(d[atty]) for d in data]
11    colors = ["c", "w", "b"]
12    cs = "".join([colors[c] for c in km.clusters])
13    pylab.scatter(x, y, c=cs, s=10)
14   
15    xc = [float(d[attx]) for d in km.centroids]
16    yc = [float(d[atty]) for d in km.centroids]
17    pylab.scatter(xc, yc, marker="x", c="k", s=200)
18   
19    pylab.xlabel(attx)
20    pylab.ylabel(atty)
21    if title:
22        pylab.title(title)
23    pylab.savefig("%s-%03d.png" % (filename, km.iteration))
24    pylab.close()
25
26def in_callback(km):
27    print "Iteration: %d, changes: %d, score: %8.6f" % (km.iteration, km.nchanges, km.score)
28    plot_scatter(data, km, "petal width", "petal length", title="Iteration %d" % km.iteration)
29   
30data = orange.ExampleTable("iris")
31random.seed(42)
32km = Orange.cluster.KMeans(data, 3, minscorechange=0, maxiters=10, inner_callback=in_callback)
Note: See TracBrowser for help on using the repository browser.