/
test.html
189 lines (159 loc) · 4.94 KB
/
test.html
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>HTML 5 Boilerplate</title>
<script src="https://cdn.plot.ly/plotly-2.25.2.min.js" charset="utf-8"></script>
<script src="https://cdn.jsdelivr.net/npm/danfojs@1.1.2/lib/bundle.min.js"></script>
</head>
<body>
<div id="correlation-heatmap" style="height: 800px; width: 1000px">
<!-- Plotly Heatmap will go here -->
</div>
</body>
<script>
/*
* Calculates Pearson correlation between
* two arrays x and y.
*/
function corr(x, y) {
let sumX = 0,
sumY = 0,
sumXY = 0,
sumX2 = 0,
sumY2 = 0;
const minLength = x.length = y.length = Math.min(x.length, y.length),
reduce = (xi, idx) => {
const yi = y[idx];
sumX += xi;
sumY += yi;
sumXY += xi * yi;
sumX2 += xi * xi;
sumY2 += yi * yi;
}
x.forEach(reduce);
return (minLength * sumXY - sumX * sumY) /
Math.sqrt((minLength * sumX2 - sumX * sumX) * (minLength * sumY2 - sumY * sumY));
}
dfd.readCSV("https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv")
.then(df => {
df.head().print()
/**
* Generate heatmap
* This needs to be in the format of
* zValues = [
* [0.00, 0.00, 0.75, 0.75, 1.00],
* [0.00, 0.00, 0.75, 1.00, 0.00],
* [0.75, 0.75, 1.00, 0.75, 0.75],
* [0.00, 1.00, 0.00, 0.75, 0.00],
* [1.00, 0.00, 0.00, 0.75, 0.00]
* ];
*/
let zValues = [];
let dfCopy = df.copy();
let columnsLength = dfCopy.shape[1];
let columnsToDrop = [];
let numericColumns = dfCopy.selectDtypes([
'int32',
'float32',
]);
// Drop columns with high cardinality (many unique values)
for (let i = 0; i < columnsLength; i++) {
let column = dfCopy.columns[i];
// Skip if a numeric column as it will have lots of unique values
// but this doesn't matter :)
if (numericColumns.$columns.includes(column)) {
continue;
}
let uniqueValuesCount = dfCopy.column(column).unique().$data.length;
if (uniqueValuesCount > 5) {
columnsToDrop.push(column);
}
}
dfCopy.drop({ columns: columnsToDrop, inplace: true });
// Create dummy columns for categoric variables
let dummies = dfCopy.getDummies(dfCopy);
// Uncomment to debug: console.log("DUMMIES", dummies);
columnsLength = dummies.$columns.length;
for (let i = 0; i < columnsLength; i++) {
let column = dummies.$columns[i];
// Uncomment to debug: console.log("COMPARING", column);
let correlations = [];
for (let j = 0; j < columnsLength; j++) {
let comparisonColumn = dummies.$columns[j];
// Uncomment to debug: console.log("TO", comparisonColumn);
let pearsonCorrelation = corr(
dummies[column].$data,
dummies[comparisonColumn].$data
).toFixed(2)
correlations.push(
pearsonCorrelation
);
}
zValues.push(correlations);
}
var xValues = dummies.$columns;
var yValues = dummies.$columns;
var colorscaleValue = [
[0, '#3D9970'],
[1, '#001f3f']
];
var data = [{
x: xValues,
y: yValues,
z: zValues,
type: 'heatmap',
colorscale: colorscaleValue,
showscale: false
}];
var layout = {
autosize: false,
width: window.innerWidth - 650,
height: 700,
annotations: [],
xaxis: {
ticks: '',
side: 'top'
},
yaxis: {
ticks: '',
ticksuffix: ' ',
autosize: false
}
};
for (var i = 0; i < yValues.length; i++) {
for (var j = 0; j < xValues.length; j++) {
var currentValue = zValues[i][j];
if (currentValue != 0.0) {
var textColor = 'white';
} else {
var textColor = 'black';
}
var result = {
xref: 'x1',
yref: 'y1',
x: xValues[j],
y: yValues[i],
text: zValues[i][j],
font: {
family: 'Arial',
size: 12,
color: 'rgb(50, 171, 96)'
},
showarrow: false,
font: {
color: textColor
}
};
layout.annotations.push(result);
}
}
console.log(data);
Plotly.newPlot('correlation-heatmap', data, layout);
}).catch(err => {
console.log(err);
})
</script>
</html>