|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace NlpTools\Clustering; |
| 4 | + |
| 5 | +use NlpTools\Similarity\Distance; |
| 6 | +use NlpTools\Clustering\CentroidFactories\CentroidFactory; |
| 7 | +use NlpTools\Documents\TrainingSet; |
| 8 | +use NlpTools\FeatureFactories\FeatureFactory; |
| 9 | + |
| 10 | +/** |
| 11 | + * This clusterer uses the KMeans algorithm for clustering documents. |
| 12 | + * It accepts as parameters the number of clusters and the distance metric |
| 13 | + * as well as the methodology for computing the new centroids (thus it |
| 14 | + * can be used to cluster documents in spaces other than the euclidean |
| 15 | + * vector space). |
| 16 | + * A description of this algorithm can be found at |
| 17 | + * http://en.wikipedia.org/wiki/K-means_clustering |
| 18 | + */ |
| 19 | +class KMeans extends Clusterer |
| 20 | +{ |
| 21 | + protected $dist; |
| 22 | + protected $centroidF; |
| 23 | + protected $n; |
| 24 | + protected $cutoff; |
| 25 | + |
| 26 | + /** |
| 27 | + * Initialize the K Means clusterer |
| 28 | + * |
| 29 | + * @param int $n The number of clusters to compute |
| 30 | + * @param Distance $d The distance metric to be used (Euclidean, Hamming, ...) |
| 31 | + * @param CentroidFactory $cf This parameter will be used to create the new centroids from a set of documents |
| 32 | + * @param float $cutoff When the maximum change of the centroids is smaller than that stop iterating |
| 33 | + */ |
| 34 | + public function __construct($n, Distance $d, CentroidFactory $cf, $cutoff=1e-5) { |
| 35 | + $this->dist = $d; |
| 36 | + $this->n = $n; |
| 37 | + $this->cutoff = $cutoff; |
| 38 | + $this->centroidF = $cf; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Apply the feature factory to the documents and then cluster the resulting array |
| 43 | + * using the provided distance metric and centroid factory. |
| 44 | + */ |
| 45 | + public function cluster(TrainingSet $documents, FeatureFactory $ff) { |
| 46 | + // transform the documents according to the FeatureFactory |
| 47 | + $docs = $this->getDocumentArray($documents,$ff); |
| 48 | + |
| 49 | + // choose N centroids at random |
| 50 | + $centroids = array(); |
| 51 | + foreach (array_rand($docs,$this->n) as $key) { |
| 52 | + $centroids[] = $docs[$key]; |
| 53 | + } |
| 54 | + |
| 55 | + // cache the distance and centroid factory functions for use |
| 56 | + // with closures |
| 57 | + $dist = array($this->dist,'dist'); |
| 58 | + $cf = array($this->centroidF,'getCentroid'); |
| 59 | + |
| 60 | + // looooooooop |
| 61 | + while (true) |
| 62 | + { |
| 63 | + // compute the distance each document has from our centroids |
| 64 | + // the array is MxN where M = count($docs) and N = count($centroids) |
| 65 | + $distances = array_map( |
| 66 | + function ($doc) use(&$centroids,$dist) { |
| 67 | + return array_map( |
| 68 | + function ($c) use($dist,$doc) { |
| 69 | + return call_user_func($dist,&$c,&$doc); |
| 70 | + //return $dist($c,$doc); |
| 71 | + }, |
| 72 | + $centroids |
| 73 | + ); |
| 74 | + }, |
| 75 | + $docs |
| 76 | + ); |
| 77 | + |
| 78 | + // initialize the empty clusters |
| 79 | + $clusters = array_fill_keys( |
| 80 | + array_keys($centroids), |
| 81 | + array() |
| 82 | + ); |
| 83 | + foreach ($distances as $idx=>$d) { |
| 84 | + // assign document idx to the closest centroid |
| 85 | + $clusters[array_search(min($d),$d)][] = $idx; |
| 86 | + } |
| 87 | + |
| 88 | + // compute the new centroids from the assigned documents |
| 89 | + // using the centroid factory function |
| 90 | + $new_centroids = array_map( |
| 91 | + function ($cluster) use(&$docs,$cf) { |
| 92 | + return call_user_func($cf,&$docs,$cluster); |
| 93 | + }, |
| 94 | + $clusters |
| 95 | + ); |
| 96 | + |
| 97 | + // compute the change each centroid had from the previous one |
| 98 | + $changes = array_map( |
| 99 | + $dist, |
| 100 | + $new_centroids, |
| 101 | + $centroids |
| 102 | + ); |
| 103 | + |
| 104 | + // if the largest change is small enough we are done |
| 105 | + if (max($changes)<$this->cutoff) { |
| 106 | + // return the clusters, the centroids and the distances |
| 107 | + return array($clusters,$centroids,$distances); |
| 108 | + } |
| 109 | + |
| 110 | + // update the centroids and loooooop again |
| 111 | + $centroids = $new_centroids; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
0 commit comments