File size: 1,336 Bytes
7bba3ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
let kmeansModel
const width = 640;
const height = 480;
const colDict = {
  0: 'skyblue',
  1: 'coral',
  2: 'olive',
  3: 'tan',
  4: 'grey'
}

const names = [{name: 'john'}, {name: 'mary'}, {name: 'anne'}, {name: 'paul'}, {name: 'george'}];
const data = [{ x: 300, y: 4}, { x: 2000, y: 3}, { x: 450, y: 5}, { x: 1500, y: 2}, { x: 850, y: 4}];


const options = {
  k: 2,
  maxIter: 10,
  threshold: 2,
};


kmeansModel = ml5.kmeans(data, options, clustersCalculated);


function clustersCalculated() {
  console.log('Points Clustered!');
  console.log(kmeansModel);

  const dataset = kmeansModel.dataset;
  
  //d3.select('svg').remove();

  const svg = d3.select('svg');

  const xScale = d3.scaleLinear()
    .domain(d3.extent(dataset, d => d[0]))
    .range([10, width - 100]);

  
  const yScale = d3.scaleLinear()
    .domain(d3.extent(dataset, d => d[1]))
    .range([height - 50, 20]);
  

  const circle_data = svg.selectAll('circle')
    .data(dataset)
    .enter();
    
    
  const circles = circle_data.append('circle')
    .attr('cx', d => xScale(d[0]))
    .attr('cy', d => yScale(d[1]))
    .attr('r', 9)
    .attr('fill', (d, i) => colDict[dataset[i].centroid]);
    
  
  circle_data.append('text')
    .attr('dy', d => yScale(d[1]))
    .attr('dx', d => xScale(d[0]))
    .data(names)
    .text(d => d.name);
  
}