Skip to content

Commit c1495e0

Browse files
author
tf-model-analysis-team
committed
Format numbers in the confusion matrix to save horizontal space for large numbers.
PiperOrigin-RevId: 276382008
1 parent 59aa274 commit c1495e0

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

tensorflow_model_analysis/frontend/tfma-multi-class-confusion-matrix-at-thresholds/tfma-multi-class-confusion-matrix-at-thresholds.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -664,12 +664,22 @@ export class MultiClassConfusionMatrixAtThresholds extends PolymerElement {
664664
}
665665
return '';
666666
};
667+
const formatNumber = (value) => {
668+
if (value < 1000) {
669+
return value;
670+
} else {
671+
const base = [1, 1e3, 1e6, 1e9, 1e12, 1e15, 1e18];
672+
const unit = ['', 'k', 'M', 'G', 'T', 'P', 'E'];
673+
const index = Math.floor(Math.log10(value) / 3);
674+
return (value / base[index]).toFixed(2) + unit[index];
675+
}
676+
};
667677
const formatValue = (value, baseline, showPercentage) => {
668678
baseline = baseline || 1;
669679
if (value && showPercentage) {
670680
return (value / baseline * 100).toFixed(1) + '%';
671681
} else {
672-
return value;
682+
return formatNumber(value);
673683
}
674684
};
675685

@@ -682,9 +692,10 @@ export class MultiClassConfusionMatrixAtThresholds extends PolymerElement {
682692
const row = [
683693
this.makeHeaderCell_(rowId),
684694
this.makeDataCell_(
685-
sourceRow.totalPositives, sourceRow.totalFalsePositives,
686-
sourceRow.totalFalseNegatives, 'guide header', 'guide header',
687-
'guide header')
695+
formatNumber(sourceRow.totalPositives),
696+
formatNumber(sourceRow.totalFalsePositives),
697+
formatNumber(sourceRow.totalFalseNegatives), 'guide header',
698+
'guide header', 'guide header')
688699
];
689700
const baseline = this.getSortValue_(summaryMatrix, rowId, sort);
690701

@@ -720,7 +731,8 @@ export class MultiClassConfusionMatrixAtThresholds extends PolymerElement {
720731

721732
if (showSortColumn) {
722733
row.push(this.makeHeaderCell_(
723-
this.getSortValue_(summaryMatrix, rowId, sort), 'guide'));
734+
formatNumber(this.getSortValue_(summaryMatrix, rowId, sort)),
735+
'guide'));
724736
}
725737
matrix.push(row);
726738
});

0 commit comments

Comments
 (0)