The IDLmlKMeans class implements a K-means model that can be used for clustering purposes.

Example


; Read image dataset
File = filepath('rose.jpg', subdirectory=['examples', 'data'])
imgData = read_image(file)
 
; Reform our image into a 3 by n-pixels array
Data = reform(imgData, 3, (imgData.dim)[1]*(imgData.dim)[2])
 
; Normalize data
Normalizer = IDLmlVarianceNormalizer(data)
Normalizer.Normalize, data
 
; Define classifier. It contains 3 attributes (pixel RGB values)
; and we want to separate it into 6 different classes.
Classifier = IDLmlKMeans(3, 6)
 
; Train the classifier
For i=0, 100 do loss=Classifier.Train(data)
 
; Classify our image
Result = Classifier.Classify(data)
 
; Display result
!null = Image(imgData, title='Original Image', layout=[2,1,1])
!null = Image(reform(result, (imgData.dim)[1], (imgData.dim)[2]), $
  rgb_table=25, title='Clustered Image', layout=[2,1,2], /current)

Syntax


Result = IDLmlKMeans(Nattributes, Nclasses [, Keywords=Value])

Arguments


Nattributes

Specify the number of attributes that the input data will be required to have.

Nclasses

Specify the number of possible outputs.

Keywords


SEED (optional)

If repeatability is desired (such as for testing), set this keyword to the seed variable used to randomly initialize the weights.

Properties


CLASS_MAP

A hash that maps internal classification values to desired labels, if the model was defined using custom labels.

NATTRIBUTES

The number of input attributes the model requires.

NOUTPUTS

The number of possible outputs.

OUTPUTS

An array of possible outputs.

Methods


IDLmlKMeans::Classify


The IDLmlKMeans::Classify method assigns each example to an output class, returning an array of label results.

Syntax


Result = Obj->[IDLmlKMeans::]Classify(Features [, Keywords=Value])

Return Value


The method returns an array of class values that correspond to the data provided.

Arguments


Features

Specify an array of features of size n x m, where n is the number of attributes and m is the number of examples.

Keywords


LOSS (optional)

Set this keyword to a variable that will contain the loss result, which is the total euclidean distance of all clusters from their corresponding mean center.

IDLmlKMeans::Restore


The IDLmlKMeans::Restore static method restores the model from a file.

Syntax


Result = IDLmlKMeans.Restore(Filename)

Return Value


A reference to the object instance restored from the file.

Arguments


Filename

Specify the name of file to restore.

Keywords


None

IDLmlKMeans::Save


The IDLmlKMeans::Save method saves the model to a file.

Syntax


Obj->[IDLmlKMeans::]Save, Filename

Arguments


Filename

Specify the name of the file to save.

Keywords


None

IDLmlKMeans::Train


The IDLmlKMeans::Train method performs training on the model and the loss, which is a unitless number that indicates how closely the model fits the training data. Training is an iterative process and it can take tens or hundreds of calls to the Train method until the model becomes fully trained. Check the loss returned by this method on each iteration. Once it converges to a low and stable value, you will know that the model has been trained.

Syntax


Result = Obj->[IDLmlKMeans::]Train(Features [, Keywords=Value])

Return Value


This method returns the loss, which is the total euclidean distance of all clusters from their corresponding mean center.

Arguments


Features

Specify an array of features of size n x m, where n is the number of attributes and m is the number of examples.

Keywords


CALLBACK_FUNCTION (optional)

An optional string with the name of an IDL function to be called on each training iteration. The callback function must accept two arguments: loss and state. The callback function must return 1 (!true) if the training should perform another iteration, or 0 (!false) if it should stop training.

TRAIN_STATE (optional)

Specify optional user data to provide for the callback function.

Version History


8.7.1

Introduced

See Also


IDLmlAutoEncoder, IDLmlFeedForwardNeuralNetwork, IDLmlSoftmax, IDLmlSupportVectorMachineClassification, IDLmlSupportVectorMachineRegression