Esempio n. 1
0
void Clusters::compute_centroids()
{
  time_t start_time = time(NULL);
  bool verbose = true;
  if(verbose) {
    cout << "Computing centroids..." << endl;
    cout << "Wiping old spread and centroid" << endl;
  }

  int i;
  for(i = 0; i < centroids.size(); i++) {
    spread[i] = 0;
    centroids[i].clear();
  }

  if(verbose) cout << "Computing new centroids" << endl;
  int iter_count = 0;
  for(int i = 0; i < nclusters; i++) {
    iter_count += recompute_centroid(i);
  }
  
  if(verbose)
    cout << "Done with centroid recompute.  (" 
         << difftime(time(NULL), start_time)
         << " sec, " << iter_count << " iter)" << endl;
}
Esempio n. 2
0
  void oskmeans::clusterize()
  {
    // initialize.
    initialize();

    if (_snippets.empty())
      return;

    // clustering.
    while (!stopping_criterion())
      {
#ifdef DEBUG
        std::cerr << "[Debug]:clusterize, iteration #" << _iterations << std::endl;
#endif

        for (short c=0; c<_K; c++)
          {
            // clear the cluster.
            _clusters[c].clear();
          }

        // clear the garbage cluster.
        _garbage_cluster.clear();

        // iterates points and associate each of them with a cluster.
        hash_map<uint32_t,hash_map<uint32_t,float,id_hash_uint>*,id_hash_uint>::const_iterator hit
        = _points.begin();
        while (hit!=_points.end())
          {
            float learning_rate = oskmeans::_nu0*pow((oskmeans::_nuf/oskmeans::_nu0),_t/static_cast<float>(_points.size()*oskmeans::_niterations));

#ifdef DEBUG
            std::cerr << "learning rate: " << learning_rate << std::endl;
#endif

            // find closest cluster to this point.
            short cl = assign_cluster((*hit).first,(*hit).second);

            // recomputation of centroids/medoids.
            if (cl != -1)
              {
                float cl_norm = 0.0;
                recompute_centroid(learning_rate,&_clusters[cl]._c,(*hit).second,cl_norm);
                normalize_centroid(&_clusters[cl]._c,cl_norm);
              }

            ++hit;
            _t++;
          }

        // count iteration.
        _iterations++;
      }
  }