Example of K-Means Clustering

Example 35.1. K-Means Clustering

####################################################################
#
#                        AdvancedMiner example script
#            Copyright Algolytics sp. z o. o. 2004-2015
#
#
# Subject:
#   Clustering
#
# Algorithm:
#   k-Means
#         
# Remarks:
#   This script uses some external java packages for managing the plot
#
####################################################################
from java.awt import Color
from org.jfree.chart.annotations import XYPointerAnnotation

 # Data preparation
table 'kmean_test':
    x   y
    5   5
    9   8
    13  7
    5   12
    10  16
    15  11
    34  22
    39  21
    31  27
    36  26
    42  27
    32  30
    37  30
    16  30
    17  28
    15  31
    18  32
    14  25

 # Create physical and logical data for model building
pd = PhysicalData('kmean_test')
save('kmean_pd', pd)
ld = LogicalData(pd)
save('kmean_ld', ld)

 # Create clustering function settings and k-Means algorithm settings
fs = ClusteringFunctionSettings()
as = KMeansSettings()
 # Set ForceMaxIterations to false, so the algorithm will stop
 #  when the membership matrix will not change anymore
as.setForceMaxIterations(FALSE)

 # set seed to receive always the same result

as.initializationProperties= KMeansInitializationProperties(KMeansInitializationPropertiesType.randomDataPoints)
as.initializationProperties.seed = 1250



 # We assume that there are three groups in the dataset
as.setClusterCount(3)

 # Assign the prepared algorithm settings to function settings
fs.setAlgorithmSettings(as)

 # Now the prepared objects can be saved and executed
fs.setLogicalData(ld)
save('kmean_fs', fs)
bt = MiningBuildTask('kmean_pd', 'kmean_fs', 'output_model')
save('kmean_bt', bt)
execute('kmean_bt')

 # After build task execution, apply task is prepared.
pdb = PhysicalData('kmean_outtable');
save('kmean_apply_target', pdb)

at = MiningApplyTask()
at.setSourceDataName('kmean_pd')
at.setModelName('output_model')
at.setTargetDataName('kmean_apply_target')
at.setReplaceExistingData(TRUE)

 # Preparation of output type, we use clustering output type
cao = ClusteringApplyOutput()

 #We want to see the probability that a given sample belong to each of
 # the three clusters
ce1 = ClusterIdItem()
ce1.setSelectedClusterId(0)
ce1.setDestinationName('pro1')
ce1.setOutputType(ClusteringOutputType.probability)

ce2 = ClusterIdItem()
ce2.setDestinationName('pro2')
ce2.setSelectedClusterId(1)
ce2.setOutputType(ClusteringOutputType.probability)

ce3 = ClusterIdItem()
ce3.setDestinationName('pro3')
ce3.setSelectedClusterId(2)
ce3.setOutputType(ClusteringOutputType.probability)

cao.item.add(ce1)
cao.item.add(ce2)
cao.item.add(ce3)

 # It is useful to copy x and y sample coordinates from source data
 #  to target dataset
at.directMapping.add(ApplySourceItem('x','x'))
at.directMapping.add(ApplySourceItem('y','y'))
at.setApplyOutput(cao)

 # Now the object can be saved and executed
save('kmean_at', at)
execute('kmean_at')

 # After data applying some conversion should be done to the result
 # dataset

trans 'draw_test' <- 'kmean_outtable':
 # pro1, pro2, pro3 columns are droped out from results
    drop out pro1
    drop out pro2
    drop out pro3
 # New column Class is added to the output table, so a series can
 # be drawn
    if pro1==1 :
        Class='a'
    if pro2==1 :
        Class='b'
    if pro3==1 :
        Class='c'

 # The created table is the source for the "Data" object. It is
 # important that 'Class' is in first place, beacuse of its
 # nominal type

d = Data('draw_test', ['Class', 'x', 'y'])
plot=ScatterPlot(d)
plot.setTitle('Clustering example')
plot.show()

 # Now we want to gather some statistics from the built model
model = load('output_model')
ms = model.getModelStatistics()
clusters = ms.getClusters()

 # get JFreeChart object from the current plot
plot1 = plot.getJFreeChart().getPlot()

 # for each cluster...
for cluster in clusters:
    cc = cluster.getCentroidCoordinates()
    xc = cc[0].getValue()
    yc = cc[1].getValue()
    # draw annotation containing the information about cluster members
    # , point it to cluster centroid

    plot1.addAnnotation(XYPointerAnnotation(cluster.getName()+" center ["+repr(cluster.\
getMembersCount())+"]", xc, yc, -45))

Figure 35.11. Result of k-means clustering example

Result of k-means clustering example