Ignore:
Timestamp:
03/15/11 14:57:05 (3 years ago)
Author:
lanz <lan.zagar@…>
Branch:
default
Convert:
a4d5dd3bc47b1cea11932b6f17ec5c28b3b34b4c
Message:

Corrected the (broken) example scripts for kmeans.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orange/doc/Orange/rst/code/kmeans-trace.py

    r7130 r7743  
    1 import orange 
    2 import Orange.cluster 
    3 import pylab 
    41import random 
    52 
    6 def plot_scatter(data, km, attx, atty, filename="kmeans-scatter", title=None): 
     3from matplotlib import pyplot as plt 
     4import Orange 
     5 
     6 
     7def plot_scatter(table, km, attx, atty, filename="kmeans-scatter", title=None): 
    78    #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] 
     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] 
    1112    colors = ["c", "w", "b"] 
    1213    cs = "".join([colors[c] for c in km.clusters]) 
    13     pylab.scatter(x, y, c=cs, s=10) 
     14    plt.scatter(x, y, c=cs, s=10) 
    1415     
    1516    xc = [float(d[attx]) for d in km.centroids] 
    1617    yc = [float(d[atty]) for d in km.centroids] 
    17     pylab.scatter(xc, yc, marker="x", c="k", s=200) 
     18    plt.scatter(xc, yc, marker="x", c="k", s=200) 
    1819     
    19     pylab.xlabel(attx) 
    20     pylab.ylabel(atty) 
     20    plt.xlabel(attx) 
     21    plt.ylabel(atty) 
    2122    if title: 
    22         pylab.title(title) 
    23     pylab.savefig("%s-%03d.png" % (filename, km.iteration)) 
    24     pylab.close() 
     23        plt.title(title) 
     24    plt.savefig("%s-%03d.png" % (filename, km.iteration)) 
     25    plt.close() 
    2526 
    2627def in_callback(km): 
    2728    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    plot_scatter(table, km, "petal width", "petal length", title="Iteration %d" % km.iteration) 
    2930     
    30 data = orange.ExampleTable("iris") 
     31table = Orange.data.Table("iris") 
    3132random.seed(42) 
    32 km = Orange.cluster.KMeans(data, 3, minscorechange=0, maxiters=10, inner_callback=in_callback) 
     33km = Orange.clustering.kmeans.Clustering(table, 3, minscorechange=0, maxiters=10, inner_callback=in_callback) 
Note: See TracChangeset for help on using the changeset viewer.