source: orange/orange/doc/Orange/rst/code/kmeans-trace.py @ 7743:5277d8c12ca8

Revision 7743:5277d8c12ca8, 1.1 KB checked in by lanz <lan.zagar@…>, 3 years ago (diff)

Corrected the (broken) example scripts for kmeans.

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