File:Iterative Clustering.png

From AWF-Wiki
(Difference between revisions)
Jump to: navigation, search
(Lburgr uploaded a new version of "File:Iterative Clustering.png": Added main title.)
 
(2 intermediate revisions by one user not shown)
Line 2: Line 2:
 
of iterative (migrating means) clustering.
 
of iterative (migrating means) clustering.
 
Code:
 
Code:
 
+
 
  ### Let us simulate iterative clustering!
 
  ### Let us simulate iterative clustering!
 
  ## Note that the result can better be
 
  ## Note that the result can better be
Line 27: Line 27:
 
    max(data$band.5)))
 
    max(data$band.5)))
 
   
 
   
  ## function for calculating distances to random means
+
  ## function for calculating distances to means
 
  cldist <- function(p, m){
 
  cldist <- function(p, m){
p.l <- dim(p)[1] # length of frame with points
 
p.w <- dim(p)[2] # width of frame with points
 
 
   
 
   
  # attach random means to point frame
+
  # attach means to point data frame
 
  dists.all <- as.matrix(dist(rbind(p, m)))
 
  dists.all <- as.matrix(dist(rbind(p, m)))
 
   
 
   
  # obtain distances of points to random means
+
  # obtain distances of points to means
 
  l <- dim(dists.all)[1] # get width/length of matrix
 
  l <- dim(dists.all)[1] # get width/length of matrix
 
   
 
   
Line 74: Line 72:
 
   
 
   
 
  op <- par(mfrow=c(1,3))
 
  op <- par(mfrow=c(1,3))
  plot(data&#36;band.4, data&#36;band.5, type='n', xlab='Band 4', ylab='Band 5',
+
  plot(data&#36;band.4, data&#36;band.5, type='n', xlab='Band 4', ylab='Band 5')
      main='Step 1')
+
title('Step 1', line=1)
 
  text(data&#36;band.4, data&#36;band.5, labels=as.character(data&#36;class))
 
  text(data&#36;band.4, data&#36;band.5, labels=as.character(data&#36;class))
 
  text(clmeans&#36;band.4, clmeans&#36;band.5, labels=c('1', '2'), col='red',
 
  text(clmeans&#36;band.4, clmeans&#36;band.5, labels=c('1', '2'), col='red',
 
       cex=2.5)
 
       cex=2.5)
 
   
 
   
  plot(data.2&#36;band.4, data.2&#36;band.5, type='n', xlab='Band 4', ylab='Band 5',
+
  plot(data.2&#36;band.4, data.2&#36;band.5, type='n', xlab='Band 4', ylab='Band 5')
      main='Step 2')
+
 
  text(data.2&#36;band.4, data.2&#36;band.5, labels=as.character(data.2&#36;class))
 
  text(data.2&#36;band.4, data.2&#36;band.5, labels=as.character(data.2&#36;class))
 
  text(clmeans.2&#36;band.4, clmeans.2&#36;band.5, labels=c('1', '2'), col='red',
 
  text(clmeans.2&#36;band.4, clmeans.2&#36;band.5, labels=c('1', '2'), col='red',
 
       cex=2.5)
 
       cex=2.5)
 +
mtext("Example for iterative clustering", line=2)
 +
title('Step 2', line=1)
 
   
 
   
  plot(data.3&#36;band.4, data.3&#36;band.5, type='n', xlab='Band 4', ylab='Band 5',
+
  plot(data.3&#36;band.4, data.3&#36;band.5, type='n', xlab='Band 4', ylab='Band 5')
      main='Step 3')
+
 
  text(data.3&#36;band.4, data.3&#36;band.5, labels=as.character(data.3&#36;class))  
 
  text(data.3&#36;band.4, data.3&#36;band.5, labels=as.character(data.3&#36;class))  
 
  text(clmeans.3&#36;band.4, clmeans.3&#36;band.5, labels=c('1', '2'),  
 
  text(clmeans.3&#36;band.4, clmeans.3&#36;band.5, labels=c('1', '2'),  
 
       col='red', cex=2.5)
 
       col='red', cex=2.5)
 +
title('Step 3', line=1)
 
   
 
   
mtext("Example for iterative clustering", outer=TRUE)
 
 
  options(op)
 
  options(op)
 
  dev.off()
 
  dev.off()

Latest revision as of 12:10, 3 August 2013

An example R plot to show the basic principle of iterative (migrating means) clustering. Code:

### Let us simulate iterative clustering!
## Note that the result can better be
# achieved by simply using the kmeans function:
# 	clust <- kmeans(data, 2)
# The following code only serves to create a
# single-step example.

## Some fictional satellite brightness values:
set.seed(23) # just change the seed to see different outcomes!

data <- data.frame(band.4=c(rnorm(500, mean=100, sd=20),
			    rnorm(500, mean=180, sd=10)),
		   band.5=c(rnorm(500, mean=180, sd=20),
			    rnorm(500, mean=100, sd=10)))


## make a fictional cluster
# first we need two random means (for two classes)

clmeans <- data.frame(band.4=runif(2, min(data$band.4), 
					  max(data$band.4)),
		      band.5=runif(2, min(data$band.5),
				   max(data$band.5)))

## function for calculating distances to means
cldist <- function(p, m){

	# attach means to point data frame
	dists.all <- as.matrix(dist(rbind(p, m)))

	# obtain distances of points to means
	l <- dim(dists.all)[1] # get width/length of matrix

	dists.m <- dists.all[(l-1):l, 1:(l-2)]


	return(data.frame(dists.1=dists.m[1,],
			  dists.2=dists.m[2,]))
			 
}

# now apply function to data
data <- cbind(data, cldist(data, clmeans))

## assign points to class, depending on minimum distance
data$class <- ifelse(data$dists.1 < data$dists.2, 1, 2)

## same for means of new classes
clmeans.2 <- aggregate(list(band.4=data$band.4, band.5=data$band.5), 
	  by=list(class=data$class), mean)[,2:3]

data.2 <- cbind(data[,1:2], cldist(data[,1:2], clmeans.2))

data.2$class <- ifelse(data.2$dists.1 < data.2$dists.2, 1, 2)

## one more step
clmeans.3 <- aggregate(list(band.4=data.2$band.4, band.5=data.2$band.5), 
	  by=list(class=data.2$class), mean)[,2:3]

data.3 <- cbind(data.2[,1:2], cldist(data.2[,1:2], clmeans.3))

data.3$class <- ifelse(data.3$dists.1 < data.3$dists.2, 1, 2)


## plot
png('Iterative_Clustering.png', width=2400, height=800,
    pointsize=30)

op <- par(mfrow=c(1,3))
plot(data$band.4, data$band.5, type='n', xlab='Band 4', ylab='Band 5')
title('Step 1', line=1)
text(data$band.4, data$band.5, labels=as.character(data$class))
text(clmeans$band.4, clmeans$band.5, labels=c('1', '2'), col='red',
     cex=2.5)

plot(data.2$band.4, data.2$band.5, type='n', xlab='Band 4', ylab='Band 5')
text(data.2$band.4, data.2$band.5, labels=as.character(data.2$class))
text(clmeans.2$band.4, clmeans.2$band.5, labels=c('1', '2'), col='red',
     cex=2.5)
mtext("Example for iterative clustering", line=2)
title('Step 2', line=1)

plot(data.3$band.4, data.3$band.5, type='n', xlab='Band 4', ylab='Band 5')
text(data.3$band.4, data.3$band.5, labels=as.character(data.3$class)) 
text(clmeans.3$band.4, clmeans.3$band.5, labels=c('1', '2'), 
     col='red', cex=2.5)
title('Step 3', line=1)

options(op)
dev.off()

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current12:05, 3 August 2013Thumbnail for version as of 12:05, 3 August 20132,400 × 800 (113 KB)Lburgr (Talk | contribs)Added main title.
11:32, 3 August 2013Thumbnail for version as of 11:32, 3 August 20132,400 × 800 (108 KB)Lburgr (Talk | contribs)An example R plot to show the basic principle of iterative (migrating means) clustering. Code: ### Let us simulate iterative clustering! ## Note that the result can better be # achieved by simply using the kmeans function: # clust <- kmeans(data,...

The following page links to this file:

Personal tools
Namespaces

Variants
Actions
Navigation
Development
Toolbox