3.K-means clustering - kNN & K-means algorithm

1 minute read

kNN algorithm



1. What is kNN - Nearest Neighbor algorithm


  • It is one of supervise train algorithm for classification or regression that look for train data of K number, where is mostly near by test data in feature space.


image


  • NN vs kNN
    • NN : Nearest neighbor (k=1)


image


  • KNN algorithm object creation


cv2.ml.KNearest_create() -> retval
  • retval : cv2.ml_KNearest object


  • Input data of classification using the KNN algorithm


cv.ml_KNearest.findNearest(samples, k, results=None, neighborResponses=None, dist=None, flgs=None) -> retval, results, neighborResposnes, dist
  • samples : Input sample matric with input vector stored by row unit


  • KNN algorithm poin classification example


image


2. KNN digit recognition


  • If it is the printed digit to setted font, Template matching is possible


image


  • KNN digit recognition processing
    • Making coordination of one point from 400-dimension using pixel value of image 20 x 20
    • KNN algorithm point classification in 400-dimension space


image


  • KNN digit recognition flow chart


image


2. k-means algorithm

  • k-means algorithm
    • Cluster algorithm the given data divides section of k number image
  • Process
    1. Select random k number center
    2. Select nearest center to every data
    3. Recalculate center to each cluster
    4. Repeat 2~3 process, If center changed
    5. If not, it’ll end


image


  • Color image segment
    • Express each pixel value of input image to the one point of color space
    • Perform k-means algorithm in color space
    • Translate each pixel value into representive color of k number


image


  • K-mean cluster code
cv2.kmeans(data, K, bestLabels, criteria, attempts, flags, centers=None) -> retval, bestLabels, centers
  • data : train data matrix
  • K : cluster number
  • bestLabels : cluster set matrix of each sample
  • criteria : finish standard
  • attempts : repeating number to using another initial label
  • flags : initial cent setting method
  • centers : matrix for expressing the matrix
  • retval : compatness measure image


  • k-means algorithm example


image


Leave a comment