Giter Site home page Giter Site logo

kmeans's Introduction

K-Means Clustering Algortihm

Build Status

General description

A R implementation of k-means clustering algorithm.

Input

A matrix of paired points and number of cluster

Output

The clusters of points and centers for each cluster

Usage

Image Compression:

alt text

  • kmeans with different cluster number
a2 = KM(Data,2)
a4 = KM(Data,4)
a6 = KM(Data,6)
  • Show compressed images
plotgrid = function(a){
  data = a[[2]]
  newmat = matrix(data$newdata,nrow = nrow(Data),ncol= ncol(Data))
  ## transfer to raster file for plot
  rasterGrob(newmat)
}

k1 = rasterGrob(Data)
k2 = plotgrid(a2)
k4 = plotgrid(a4)
k6 = plotgrid(a6)

grid.arrange(k1,k2,k4,k6, ncol=2,nrow = 2)

imagE = function(a){
  data = a[[2]]
  newmat = matrix(data$newdata,nrow = nrow(Data),ncol= ncol(Data))
  #transfer in to matrix, the original form 
  newmat = apply(newmat,2,rev)
  return(newmat)
}

i2 = imagE(a2)
i4 = imagE(a4)
i6 = imagE(a6)


image(t(Data),col = grey(seq(0,1,length = 256)),axes = F,main = "Original")
image(t(i2), col = grey(seq(0,1,length = 256)),axes = F,main = "K = 2")
image(t(i4), col = grey(seq(0,1,length = 256)),axes = F,main = "K = 4")
image(t(i6), col = grey(seq(0,1,length = 256)),axes = F,main = "K = 6")

alt text

kmeans's People

Contributors

tunchiehhsu avatar htj801116 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.