Changeset 7743:5277d8c12ca8 in orange


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.

Location:
orange/doc/Orange/rst/code
Files:
5 edited

Legend:

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

    r7130 r7743  
    1 import orange 
    2 import Orange.cluster 
    31import random 
     2import Orange 
    43 
    54data_names = ["iris", "housing", "vehicle"] 
    6 data_sets = [orange.ExampleTable(name) for name in data_names] 
     5data_sets = [Orange.data.Table(name) for name in data_names] 
    76 
    87print "%10s %3s %3s %3s" % ("", "Rnd", "Div", "HC") 
    98for data, name in zip(data_sets, data_names): 
    109    random.seed(42) 
    11     km_random = Orange.cluster.KMeans(data, centroids = 3) 
    12     km_diversity = Orange.cluster.KMeans(data, centroids = 3, \ 
    13         initialization=Orange.cluster.kmeans_init_diversity) 
    14     km_hc = Orange.cluster.KMeans(data, centroids = 3, \ 
    15         initialization=Orange.cluster.KMeans_init_hierarchicalClustering(n=100)) 
     10    km_random = Orange.clustering.kmeans.Clustering(data, centroids = 3) 
     11    km_diversity = Orange.clustering.kmeans.Clustering(data, centroids = 3, 
     12        initialization=Orange.clustering.kmeans.init_diversity) 
     13    km_hc = Orange.clustering.kmeans.Clustering(data, centroids = 3, 
     14        initialization=Orange.clustering.kmeans.init_hclustering(n=100)) 
    1615    print "%10s %3d %3d %3d" % (name, km_random.iteration, km_diversity.iteration, km_hc.iteration) 
  • orange/doc/Orange/rst/code/kmeans-run-callback.py

    r7130 r7743  
    1 import orange 
    2 import Orange.cluster 
     1import Orange 
    32 
    43def callback(km): 
    54    print "Iteration: %d, changes: %d, score: %.4f" % (km.iteration, km.nchanges, km.score) 
    65     
    7 data = orange.ExampleTable("iris") 
    8 km = Orange.cluster.KMeans(data, 3, minscorechange=0, inner_callback=callback) 
     6table = Orange.data.Table("iris") 
     7km = Orange.clustering.kmeans.Clustering(table, 3, minscorechange=0, inner_callback=callback) 
  • orange/doc/Orange/rst/code/kmeans-run.py

    r7130 r7743  
    1 import orange 
    2 import Orange.cluster 
     1import Orange 
    32     
    4 data = orange.ExampleTable("iris") 
    5 km = Orange.cluster.KMeans(data, 3) 
     3table = Orange.data.Table("iris") 
     4km = Orange.clustering.kmeans.Clustering(table, 3) 
    65print km.clusters[-10:] 
  • orange/doc/Orange/rst/code/kmeans-silhouette.py

    r7130 r7743  
    1 import orange 
    2 import Orange.cluster 
     1import Orange 
    32 
    4 data = orange.ExampleTable("voting") 
    5 # data = orange.ExampleTable("iris") 
    6 for k in range(2,5): 
    7     km = Orange.cluster.KMeans(data, k, initialization=Orange.cluster.kmeans_init_diversity) 
    8     score = Orange.cluster.score_silhouette(km) 
     3table = Orange.data.Table("voting") 
     4# table = Orange.data.Table("iris") 
     5 
     6for k in range(2, 8): 
     7    km = Orange.clustering.kmeans.Clustering(table, k, initialization=Orange.clustering.kmeans.init_diversity) 
     8    score = Orange.clustering.kmeans.score_silhouette(km) 
    99    print k, score 
    1010 
    11 km = Orange.cluster.KMeans(data, 3, initialization=Orange.cluster.kmeans_init_diversity) 
    12 Orange.cluster.plot_silhouette(km, "kmeans-silhouette.png") 
     11km = Orange.clustering.kmeans.Clustering(table, 3, initialization=Orange.clustering.kmeans.init_diversity) 
     12Orange.clustering.kmeans.plot_silhouette(km, "kmeans-silhouette.png") 
  • 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.