double Linear_Kernel_Kmeans::BuildFastKernel() { // Compute the cluster centers, which are the average of its data points emptycenters=0; eval.Zero(); if(useMedian==false) { for(int i=0;i<n;i++) for(int j=0;j<m;j++) eval.p[labels[i]][j] += data.p[i][j]; for(int i=0;i<k;i++) { if(counts[i]>0) { double r = 1.0/counts[i]; for(int j=0;j<m;j++) eval.p[i][j] *= r; } else emptycenters++; } validcenters = k - emptycenters; RemoveEmptyClusters(); } else { Array2dC<double> t(k,*std::max_element(counts,counts+k)); Array2dC<int> added(1,k); for(int i=0;i<m;i++) // each dimension { added.Zero(); for(int j=0;j<n;j++) { t.p[labels[j]][added.buf[labels[j]]] = data.p[j][i]; added.buf[labels[j]]++; } for(int j=0;j<k;j++) { assert(added.buf[j]==counts[j]); std::sort(t.p[j],t.p[j]+added.buf[j]); if(added.buf[j]%2==0) eval.p[j][i] = (t.p[j][added.buf[j]/2-1]+t.p[j][added.buf[j]/2])/2.0; else eval.p[j][i] = t.p[j][added.buf[j]/2]; } } } return ComputeConstants(); }
// Cluster_Kmeans::Cluster() int Cluster_Kmeans::Cluster() { // First determine which frames are being clustered. Iarray const& FramesToCluster = FrameDistances().FramesToCluster(); // Determine seeds FindKmeansSeeds( FramesToCluster ); if (mode_ == RANDOM) RN_.rn_set( kseed_ ); int pointCount = (int)FramesToCluster.size(); // This array will hold the indices of the points to process each iteration. // If sequential this is just 0 -> pointCount. If random this will be // reassigned each iteration. Iarray PointIndices; PointIndices.reserve( pointCount ); for (int processIdx = 0; processIdx != pointCount; processIdx++) PointIndices.push_back( processIdx ); // Add the seed clusters for (Iarray::const_iterator seedIdx = SeedIndices_.begin(); seedIdx != SeedIndices_.end(); ++seedIdx) { int seedFrame = FramesToCluster[ *seedIdx ]; // A centroid is created for new clusters. AddCluster( ClusterDist::Cframes(1, seedFrame) ); // NOTE: No need to calc best rep frame, only 1 frame. if (debug_ > 0) mprintf("Put frame %i in cluster %i (seed index=%i).\n", seedFrame, clusters_.back().Num(), *seedIdx); } // Assign points in 3 passes. If a point looked like it belonged to cluster A // at first, but then we added many other points and altered our cluster // shapes, its possible that we will want to reassign it to cluster B. for (int iteration = 0; iteration != maxIt_; iteration++) { if (mode_ == RANDOM) ShufflePoints( PointIndices ); // Add each point to an existing cluster, and recompute centroid mprintf("\tRound %i: ", iteration); ProgressBar progress( PointIndices.size() ); int Nchanged = 0; int prog = 0; for (Iarray::const_iterator pointIdx = PointIndices.begin(); pointIdx != PointIndices.end(); ++pointIdx, ++prog) { if (debug_ < 1) progress.Update( prog ); int oldClusterIdx = -1; // if ( iteration != 0 || mode_ != SEQUENTIAL) // FIXME: Should this really happen for RANDOM // { int pointFrame = FramesToCluster[ *pointIdx ]; if (debug_ > 0) mprintf("DEBUG: Processing frame %i (index %i)\n", pointFrame, *pointIdx); bool pointWasYanked = true; if (iteration > 0) { // Yank this point out of its cluster, recompute the centroid for (cluster_it C1 = clusters_.begin(); C1 != clusters_.end(); ++C1) { if (C1->HasFrame( pointFrame )) { // If this point is alone in its cluster its in the right place if (C1->Nframes() == 1) { pointWasYanked = false; continue; // FIXME: should this be a break? } //oldBestRep = C1->BestRepFrame(); oldClusterIdx = C1->Num(); C1->RemoveFrameUpdateCentroid( Cdist_, pointFrame ); // TEST // C1->RemoveFrameFromCluster( pointFrame ); //newBestRep = C1->FindBestRepFrame(); // C1->CalculateCentroid( Cdist_ ); if (debug_ > 0) mprintf("Remove Frame %i from cluster %i\n", pointFrame, C1->Num()); //if (clusterToClusterCentroid_) { // if (oldBestRep != NewBestRep) // C1->AlignToBestRep( Cdist_ ); // FIXME: Only relevant for COORDS dist? // C1->CalculateCentroid( Cdist_ ); // FIXME: Seems unnessecary to align prior //} } } } else { // First iteration. If this point is already in a cluster it is a seed. for (cluster_it C1 = clusters_.begin(); C1 != clusters_.end(); ++C1) { if (C1->HasFrame( pointFrame )) { pointWasYanked = false; if (debug_ > 0) mprintf("Frame %i was already used to seed cluster %i\n", pointFrame, C1->Num()); continue; // FIXME break? } } } if (pointWasYanked) { // Find out what cluster this point is now closest to. double closestDist = -1.0; cluster_it closestCluster = clusters_.begin(); for (cluster_it C1 = clusters_.begin(); C1 != clusters_.end(); ++C1) { double dist = Cdist_->FrameCentroidDist(pointFrame, C1->Cent()); if (closestDist < 0.0 || dist < closestDist) { closestDist = dist; closestCluster = C1; } } //oldBestRep = closestCluster->BestRepFrame(); closestCluster->AddFrameUpdateCentroid( Cdist_, pointFrame ); // TEST // closestCluster->AddFrameToCluster( pointFrame ); //newBestRep = closestCluster->FindBestFrameFrame(); // closestCluster->CalculateCentroid( Cdist_ ); if (closestCluster->Num() != oldClusterIdx) { Nchanged++; if (debug_ > 0) mprintf("Remove Frame %i from cluster %i, but add to cluster %i (dist= %f).\n", pointFrame, oldClusterIdx, closestCluster->Num(), closestDist); } else { if (debug_ > 0) mprintf("Frame %i staying in cluster %i\n", pointFrame, closestCluster->Num()); } if (clusterToClusterCentroid_) { //if (oldBestRep != NewBestRep) { // C1->AlignToBestRep( Cdist_ ); // FIXME: Only relevant for COORDS dist? // C1->CalculateCentroid( Cdist_ ); // FIXME: Seems unnessecary to align prior //} } } // } } // END loop over points to cluster if (Nchanged == 0) { mprintf("\tK-means round %i: No change. Skipping the rest of the iterations.\n", iteration); break; } else mprintf("\tK-means round %i: %i points changed cluster assignment.\n", iteration, Nchanged); } // END k-means iterations // Remove any empty clusters // FIXME: Will there ever be empty clusters? RemoveEmptyClusters(); // NOTE in PTRAJ here align all frames to best rep return 0; }