-
Notifications
You must be signed in to change notification settings - Fork 11
/
wine.js
80 lines (64 loc) · 2.04 KB
/
wine.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
var csvParse = require('csv-parse'),
fs = require('fs'),
KMeans = require('../index').KMeans,
_ = require('underscore');
var apiKey = process.env.PLOTLY_API_KEY;
var username = process.env.PLOTLY_USERNAME;
var plotly = require('plotly')(username,apiKey);
fs.readFile('./examples/wine.data', 'utf8', function(err, dataStr) {
if (err) {
console.log(err);
process.exit(1);
}
csvParse(dataStr, {delimiter: ',', auto_parse: true}, function(err, data) {
var xAndY = data.map(function(h) { return [h[1], h[9]]; });
var x = data.map(function(h) { return h[1]; }); // x is Malic Acid
var y = data.map(function(h) { return h[9]; }); // y is color intensity
var kmeans = new KMeans(3);
kmeans.cluster(xAndY, function(err, clusters, centroids) {
// plot x-y scatter plot
var layout = {
title: 'Wine',
xaxis: {
title: 'Malic Acid'
},
yaxis: {
title: 'Magnesium'
}
};
var centroidTrace = {
x: centroids.map(function(c) { return c[0]; }),
y: centroids.map(function(c) { return c[1]; }),
mode: 'markers',
type: 'scatter',
name: 'Centroids',
marker: {
color: '#000000',
symbol: 'cross',
size: 10
}
}
var plotData = [centroidTrace];
clusters.forEach(function(cluster, index) {
var trace = {
x: cluster.map(function(c) { return c[0]; }),
y: cluster.map(function(c) { return c[1]; }),
mode: 'markers',
type: 'scatter',
name: 'Cluster ' + index
}
plotData.push(trace);
});
var graphOptions = {layout: layout, filename: 'wine-categories', fileopt: 'overwrite'};
plotly.plot(plotData, graphOptions, function (err, msg) {
if (err) {
console.log(err);
process.exit(3);
} else {
console.log('Success! The plot (' + msg.filename + ') can be found at ' + msg.url);
process.exit();
}
});
});
});
});