This repository has been archived by the owner on Jun 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
confusion_matrix.jsx
83 lines (73 loc) · 2.12 KB
/
confusion_matrix.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import React, { Component } from 'react';
import Chart from 'chart.js';
class ColorBuilder {
// TODO: better and accessible pallete.
static buildCorrect( normalized ) {
let val = 255 - Math.floor( normalized * 255.0 );
return 'rgb(' + val + ', ' + val + ', ' + val + ')';
}
static buildIncorrect( normalized ) {
let val = 255 - Math.floor( normalized * 255.0 );
return 'rgb(' + val + ', ' + val + ', ' + val + ')';
}
}
class ConfusionMatrix extends Component {
componentDidMount() {
let chartCanvas = this.refs.chart,
cells =
Object.keys( this.props.counts.predictions ).map( actual => {
let targetCount = this.props.counts.labels[ actual ],
cellColors = Object.keys( this.props.counts.predictions[ actual ] ).map( predicted => {
let cellCount = this.props.counts.predictions[ actual ][ predicted ],
cellNormalized = cellCount / targetCount,
isCorrectDiagonal = actual === predicted,
cellColor = isCorrectDiagonal ?
ColorBuilder.buildCorrect( cellNormalized ) :
ColorBuilder.buildIncorrect( cellNormalized );
// label: actual + ' -> ~' + predicted,
return cellColor;
} );
return {
label: actual,
data: Object.keys( this.props.counts.predictions[ actual ] ).map( _ => { return 1; } ),
backgroundColor: cellColors,
stack: 'confusion'
};
} ),
data = {
labels: Object.keys( this.props.counts.labels ).map( label => { return '~' + label; } ),
datasets: cells
};
/* eslint-disable no-new */
new Chart( chartCanvas, {
type: 'horizontalBar',
data: data,
options: {
title: {
display: true,
text: 'Confusion matrix for ' + this.props.wiki + ' ' + this.props.model + ' model'
},
legend: {
display: false
},
scales: {
xAxes: [ {
// TODO: until we can display categories on the x-axis
display: false
} ],
yAxes: [ {
stacked: true,
barPercentage: 1.0,
categoryPercentage: 1.0
} ]
}
}
} );
}
render() {
return (
<canvas ref={ 'chart' } height="400" width="400"></canvas>
);
}
}
export default ConfusionMatrix;