Algorithm-KMeans
view release on metacpan or search on metacpan
lib/Algorithm/KMeans.pm view on Meta::CPAN
b9 0 7.60118206283120 5.05889245193079 5.82841781759102 ...
....
....
where the first column contains the symbolic ID tag for each data record and the rest
of the columns the numerical information. As to which columns are actually used for
clustering is decided by the string value of the mask. For example, if we wanted to
cluster on the basis of the entries in just the 3rd, the 4th, and the 5th columns
above, the mask value would be C<N0111> where the character C<N> indicates that the
ID tag is in the first column, the character C<0> that the second column is to be
ignored, and the C<1>'s that follow that the 3rd, the 4th, and the 5th columns are to
be used for clustering.
If you wish for the clusterer to search through a C<(Kmin,Kmax)> range of values for
C<K>, the constructor should be called in the following fashion:
my $clusterer = Algorithm::KMeans->new(datafile => $datafile,
mask => $mask,
Kmin => 3,
Kmax => 10,
cluster_seeding => 'smart', # try 'random' also
terminal_output => 1,
);
where obviously you can choose any reasonable values for C<Kmin> and C<Kmax>. If you
choose a value for C<Kmax> that is statistically too large, the module will let you
know. Again, you may choose C<random> for C<cluster_seeding>, the default value being
C<smart>.
If you believe that the variability of the data is very different along the different
dimensions of the data space, you may get better clustering by first normalizing the
data coordinates by the standard-deviations along those directions. When you set the
constructor option C<do_variance_normalization> as shown below, the module uses the
overall data standard-deviation along a direction for the normalization in that
direction. (As mentioned elsewhere in the documentation, such a normalization could
backfire on you if the data variability along a dimension is more a result of the
separation between the means than a consequence of the intra-cluster variability.):
my $clusterer = Algorithm::KMeans->new( datafile => $datafile,
mask => "N111",
K => 2,
cluster_seeding => 'smart', # try 'random' also
terminal_output => 1,
do_variance_normalization => 1,
);
=back
=head2 Constructor Parameters
=over 8
=item C<datafile>:
This parameter names the data file that contains the multidimensional data records
you want the module to cluster.
=item C<mask>:
This parameter supplies the mask to be applied to the columns of your data file. See
the explanation in Synopsis for what this mask looks like.
=item C<K>:
This parameter supplies the number of clusters you are looking for. If you set this
option to 0, that means that you want the module to search for the best value for
C<K>. (Keep in mind the fact that searching for the best C<K> may take a long time
for large data files.)
=item C<Kmin>:
If you supply an integer value for C<Kmin>, the search for the best C<K> will begin
with that value.
=item C<Kmax>:
If you supply an integer value for C<Kmax>, the search for the best C<K> will end at
that value.
=item C<cluster_seeding>:
This parameter must be set to either C<random> or C<smart>. Depending on your data,
you may get superior clustering with the C<random> option. The choice C<smart> means
that the clusterer will (1) subject the data to principal components analysis to
determine the maximum variance direction; (2) project the data onto this direction;
(3) find peaks in a smoothed histogram of the projected points; and (4) use the
locations of the highest peaks as seeds for cluster centers. If the C<smart> option
produces bizarre results, try C<random>.
=item C<use_mahalanobis_metric>:
When set to 1, this option causes Mahalanobis distances to be used for clustering.
The default is 0 for this parameter. By default, the module uses the Euclidean
distances for clustering. In general, Mahalanobis distance based clustering will
fail if your data resides on a lower-dimensional hyperplane in the data space, if you
seek too many clusters, and if you do not have a sufficient number of samples in your
data file. A necessary requirement for the module to be able to compute Mahalanobis
distances is that the cluster covariance matrices be non-singular. (Let's say your
data dimensionality is C<D> and the module is considering a cluster that has only
C<d> samples in it where C<d> is less than C<D>. In this case, the covariance matrix
will be singular since its rank will not exceed C<d>. For the covariance matrix to
be non-singular, it must be of full rank, that is, its rank must be C<D>.)
=item C<do_variance_normalization>:
When set, the module will first normalize the data variance along the different
dimensions of the data space before attempting clustering. Depending on your data,
this option may or may not result in better clustering.
=item C<terminal_output>:
This boolean parameter, when not supplied in the call to C<new()>, defaults to 0.
When set, you will see in your terminal window the different clusters as lists of the
symbolic IDs and their cluster centers. You will also see the QoC (Quality of
Clustering) values for the clusters displayed.
=item C<write_clusters_to_files>:
This parameter is also boolean. When set to 1, the clusters are written out to files
that are named in the following manner:
( run in 0.981 second using v1.01-cache-2.11-cpan-6aa56a78535 )