Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

Revision af9863621ad2e9f8042207ff0463fe04d9485b6e authored by David Hofmeyr on 22 November 2017, 09:14:29 UTC, committed by cran-robot on 22 November 2017, 09:14:29 UTC
version 0.1.0
0 parent
  • Files
  • Changes
  • c3dda25
  • /
  • R
  • /
  • clustereval.R
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • revision
  • directory
  • content
revision badge
swh:1:rev:af9863621ad2e9f8042207ff0463fe04d9485b6e
directory badge
swh:1:dir:2c0297949093c3a24c6f2960ce4d49a92ec52e69
content badge
swh:1:cnt:aa260ce1396cf39681239dc00b912a4af0616e2b

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • revision
  • directory
  • content
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
clustereval.R
### function cluster_performance() computes external cluster performance measures: Purity, V-Measure, Normalised Mutual Information and Adjusted Rand Index
## arguments:
# assigned = vector of cluster assignments
# labels = true class labels (same length as assigned)
# beta = weight parameter used in calculation of V-measure. Higher values apply higher weight to homogeneity over completeness.

cluster_performance = function(assigned, labels, beta = 1){
	n <- length(labels)
	T <- table(assigned, labels)
	RS <- rowSums(T)
	CS <- colSums(T)

	## V-measure

	CK <- - sum(apply(T, 1, function(x) return(sum(x[which(x>0)]*log(x[which(x>0)]/sum(x))))))/n
	KC <- - sum(apply(T, 2, function(x) return(sum(x[which(x>0)]*log(x[which(x>0)]/sum(x))))))/n
	K <- - sum(apply(T, 1, function(x) return(sum(x)*log(sum(x)/n))))/n
	C <- - sum(apply(T, 2, function(x) return(sum(x)*log(sum(x)/n))))/n
	if(C!=0){
		h <- 1 - CK/C
	}
	else{
		h <- 0
	}
	if(K!=0){
		c <- 1 - KC/K
	}
	else{
		c <- 0
	}
	if(h==0 && c==0) v.measure <- 0
	else v.measure <- (1+beta)*h*c/(beta*h+c)

	## Purity

	purity <- sum(apply(T, 1, function(x) return(max(x))))/n

	## Adjusted Rand Index

	O <- sum(sapply(T, function(t) choose(t, 2)))
	E <- (sum(sapply(RS, function(t) choose(t, 2)))*sum(sapply(CS, function(t) choose(t, 2))))/choose(n, 2)
	M <- (sum(sapply(RS, function(t) choose(t, 2))) + sum(sapply(CS, function(t) choose(t, 2))))/2
	adj.rand <- (O-E)/(M-E)


	## Normalised Mutual Information

	prod <- RS%*%t(CS)
	Tp <- T
	Tp[which(T==0)] <- 1e-10
	IXY <- sum(T*log(Tp*n/prod))
	HX <- sum(RS*log(RS/n))
	HY <- sum(CS*log(CS/n))
	NMI <- IXY/sqrt(HX*HY)

	c(adj.rand = adj.rand, purity = purity, v.measure = v.measure, nmi = NMI)
}



### function success_ratio() computes the success ratio (Pavlidis et al. 2016) of a binary partition
## arguments:
# assigned = vector of cluster assignments (takes at most two distinct values)
# labels = true class labels (same length as assigned)



success_ratio = function(assigned, labels){
  n = length(labels)

  #### Find the sizes of the overlaps between assigned and labels
  T = table(assigned, labels)

  #### determine which of the 2 aggregated classes to assign the classes
  split = apply(T, 2, which.max)

  #### if an aggregated class is empty, exit. failure to split any classes
  if(length(unique(split))==1){
    return(0)
  }

  #### calculate success and error
  success = min(sum(T[1,split==1]), sum(T[2,split==2]))
  error = sum(apply(T, 2, min))

  success/(success+error)
}
The diff you're trying to view is too large. Only the first 1000 changed files have been loaded.
Showing with 0 additions and 0 deletions (0 / 0 diffs computed)
swh spinner

Computing file changes ...

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API