示例#1
0
/**
* Assigns points to one of the centroids 
* @param array $data the data points to cluster
* @param array $centroids The array of centroids
* @param int $k The number of clusters
*/
function assign_points($data, $centroids, $k)
{
    foreach ($data as $datum_index => $datum) {
        foreach ($centroids as $centroid) {
            $distances[$datum_index][] = dist($datum, $centroid);
        }
    }
    foreach ($distances as $distance_index => $distance) {
        $which_cluster = min_key($distance);
        $tentative_clusters[$which_cluster][] = $distance_index;
        $distances_from_clusters = array("{$distance_index}" => $distance);
    }
    //in case there's not enough clusters, take the farthest element from any of the cluster's centres
    //and make it a cluster.
    if (count($tentative_clusters) < $k) {
        $point_as_cluster = max_key($distances_from_clusters);
        foreach ($tentative_clusters as $tentative_index => $tentative_cluster) {
            foreach ($tentative_cluster as $tentative_element) {
                if ($tentative_element == $point_as_cluster) {
                    $clusters[$k + 1][] = $tentative_element;
                } else {
                    $clusters[$tentative_index][] = $tentative_element;
                }
            }
        }
    } else {
        $clusters = $tentative_clusters;
    }
    return $clusters;
}
示例#2
0
文件: iCacher.php 项目: Rpsl/iCacher
/**
 * Обрезание картинки от центра
 *
 * @param   Object    $T (phpThumb)
 * @param   string    $file
 * @param   mixed     $size
 * @param   string    $folder
 *
 * @return  string    $file_path
 */
function CropFromCenter($t, $file, $size, $folder = 'cc')
{
    if (empty($size[0]) or empty($size[1])) {
        $size[min_key($size)] = $size[max_key($size)];
    }
    $t->CropFromCenter($size[0], $size[1]);
    $save_size = SaveSize($size);
    $file = CACHE_FOLDER . '/' . $folder . '/' . $save_size . '/' . $file;
    return $file;
}