3 Nisan 2018 Salı

K-means Clustering

Giriş
Açıklaması şöyle.
K-means clustering is a type of unsupervised learning, which is used when you have unlabeled data (i.e., data without defined categories or groups). The goal of this algorithm is to find groups in the data, with the number of groups represented by the variable K.   
K Seçimi
Algoritmaya girdi olarak K sayısı ve veri seti verilir. K sayısı çeşitki aralıklarda denenerek en iyi sonuç bulunur.

Örnek
Sadece tek bir döngüyü gösteren kod için şöyle yaparız. İlk başta her cluster'ın centeroid' değeri 0'dır. Pikseller eklendikçe centeriod'ler kaymaya başlar. Bu döngüyü her cluster'ın centeroid sürekli değiştiği için belli bir eşik değerinden küçük oluncaya kadar tekrarlamak gerekir.
vector<Cluster> clusters;

// Go through each pixel in the image
for (int i(0); i < image->getLength(); i++)
{
  // Calculate which cluster centroid the pixel is nearest to
  int closestClusterIndex(0);
  double dist;
  for (int j(0); j < clusters.size(); j++)
  {
    dist = clusters[j]->getDistanceTo(image->getPixel(i));
    if (dist < clusters[closestClusterIndex]->getDistanceTo(image->getPixel(i)))
    {
      closestClusterIndex = j;
    }
  }

  // Add the pixel to the nearest cluster
  clusters[closestClusterIndex]->addPixel(image->getPixel(i));
}

Hiç yorum yok:

Yorum Gönder