-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
88 lines (76 loc) · 2.11 KB
/
utils.js
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
var fs = require("fs");
var parse = require('csv-parse/lib/sync');
var table = require('text-table');
function loadUnlabelledWine(options) {
return loadWine("datasets/wine.data.unlabelled.csv", options);
}
function loadLabelledWine(options) {
return loadWine("datasets/wine.data.csv", options);
}
function loadWine(filename, options) {
var features = [
'Alcohol',
'Malic Acid',
'Ash',
'Alcalinity of ash',
'Magnesium',
'Total phenols',
'Flavanoids',
'Nonflavanoid phenols',
'Proanthocyanins',
'Color intensity',
'Hue',
'OD280/OD315 of diluted wines',
'Proline',
'Class'];
var raw_csv_string = fs.readFileSync(filename).toString();
var dataset = parse(raw_csv_string, {auto_parse: true});
var format = { align: [ 'c', 'c' ], hsep: ' | ' };
if (options && options.verbose) {
console.log("our dataset has", dataset.length, "rows and ", dataset[0].length, "columns")
var T = table([features], format);
console.log(T)
console.log("---------------------------")
var D = table(dataset.slice(0,5), format);
console.log(D)
}
return {
features,
dataset
}
}
function plotClustersWithLabels(x, y, labels, title) {
var trace = {
x,
y,
mode: 'markers',
marker: {
color: labels, // <- here are our results
size: 8,
colorbar: {
xpad: 100
}
},
type: 'scatter'
};
var defaults = { width: 800, height: 700, xaxis: { title: features[0] }, yaxis: { title: features[11] }, title};
return Plot.createPlot([trace], defaults).render()
}
function grid2(range=[-1,1], step=0.01) {
var X = [];
// build a 2d array of coordinates
for (var i = range[0]; i <= range[1]; i += step) {
var x = [];
for (var j = range[0]; j <= range[1]; j += step) {
x.push([i, j])
}
X.push(x)
}
return X;
}
module.exports = {
loadUnlabelledWine,
loadLabelledWine,
grid2,
plotClustersWithLabels
};