static/js/ari.js
/**
 * Compute the adjusted rand index given true labels and predicted labels.
 * @function adjustedRandScore
 * @param {number[]} labelTrue - the array of true labels
 * @param {number[]} labelPred - the array of predicted labels
 * @return {number} adjusted rand score
 */
Compute the adjusted rand index given true labels and predicted labels.
@function adjustedRandScore
@param {number[]} labelTrue - the array of true labels
@param {number[]} labelPred - the array of predicted labels
@return {number} adjusted rand score
Compute the adjusted rand index given true labels and predicted labels . @function adjustedRandScore @param { number [] } labelTrue - the array of true labels @param { number [] } labelPred - the array of predicted labels @return { number } adjusted rand score
function adjustedRandScore(labelTrue, labelPred) {
  let nSamples = labelTrue.length;
  let nClasses = unique(labelTrue).length;
  let nClusters = unique(labelPred).length;
  if (nClasses == nClusters === 1 || nClasses == nClusters === 0 || nClasses === nClusters === nSamples) {
    return 1.0;
  }
  let contingency = contingencyMatrix(labelTrue, labelPred);
  let classSum = Array(nClasses).fill(0);
  let clusterSum = Array(nClusters).fill(0);
  for (let i = 0; i < contingency[0].length; i++) {
    classSum[contingency[0][i][0]] += contingency[1][i];
    clusterSum[contingency[0][i][1]] += contingency[1][i];
  }
  let combK = classSum.map(x => comb(x, 2));
  let combC = clusterSum.map(x => comb(x, 2));
  let combIJ = contingency[1].map(x => comb(x, 2));
  let sumCombK = arrSum(combK);
  let sumCombC = arrSum(combC);
  let sumComb = arrSum(combIJ);
  let prodComb = sumCombK * sumCombC / comb(nSamples, 2);
  let meanComb = (sumCombK + sumCombC) / 2;
  return (sumComb - prodComb) / (meanComb - prodComb);
}
function adjustedRandScore ( labelTrue , labelPred ) { let nSamples = labelTrue . length ; let nClasses = unique ( labelTrue ) . length ; let nClusters = unique ( labelPred ) . length ; if ( nClasses == nClusters === 1 || nClasses == nClusters === 0 || nClasses === nClusters === nSamples ) { return 1.0 ; } let contingency = contingencyMatrix ( labelTrue , labelPred ) ; let classSum = Array ( nClasses ) . fill ( 0 ) ; let clusterSum = Array ( nClusters ) . fill ( 0 ) ; for ( let i = 0 ; i < contingency [ 0 ] . length ; i ++ ) { classSum [ contingency [ 0 ] [ i ] [ 0 ] ] += contingency [ 1 ] [ i ] ; clusterSum [ contingency [ 0 ] [ i ] [ 1 ] ] += contingency [ 1 ] [ i ] ; } let combK = classSum . map ( x => comb ( x , 2 ) ) ; let combC = clusterSum . map ( x => comb ( x , 2 ) ) ; let combIJ = contingency [ 1 ] . map ( x => comb ( x , 2 ) ) ; let sumCombK = arrSum ( combK ) ; let sumCombC = arrSum ( combC ) ; let sumComb = arrSum ( combIJ ) ; let prodComb = sumCombK * sumCombC / comb ( nSamples , 2 ) ; let meanComb = ( sumCombK + sumCombC ) / 2 ; return ( sumComb - prodComb ) / ( meanComb - prodComb ) ; }
Compute the adjusted rand index given true labels and predicted labels.
Compute the adjusted rand index given true labels and predicted labels .