Skip to content

Instantly share code, notes, and snippets.

@Jverma
Created July 19, 2015 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jverma/38dcb98a058539d71672 to your computer and use it in GitHub Desktop.
Save Jverma/38dcb98a058539d71672 to your computer and use it in GitHub Desktop.
K-means clustering of wine data

I wrote a blog post for packtpub describing the K-means clustering procedure. The codes I provided were written in R, I also pointed to another blog post of mine where I use python to explain this algotithm. I decided to write a D3 visualization of the clusters.

Index Alcohol Malic Ash Alacalinity Magnesium Phenols Flavanoids Nonflavanoids Proanthocyanins Color Hue Dilution Proline cluster
1 1.514340767 -0.560668221 0.23139979 -1.166303174 1.908521507 0.806721729 1.031908069 -0.657707799 1.221438453 0.251008784 0.361158491 1.84272147 1.010159388 1
2 0.245596828 -0.498008556 -0.825667215 -2.483840525 0.01809398 0.567048088 0.731565283 -0.818410597 -0.543188717 -0.292496232 0.404908465 1.110317231 0.962526349 1
3 0.196325219 0.021171524 1.106213863 -0.267982252 0.088109814 0.806721729 1.212113741 -0.497005002 2.129959372 0.268262912 0.317408517 0.786369202 1.3912237 1
4 1.6867914 -0.345835084 0.486553895 -0.806974805 0.928299826 2.484437221 1.462399395 -0.979113395 1.029251335 1.182731669 -0.426341036 1.180740715 2.3280068 1
5 0.294868437 0.22705328 1.835225591 0.450674485 1.278378998 0.806721729 0.6614853 0.226157589 0.400275314 -0.318377423 0.361158491 0.448336476 -0.037767469 1
6 1.47738706 -0.515911317 0.304300963 -1.286079296 0.858283992 1.55769914 1.362285134 -0.175599406 0.662348656 0.729810822 0.404908465 0.335658901 2.232740722 1
7 1.711427204 -0.41744613 0.304300963 -1.465743481 -0.261969358 0.327374446 0.491291055 -0.497005002 0.679820212 0.082781041 0.273658544 1.363841775 1.724654973 1
8 1.304936428 -0.16680747 0.887510345 -0.567422559 1.488426501 0.487156874 0.481279629 -0.416653603 -0.595603386 -0.003489596 0.448658438 1.363841775 1.740532653 1
9 2.253414907 -0.623327885 -0.716315456 -1.645407665 -0.191953523 0.806721729 0.95181666 -0.577356401 0.679820212 0.061213382 0.536158386 0.335658901 0.94664867 1
10 1.058578381 -0.882917925 -0.351809592 -1.046527051 -0.121937689 1.094330099 1.122010905 -1.139816193 0.452689983 0.93254682 0.22990857 1.321587684 0.94664867 1
11 1.354208037 -0.15785609 -0.242457833 -0.447646437 0.368173151 1.046395371 1.29220515 -1.139816193 1.378682458 0.298457635 1.279907939 0.786369202 2.423272878 1
12 1.378843842 -0.766549976 -0.16955666 -0.806974805 -0.331985192 -0.151972837 0.401188219 -0.818410597 -0.036513589 -0.025057256 0.929908149 0.29340481 1.692899614 1
13 0.923081456 -0.542765459 0.158498617 -1.046527051 -0.752080198 0.487156874 0.731565283 -0.577356401 0.382803758 0.233754657 0.842408202 0.406082385 1.819921051 1
14 2.154871688 -0.542765459 0.085597444 -2.423952463 -0.61204853 1.286069013 1.662627919 0.547563185 2.129959372 0.147484019 1.279907939 0.166642538 1.280079943 1
15 1.699109302 -0.41744613 0.049146858 -2.244288279 0.158125648 1.605633868 1.612570788 -0.577356401 2.392032714 1.053325713 1.061158071 0.546929354 2.540767708 1
16 0.775266628 -0.471154414 1.215565622 -0.687198682 0.858283992 0.886612943 0.881736676 -0.497005002 -0.228700707 0.967055075 1.41115786 0.377912991 1.788165692 1
17 1.600566084 -0.372689226 1.288466795 0.151234178 1.418410667 0.806721729 1.111999479 -0.255950805 0.662348656 0.492566569 0.492408412 0.053964962 1.692899614 1
18 1.021624674 -0.68598755 0.923960931 0.151234178 1.068331495 1.046395371 1.37229656 0.306508988 0.225559753 0.665107844 0.754908255 -0.058712613 1.216569224 1
19 1.465069158 -0.668084789 0.413652722 -0.896806897 0.578220655 1.605633868 1.902902148 -0.336302204 0.470161539 1.570949537 1.192407992 0.29340481 2.963113987 1
20 0.78758453 0.683573695 0.705257413 -1.286079296 1.138347329 0.646939302 1.001873791 -1.541573188 0.120730416 0.018078063 0.011158701 1.053978443 0.311541483 1
21 1.304936428 -0.632279266 -0.315359006 -1.046527051 1.838505673 1.126286585 1.142033757 -0.979113395 0.889478886 0.255322316 0.57990836 1.546942835 0.105131647 1
22 -0.086986535 1.310170343 1.03331269 -0.267982252 0.158125648 0.183570261 0.381165367 -0.898761996 0.679820212 -0.240733849 0.317408517 1.279333594 0.073376288 1
23 0.873809846 -0.42639751 -0.023754315 -0.866862867 0.088109814 0.503135117 0.851702398 -0.738059198 0.173145084 -0.542681081 0.667408307 1.955399046 0.91489331 1
24 -0.185529754 -0.659133408 0.559455067 -0.507534498 -0.331985192 0.295417961 0.341119662 -0.818410597 -0.228700707 -0.486605166 0.57990836 1.43426526 0.851382592 1
25 0.615133898 -0.471154414 0.887510345 0.151234178 -0.261969358 0.375309174 0.581393891 -0.657707799 0.120730416 -0.663459973 0.711158281 1.701874501 0.311541483 1
26 0.060828293 -0.256321277 3.110996114 1.648435713 1.698474004 0.535091602 0.651473874 0.868968781 0.574990876 -0.637578782 0.754908255 0.828623293 0.263908444 1
27 0.479636972 -0.506959936 0.923960931 -1.01658302 -0.472016861 0.886612943 0.911770955 -0.175599406 -0.246172263 -0.111327893 -0.163841194 0.856792686 1.422979059 1
28 0.368775851 -0.55171684 -0.825667215 -0.747086744 -0.402001026 0.167592018 0.160913991 -0.738059198 -0.420887824 -0.477978102 0.273658544 0.222981325 1.708777293 1
29 1.070896284 -0.390591988 1.580071486 -0.028430007 0.50820482 1.046395371 0.941805234 0.065454791 0.295445977 -0.240733849 1.279907939 1.110317231 0.533828998 1
30 1.255664818 -0.587522363 -0.57051311 -1.046527051 -0.261969358 0.567048088 0.301073957 -0.818410597 0.679820212 -0.154463212 0.361158491 1.377926472 0.91489331 1
31 0.898445651 -0.748647215 1.215565622 0.899834945 0.088109814 1.126286585 1.222125167 -0.577356401 1.378682458 0.276889975 1.017408097 0.138473144 1.708777293 1
32 0.713677116 -0.605425124 -0.023754315 -0.118262099 0.438188986 0.902591186 1.16205661 -1.139816193 0.627405544 0.7945138 0.57990836 0.377912991 2.439150558 1
33 0.836856139 -0.453251652 -0.023754315 -0.687198682 0.298157317 0.199548504 0.6614853 0.467211786 0.662348656 -0.525426953 1.192407992 0.363828294 0.771994193 1
34 0.935399358 -0.721793073 1.215565622 0.001514024 2.258600679 1.046395371 0.711542431 1.110022978 -0.420887824 0.147484019 1.279907939 0.546929354 1.550000497 1
35 0.6274518 -0.480105794 1.03331269 -0.14820613 0.718252323 0.087700804 0.501302481 -0.577356401 -0.088928258 -0.370139806 0.623658333 0.363828294 1.105425466 1
36 0.590498093 -0.471154414 0.158498617 0.300954331 0.01809398 0.646939302 0.95181666 -0.818410597 0.470161539 0.018078063 0.361158491 1.208910109 0.549706678 1
37 0.344140046 -0.623327885 1.725873832 -1.196247204 0.718252323 0.487156874 0.651473874 -0.175599406 -0.403416268 -0.197598531 0.57990836 0.237066022 0.422685241 1
38 0.060828293 -0.614376505 0.668806826 -0.447646437 -0.121937689 0.247483232 0.401188219 -0.577356401 -0.263643819 -0.348572146 0.711158281 -0.143220794 1.137180826 1
39 0.085464098 -0.748647215 -0.971469561 -1.196247204 -0.121937689 0.167592018 0.611428169 -0.657707799 -0.385944712 -0.585816399 0.973658123 0.11030375 0.867260271 1
40 1.502022865 1.480246576 0.523004481 -1.884959911 1.978537342 1.126286585 1.011885217 -1.300518991 0.854535774 0.018078063 -0.295091115 1.293418291 0.041620929 1
41 0.689041312 -0.560668221 -0.206007247 -0.986638989 1.208363164 1.365960227 1.262170872 -0.175599406 1.308796233 0.462371846 -0.032591273 1.082147837 0.152764686 1
42 0.504272777 1.345975865 -0.898568388 -0.208094191 -0.682064364 0.247483232 0.651473874 -0.738059198 -0.193757594 -0.335631551 -0.207591167 0.546929354 0.91489331 1
43 1.083214186 -0.399543368 0.814609172 -1.345967358 0.088109814 1.525742654 1.532479379 -1.541573188 0.190616641 0.160424615 -0.338841089 1.335672381 1.105425466 1
44 0.294868437 1.471295195 -0.278908419 -0.59736659 0.228141483 0.551069845 0.601416743 -0.336302204 0.120730416 -0.301123296 -0.601340931 0.546929354 -0.212421946 1
45 0.060828293 -0.506959936 -0.971469561 -0.747086744 0.50820482 1.126286585 0.971839512 -0.657707799 0.767177993 -0.007803128 -0.338841089 1.039893746 0.43856292 1
46 1.489704963 1.525003479 0.267850376 -0.17815016 0.788268158 0.886612943 0.621439595 -0.497005002 -0.595603386 0.078467509 -0.382591062 1.011724352 1.057792427 1
47 1.699109302 1.122191348 -0.315359006 -1.046527051 0.158125648 1.525742654 1.142033757 -0.738059198 1.046722891 -0.068192574 0.361158491 1.166656018 1.010159388 1
48 1.107849991 -0.587522363 -0.898568388 -1.046527051 0.088109814 1.286069013 1.362285134 -1.220167592 0.959365111 0.44943125 -0.207591167 1.011724352 0.756116514 1
49 1.354208037 -0.283175419 0.122048031 -0.208094191 0.228141483 0.726830515 0.891748103 -0.336302204 1.378682458 0.492566569 0.492408412 0.194811931 0.994281709 1
50 1.1571216 -0.542765459 -0.351809592 -0.627310621 0.578220655 0.934547672 1.512456526 -0.336302204 0.854535774 1.657220175 0.711158281 0.687776323 1.629388895 1
51 0.060828293 -0.542765459 -1.190173079 -2.124512156 -0.542032695 0.678895787 1.242148019 -1.541573188 2.304674933 0.923919756 0.711158281 0.420167082 1.280079943 1
52 1.021624674 -0.614376505 0.851059758 -0.687198682 -0.402001026 0.247483232 0.961828086 -1.139816193 1.221438453 0.233754657 1.236157966 1.06806314 1.645266575 1
53 1.009306772 -0.524862698 0.194949204 -1.645407665 0.788268158 2.532371949 1.71268505 -0.336302204 0.487633095 0.859216778 0.22990857 0.913131474 1.40710138 1
54 0.94771726 -0.390591988 1.142664449 -0.717142713 1.068331495 1.126286585 0.761599562 0.226157589 0.155673528 0.535701888 0.754908255 0.448336476 1.994575527 1
55 0.910763553 -0.596473743 -0.424710765 -0.926750928 1.278378998 0.487156874 0.87172525 -1.220167592 0.050844191 0.341592953 -0.163841194 0.828623293 0.994281709 1
56 0.689041312 -0.542765459 0.340751549 0.300954331 1.138347329 1.062373614 0.751588136 -1.300518991 1.500983351 0.514134228 0.098658649 0.589183445 1.184813865 1
57 1.502022865 -0.569619601 -0.242457833 -0.956694959 1.278378998 1.44585144 0.971839512 -0.818410597 0.767177993 0.570210143 -0.076341246 0.983554959 0.708483475 1
58 0.356457949 -0.327932323 1.142664449 -0.806974805 0.158125648 1.126286585 1.202102314 -0.416653603 0.120730416 0.406295932 0.492408412 0.321574204 1.661144254 1
59 0.886127749 -0.81130688 0.486553895 -0.836918836 0.578220655 1.765416296 1.642605067 -1.38087039 0.784649549 0.751378481 -0.295091115 0.363828294 1.708777293 1
60 -0.776789065 -1.249924533 -3.668812953 -2.663504709 -0.822096033 -0.503494178 -1.460937052 -0.657707799 -2.045742545 -1.340684477 0.404908465 -1.115064881 -0.720507695 2
61 -0.826060674 -1.106702442 -0.315359006 -1.046527051 0.088109814 -0.391646479 -0.94034289 2.154591165 -2.063214101 -0.77129827 1.279907939 -1.326335335 -0.212421946 2
62 -0.444205702 -0.873966544 -1.263074252 -0.806974805 0.01809398 -0.439581207 -0.619977252 1.351077175 -1.696311422 0.298457635 0.098658649 -1.43901291 -0.94279521 3
63 0.824538237 -0.972431732 -1.627580115 -0.447646437 -0.402001026 -0.311755265 -0.239543057 -0.336302204 -1.504124305 -0.542681081 1.192407992 -0.213644279 -0.371198742 2
64 -0.776789065 -1.0798483 -0.752766042 -0.14820613 -0.892111867 1.925198724 1.071953774 -1.38087039 0.487633095 -0.262301509 1.148658018 0.363828294 -1.038061288 2
65 -1.023147112 -0.793404118 0.595905654 -0.14820613 0.298157317 -0.647298363 -0.279588762 0.708265983 -0.979977621 -0.90933129 2.154907414 -0.537592308 -1.244471124 2
66 -0.776789065 -1.008237255 0.705257413 -0.417702406 -0.121937689 0.199548504 0.621439595 0.065454791 0.854535774 -0.197598531 1.017408097 -0.43899943 -0.218773018 2
67 0.134735707 -1.187264868 -2.429493016 -1.345967358 -1.522254376 1.094330099 1.152045184 -0.818410597 1.203966897 0.1043487 0.711158281 0.800453899 -0.777667342 2
68 -0.776789065 -1.044042777 -1.627580115 0.031458055 -1.522254376 -0.295777022 -0.029303107 -0.738059198 -0.962506065 -0.163090276 0.711158281 1.222994806 -0.752263054 2
69 0.41804746 -1.249924533 -0.023754315 -0.747086744 0.718252323 0.375309174 -0.73010294 1.511779973 -2.045742545 -0.814433589 0.273658544 -0.960133215 0.009865569 2
70 -0.973875502 -1.026140016 -2.247240084 -0.806974805 3.588901532 -0.711211334 -0.750125793 -1.782627385 1.588341132 -0.952466609 1.41115786 0.645522233 -0.09175158 2
71 -0.875332284 -0.650182027 -0.57051311 0.2710103 0.228141483 -1.909579543 -1.010422874 0.065454791 -0.228700707 -0.866195971 -0.225091157 -1.115064881 0.390929881 2
72 1.058578381 -0.739695834 1.106213863 1.648435713 -0.962127701 1.046395371 0.831679545 -1.220167592 0.487633095 -0.723849419 1.76115765 0.772284505 -1.069816648 2
73 0.602815995 -0.605425124 -0.461161351 1.348995406 -0.892111867 -0.663276606 -0.189485926 -0.738059198 -0.979977621 -0.568562272 0.098658649 0.237066022 -0.87293342 2
74 -0.013079121 -0.596473743 0.851059758 3.145637249 2.74871152 1.605633868 0.861713824 -1.220167592 0.6448771 -0.736790015 1.542407782 1.2511642 0.756116514 1
75 -1.28182306 -1.115653823 -0.242457833 0.450674485 0.088109814 1.73345981 0.11085686 -1.862978784 0.10325886 -0.797179461 0.142408622 0.730030414 0.441738456 2
76 -1.65136013 -0.408494749 -1.627580115 -1.046527051 -0.191953523 -1.094689161 -0.459794433 -0.175599406 -0.770318947 -0.542681081 1.192407992 -0.66435458 -1.012657001 2
77 0.036192488 -1.285730056 -2.393042429 -1.046527051 -0.962127701 -0.551428907 0.000731172 -0.979113395 -0.228700707 -0.197598531 1.017408097 -0.185474885 -1.126976294 2
78 -1.429637888 0.4955947 -0.497611938 -0.447646437 0.858283992 -0.91892849 -0.710080088 0.547563185 -1.11975007 -1.038737246 0.011158701 -0.129136098 -0.784018414 2
79 -0.826060674 -1.20516763 -1.518228356 -1.405855419 2.538664016 -0.63132012 -0.1794745 -0.095248007 2.042601591 -0.715222356 0.448658438 -0.424914733 0.009865569 2
80 -0.370298288 1.372830007 0.122048031 1.049555099 0.088109814 0.854656458 0.521325333 0.547563185 0.627405544 -1.073245501 1.017408097 0.730030414 -0.901513243 2
81 -1.232551451 -1.267827295 -1.335975424 -0.14820613 -0.962127701 0.199548504 0.230993974 -0.497005002 -0.281115375 -1.103440224 1.848657598 0.715945717 -1.488987391 2
82 -0.345662484 -0.471154414 -0.606963697 -0.208094191 -0.962127701 -0.151972837 0.501302481 -0.818410597 0.312917534 -0.499545762 0.886158176 0.744115111 -0.104453724 2
83 -1.134008233 -1.0798483 0.523004481 1.348995406 -1.522254376 -0.471537693 -0.449783007 0.306508988 -0.333530044 -1.23284618 1.542407782 0.152557841 -0.371198742 2
84 0.060828293 1.363878627 -0.16955666 0.899834945 -1.032143536 -1.03077619 -0.439771581 1.993888367 0.050844191 -0.111327893 -0.513840984 -0.84745564 -0.736385375 3
85 -1.429637888 -1.294681437 0.778158586 -0.447646437 -0.402001026 -0.151972837 0.180936843 -1.139816193 1.326267789 -0.866195971 -0.732590852 0.65960693 -0.720507695 2
86 -0.407251995 -1.21411901 -0.461161351 -0.447646437 -0.051921855 -0.151972837 -0.089371664 -0.497005002 -0.228700707 -1.051677842 1.192407992 0.772284505 -0.94279521 2
87 -1.035465014 -0.650182027 -0.206007247 0.989667037 -0.682064364 -0.823059034 -0.339657319 0.547563185 -0.053985145 -1.125007884 1.629907729 -0.495338217 -0.799896093 2
88 -1.663678033 -0.596473743 0.923960931 1.94787602 -0.822096033 -0.599363635 -0.419748728 0.306508988 -0.43835938 -1.060304905 1.76115765 0.842707989 -0.587135186 2
89 -1.675995935 -0.247369897 0.340751549 0.630338669 -1.10215937 -0.551428907 -0.339657319 0.94932018 -0.420887824 -0.974034268 0.186158596 0.194811931 -0.212421946 2
90 -1.134008233 -0.900820687 -0.242457833 1.229219283 -2.082381051 -0.151972837 -0.439771581 0.467211786 -0.368473156 -1.431268647 0.492408412 0.842707989 -0.387076422 2
91 -1.134008233 -0.453251652 -0.16955666 -0.297926283 -1.312206873 -1.110667404 -0.529874417 1.270725776 0.085787304 -1.146575543 0.536158386 -0.48125352 -0.847529132 2
92 -1.232551451 -0.739695834 0.194949204 0.750114792 -0.962127701 -1.350341045 -0.780160071 1.110022978 0.068315748 -0.628951718 0.404908465 0.053964962 -0.94279521 2
93 -0.382616191 -0.721793073 -0.388260179 0.360842393 -1.382222707 -1.462188745 -0.569920121 1.75283417 0.050844191 -0.866195971 0.011158701 -0.777032156 -0.799896093 2
94 -0.875332284 0.441886416 -0.534062524 -0.447646437 -0.822096033 0.247483232 0.220982548 -0.898761996 0.697291769 -1.25441384 0.842408202 0.969470262 -1.450880959 2
95 -1.70063174 -0.310029561 -0.315359006 -0.447646437 -0.121937689 1.15824307 0.230993974 -1.541573188 -0.420887824 -0.779925334 0.886158176 0.490590567 -1.276226483 2
96 -0.653610042 -0.730744454 -0.606963697 -0.14820613 4.35907571 0.327374446 0.2410054 -0.336302204 2.95112251 -1.060304905 0.886158176 0.025795569 0.603690789 1
97 -1.466591595 -0.193661612 1.361367968 0.600394638 2.398632348 -1.110667404 -1.040457152 -1.782627385 -0.053985145 -1.103440224 -0.032591273 -0.495338217 -0.387076422 2
98 -0.875332284 -0.829209641 -1.408876597 -1.046527051 -1.032143536 0.40726566 0.471268203 -0.577356401 0.312917534 -0.930898949 1.192407992 0.180727235 -1.012657001 2
99 -0.776789065 -1.133556584 -0.971469561 -0.297926283 -0.822096033 1.957155209 1.722696476 -0.979113395 0.627405544 -0.240733849 0.361158491 0.222981325 -0.275932664 2
100 -0.875332284 0.746233359 -0.57051311 -0.447646437 -0.822096033 0.886612943 0.961828086 0.708265983 2.129959372 -1.189710862 2.023657493 0.307489507 -1.082518791 2
101 -1.134008233 -0.229467135 -2.429493016 -0.59736659 -0.191953523 -0.104038109 0.140891138 -0.818410597 -0.333530044 -0.758357674 1.367407887 0.490590567 -0.117155868 2
102 -0.493477312 -0.891869306 -1.700481288 -0.297926283 -0.822096033 -1.350341045 -0.670034383 -0.577356401 -0.420887824 -1.125007884 0.361158491 0.222981325 -0.587135186 2
103 -0.813742772 0.10173395 0.340751549 0.450674485 -0.121937689 0.423243903 0.080822581 -0.175599406 -0.490774049 -0.974034268 -0.688840878 1.082147837 -0.980901641 2
104 -1.454273693 -0.55171684 -1.773382461 0.001514024 -0.962127701 0.327374446 -0.38971445 0.065454791 -0.298586931 -1.293235627 -0.076341246 -0.241813673 -1.053938968 2
105 -0.604338433 -0.542765459 -1.408876597 0.300954331 -1.032143536 -0.151972837 -0.109394517 -0.336302204 -0.193757594 -0.913644822 0.361158491 1.349757078 -0.237826233 2
106 -0.715199554 0.191247757 -0.351809592 0.750114792 -0.682064364 -0.982841462 -0.189485926 2.395645362 -0.298586931 -1.017169587 -0.426341036 0.969470262 -1.371492561 2
107 -0.924603893 -0.542765459 -0.898568388 -0.14820613 -1.382222707 -1.03077619 0.000731172 0.065454791 0.068315748 -0.715222356 0.186158596 0.786369202 -0.752263054 2
108 -0.345662484 -0.524862698 -0.315359006 0.899834945 -1.10215937 -1.462188745 -0.269577336 0.94932018 0.068315748 -0.758357674 -0.338841089 -0.269983067 -0.822124845 2
109 -0.9615576 -0.936626209 -1.554678943 -0.14820613 -0.542032695 0.103679047 0.010742598 0.226157589 0.854535774 -1.017169587 -0.426341036 0.575098748 -1.381019169 2
110 -1.712949642 -0.882917925 1.215565622 0.151234178 -0.402001026 0.710852273 0.891748103 -0.577356401 1.570869575 -1.038737246 0.011158701 0.913131474 -0.212421946 2
111 -1.897718177 1.256462058 -1.992085979 0.001514024 0.50820482 1.413894955 0.551359612 -0.979113395 3.475269194 -0.930898949 -0.907590747 0.279320113 -0.587135186 2
112 -0.59202053 0.083831189 -0.716315456 0.450674485 -0.822096033 0.40726566 0.2410054 -0.818410597 -0.648018054 -1.319116818 -0.251341141 0.237066022 -1.339737202 2
113 -1.528181107 0.307615706 2.017478523 0.151234178 0.228141483 -0.870993762 0.000731172 1.913536968 -0.945034508 -0.542681081 1.192407992 -0.157305491 -0.444236069 2
114 -1.959307688 -1.428952147 0.486553895 0.450674485 -0.822096033 0.295417961 -0.019291681 0.467211786 -0.263643819 -0.853255375 0.623658333 -0.424914733 -0.993603785 2
115 -1.134008233 -0.847112402 0.486553895 0.899834945 -1.10215937 0.423243903 0.261028253 0.547563185 -0.962506065 -0.930898949 -0.12009122 0.814538596 -1.149205046 2
116 -2.427387977 -0.739695834 -0.606963697 0.600394638 -1.032143536 0.263461475 0.140891138 1.270725776 0.732234881 -1.362252137 3.292406731 0.363828294 -1.079343255 2
117 -1.454273693 -0.775501357 -1.372426011 0.390786423 -0.962127701 -0.503494178 -0.429760155 -0.497005002 -0.106399814 -1.340684477 -0.032591273 1.011724352 -0.799896093 2
118 -0.715199554 -0.650182027 -0.643414283 0.899834945 0.578220655 -0.471537693 0.060799729 -0.175599406 0.033372635 -1.293235627 0.448658438 0.490590567 -1.276226483 2
119 -0.284072972 0.978969257 -1.408876597 -1.046527051 -1.382222707 -1.062732675 -0.780160071 0.547563185 -1.329408743 -0.715222356 -1.126340616 -0.692523974 -1.190487013 3
120 -1.232551451 0.978969257 -1.335975424 -0.14820613 -0.892111867 -0.471537693 -0.38971445 0.065454791 0.487633095 -1.629691113 -0.12009122 0.617352839 -0.580784114 2
121 -1.910036079 0.056977047 0.194949204 0.151234178 -0.261969358 0.966504157 0.761599562 -0.336302204 0.41774687 -0.779925334 -0.688840878 1.096232534 -0.387076422 2
122 -1.774539153 -0.256321277 3.1474467 2.696476788 1.348394832 1.413894955 3.05421616 0.868968781 0.487633095 0.406295932 -0.12009122 1.518773441 -0.895162171 1
123 -0.715199554 1.874107326 1.324917381 2.097596174 0.158125648 -0.151972837 0.100845433 0.547563185 0.208088197 -1.284608563 -0.163841194 0.715945717 -1.212715765 2
124 0.060828293 3.100446479 -0.862117801 0.600394638 -0.962127701 0.519113359 0.621439595 -0.497005002 0.732234881 -1.060304905 -0.995090695 0.687776323 -1.165082726 2
125 -1.392684181 1.766690757 0.085597444 0.450674485 -1.242191039 0.902591186 1.001873791 -1.220167592 2.304674933 -0.974034268 -0.907590747 1.448349957 -1.165082726 2
126 -1.146326135 -0.15785609 -0.716315456 0.450674485 -1.032143536 0.487156874 0.621439595 0.065454791 -0.420887824 -0.991288395 -0.426341036 0.941300868 -1.171433797 2
127 -0.702881651 -0.721793073 -0.278908419 0.600394638 -0.962127701 0.710852273 1.122010905 0.226157589 0.312917534 -0.482291634 -1.170090589 0.321574204 -1.253997732 2
128 -1.4912274 -0.184710232 1.507170313 2.696476788 -0.542032695 -0.263820537 0.210971122 1.75283417 0.295445977 -0.88776363 0.054908675 -0.241813673 -0.891986635 2
129 -0.776789065 -0.632279266 -0.242457833 1.498715559 -0.822096033 -0.120016352 0.421211072 0.306508988 0.540047763 -1.267354435 -0.295091115 0.237066022 -1.285753091 2
130 -1.183279842 1.757739377 0.049146858 0.750114792 -1.382222707 -0.311755265 -0.279588762 0.467211786 -0.420887824 -1.060304905 -0.732590852 -0.058712613 -0.529975539 2
131 -0.173211851 -0.882917925 -0.16955666 -0.447646437 1.558442335 -1.254471589 -0.780160071 -1.220167592 -1.137221626 -0.413275124 -0.863840773 -1.861553818 -0.371198742 3
132 -0.148576047 0.585108507 0.122048031 0.151234178 0.298157317 -1.590014687 -0.81019435 -0.979113395 -1.329408743 0.147484019 -0.951340721 -1.678452758 -0.688752336 3
133 -0.234801363 -0.023585379 0.122048031 1.348995406 -0.121937689 -1.829688329 -0.94034289 -0.738059198 -1.329408743 0.276889975 -1.301340511 -1.762960939 -0.593486258 3
134 -0.370298288 1.086385825 -0.023754315 0.600394638 0.438188986 -0.950884976 -0.830217202 -1.541573188 -1.311937187 -0.025057256 -0.776340826 -1.861553818 -0.46646482 3
135 -0.604338433 -0.981383113 -0.424710765 -0.59736659 -1.032143536 -0.471537693 -1.450925626 1.913536968 -0.595603386 0.169051679 -0.907590747 -1.551690486 -0.307688024 3
136 -0.493477312 0.110685331 -0.606963697 -0.297926283 -0.402001026 -1.078710918 -1.370834217 2.154591165 -1.137221626 0.880784438 -0.995090695 -1.453097607 -0.164788907 3
137 -0.924603893 2.133697366 0.63235624 0.450674485 -0.752080198 -1.462188745 -1.561051314 1.351077175 -1.381823412 -0.521113421 -0.907590747 -1.889723212 -0.085400508 3
138 -0.579702628 2.84085644 0.996862104 1.648435713 -0.261969358 -0.807080791 -1.430902774 2.154591165 -0.857676728 -0.025057256 -0.601340931 -1.298165941 -0.736385375 3
139 0.602815995 1.122191348 -0.643414283 0.001514024 -0.822096033 -1.078710918 -1.551039888 1.75283417 -1.242050963 0.276889975 -0.645090905 -1.115064881 -0.529975539 3
140 -0.197847656 0.558254365 0.887510345 1.348995406 0.088109814 0.039766076 -1.430902774 1.351077175 -1.364351856 -0.059565511 -0.295091115 -0.650269883 -0.49822018 3
141 -0.086986535 0.423983655 1.215565622 0.450674485 -0.261969358 -1.20653686 -1.531017036 1.351077175 -1.469181193 -0.197598531 -0.8200908 -0.424914733 -0.46646482 3
142 0.442683265 0.200199138 -0.060204901 0.151234178 -0.752080198 -1.430232259 -1.531017036 0.065454791 -1.66136831 0.233754657 -1.126340616 -0.199559582 0.105131647 3
143 0.639769702 0.746233359 1.288466795 1.199275252 -0.191953523 -1.190558618 -1.510994183 1.110022978 -1.818612315 -0.305436828 -0.295091115 -0.777032156 -0.720507695 3
144 0.762948725 2.339579121 -0.060204901 0.151234178 -0.542032695 -0.471537693 -1.23067425 0.868968781 -0.997449177 -0.283869168 -0.207591167 -0.791116852 -0.625241617 3
145 -0.924603893 1.381781388 -0.606963697 -0.297926283 0.858283992 -1.462188745 -1.250697102 -0.577356401 -0.787790503 1.359586476 -1.345090484 -0.861540337 0.343296842 3
146 0.196325219 1.104288587 -0.789216629 0.450674485 0.158125648 -1.270449832 -1.480959905 0.547563185 -0.508245605 -0.456410443 -1.563840353 -1.312250638 0.263908444 3
147 1.083214186 2.420141547 -0.497611938 0.151234178 -1.382222707 -2.101318456 -1.691199855 0.306508988 -1.591482085 -0.068192574 -1.6513403 -1.80521503 -1.053938968 3
148 -0.160893949 2.035232178 0.413652722 0.600394638 -0.962127701 -0.950884976 -1.380845643 0.868968781 -1.276994075 1.118028691 -1.826340195 -1.058726094 -0.387076422 3
149 0.393411656 0.808893024 0.049146858 0.600394638 -0.542032695 -0.583385392 -1.270719955 0.708265983 -0.595603386 1.450170645 -1.782590222 -1.39675882 -0.307688024 3
150 0.097782 1.399684149 -0.023754315 0.600394638 0.928299826 -1.414254017 -0.640000105 -0.175599406 -0.787790503 1.872896769 -1.695090274 -1.80521503 -0.625241617 3
151 0.615133898 0.701476456 0.923960931 1.348995406 1.62845817 -1.430232259 -0.459794433 -1.139816193 -0.595603386 1.527814219 -1.607590327 -1.847469121 -0.784018414 3
152 -0.259437168 0.298664325 0.413652722 0.750114792 0.858283992 -1.302406317 -0.670034383 -0.979113395 -0.578131829 2.476791231 -2.088840038 -1.608029273 -0.847529132 3
153 0.134735707 -0.390591988 1.397818554 1.798155867 1.138347329 -0.151972837 -0.750125793 -0.818410597 -0.053985145 0.880784438 -1.520090379 -1.80521503 -1.022183609 3
154 0.282550535 0.862601308 -0.315359006 -0.297926283 -0.121937689 -0.791102548 -1.200639971 1.993888367 0.487633095 2.356012338 -1.738840248 -1.551690486 -0.228299625 3
155 -0.518113116 -0.936626209 -0.971469561 0.151234178 0.228141483 -1.302406317 -1.450925626 1.351077175 -0.333530044 1.096461031 -1.6513403 -1.495351698 -0.339443383 3
156 0.208643121 2.554412258 -0.16955666 0.750114792 -0.472016861 -0.886972005 -1.400868495 1.993888367 -0.071456702 1.225866988 -1.563840353 -1.593944576 -0.069522829 3
157 1.033942577 1.596614524 0.049146858 0.001514024 -0.752080198 -0.791102548 -1.200639971 0.94932018 -0.053985145 1.704669026 -1.695090274 -1.368589426 -0.847529132 3
158 -0.678245847 0.62091403 0.996862104 2.247316327 -0.191953523 -0.63132012 -1.450925626 2.154591165 -0.787790503 1.053325713 -1.257590537 -1.241827154 0.422685241 3
159 1.649837693 -0.587522363 1.215565622 1.648435713 -0.121937689 0.806721729 -0.720091514 1.351077175 1.937772254 3.425768243 -1.695090274 -0.917879125 -0.275932664 3
160 0.590498093 -0.596473743 0.996862104 0.899834945 -0.752080198 0.487156874 -0.930331464 1.270725776 1.221438453 2.886576759 -1.695090274 -1.171403669 -0.402954102 3
161 -0.789106967 1.337024485 0.049146858 0.450674485 -0.822096033 0.007809591 -1.110537136 1.110022978 -0.962506065 1.118028691 -1.738840248 -1.453097607 -0.720507695 3
162 0.849174042 0.826795786 0.63235624 0.151234178 0.50820482 -0.74316782 -1.470948478 1.110022978 -1.381823412 0.354533549 0.011158701 -1.115064881 -0.212421946 3
163 -0.185529754 0.835747166 0.778158586 0.750114792 0.438188986 -1.03077619 -1.430902774 1.913536968 -1.102278514 0.225127593 -0.382591062 -0.706608671 -0.561730898 3
164 -0.050032828 0.996872019 -0.060204901 -0.297926283 0.438188986 -1.446210502 -1.330788512 0.306508988 -1.137221626 0.095721637 -1.213840563 -1.21365776 -0.228299625 3
165 0.960035163 0.379226751 -0.242457833 0.750114792 -0.682064364 -1.510123473 -1.350811364 0.386860387 -0.979977621 1.950540342 -1.126340616 -1.312250638 -0.418831781 3
166 0.898445651 1.811447661 -0.388260179 0.899834945 -0.822096033 -1.621971173 -1.561051314 1.270725776 -0.770318947 0.673734908 -0.776340826 -1.21365776 -0.720507695 3
167 0.553544386 1.220656536 0.851059758 1.049555099 0.788268158 -0.950884976 -1.110537136 0.547563185 -0.228700707 2.425028848 -0.47009101 -1.481267001 -0.164788907 3
168 -0.222483461 0.925260973 -0.242457833 0.001514024 -0.822096033 -1.302406317 -1.370834217 0.306508988 -1.084806957 2.24386051 -1.038840668 -1.21365776 -0.196544266 3
169 0.713677116 0.218101899 1.179115036 1.498715559 0.368173151 -1.190558618 -1.190628545 0.226157589 -0.088928258 1.55369541 -0.951340721 -1.143234275 0.009865569 3
170 0.491954874 2.026280797 1.798775004 1.648435713 0.858283992 -0.503494178 -1.070491431 -0.738059198 -0.840205172 1.4846789 -1.257590537 -0.974217912 -0.371198742 3
171 -0.986193405 0.62091403 -0.16955666 -0.14820613 -0.261969358 -1.669905901 -1.541028462 0.306508988 -1.504124305 0.190619338 -1.301340511 -1.100980185 -0.752263054 3
172 -0.284072972 0.048025666 -0.315359006 0.001514024 -0.962127701 -1.446210502 -1.521005609 0.94932018 -1.66136831 2.088572931 -1.695090274 -1.382674123 -0.879284492 3
173 1.428115451 0.155442234 0.413652722 0.151234178 -0.61204853 -0.982841462 -1.330788512 0.627914584 -0.613074942 2.002302725 -1.476340406 -1.269996547 -0.275932664 3
174 0.873809846 2.966175769 0.304300963 0.300954331 -0.331985192 -0.982841462 -1.420891348 1.270725776 -0.927562952 1.13959635 -1.388840458 -1.227742457 -0.02188979 3
175 0.491954874 1.40863553 0.413652722 1.049555099 0.158125648 -0.791102548 -1.280731381 0.547563185 -0.316058487 0.967055075 -1.126340616 -1.481267001 0.009865569 3
176 0.331822144 1.739836615 -0.388260179 0.151234178 1.418410667 -1.126645647 -1.340799938 0.547563185 -0.420887824 2.217979318 -1.607590327 -1.481267001 0.279786124 3
177 0.208643121 0.22705328 0.012696272 0.151234178 1.418410667 -1.03077619 -1.350811364 1.351077175 -0.228700707 1.82976145 -1.563840353 -1.39675882 0.295663803 3
178 1.391161744 1.578711763 1.361367968 1.498715559 -0.261969358 -0.391646479 -1.270719955 1.592131372 -0.420887824 1.786626131 -1.520090379 -1.424928213 -0.593486258 3
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.dot {
stroke: none;
}
.tooltip{
position: absolute;
width: 130px;
height: 50px;
padding: 2px;
font: 12px sans-serif;
background: lightgreen;
border: 2px;
border-radius: 5px;
/* border-left: 1px;
border-right: 1px;*/
pointer-events: none;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category20();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
d3.csv("clusters.csv", function(error, data) {
if (error) throw error;
data.forEach(function(d) {
d.Alcohol = +d.Alcohol;
d.Malic = +d.Malic;
d.cluster = d.cluster;
});
x.domain(d3.extent(data, function(d) { return d.Alcohol; })).nice();
y.domain(d3.extent(data, function(d) { return d.Malic; })).nice();
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("Alcohol");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Malic")
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 5)
.attr("cx", function(d) { return x(d.Alcohol); })
.attr("cy", function(d) { return y(d.Malic); })
.style("fill", function(d) { return color(d.cluster); })
.on("mouseover", function(d){
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html("Cluster: " + d.cluster + "<br/> Alcohol: " + d.Alcohol + "<br/> Malic: " + d.Malic)
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY - 20) + "px");
})
.on("mouseout", function(d){
tooltip.transition()
.duration(500)
.style("opacity",0);
});
var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment